Codeforces Round #466 (Div. 2) 题解
A.Points on the line
题目大意:
给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权值小于等于给定值d
题解:
排序,每次挑最大的和最小的,看看最小的能跟多少个数差\(>d\),看看最大的能跟多少个数差\(>d\),取个数大的那个删除。复杂度\(n^2 + n\log n\)
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const int INF = 0x3f3f3f3f;
const int MAXN = 100 + 10;
int n,d,num[MAXN];
int main()
{
// freopen("data.txt", "r", stdin);
read(n), read(d);
for(int i = 1;i <= n;++ i) read(num[i]);
std::sort(num + 1, num + 1 + n);
int l = 1, r = n, ans = 0;
while(l <= r)
{
int tmp1 = r, tmp2;
while(num[tmp1] - num[l] > d) -- tmp1;
tmp2 = r - tmp1;
int tmp3 = l, tmp4;
while(num[r] - num[tmp3] > d) ++ tmp3;
tmp4 = tmp3 - l;
if(tmp2 == 0 && tmp4 == 0) break;
if(tmp2 >= tmp4) ++ l;
else -- r;
++ ans;
}
printf("%d", ans);
return 0;
}
B.Our Tanya is Crying Out Loud
题目大意:
给你一个数\(n\),可以对它指向两种操作。第一种操作令\(n\)减去\(1\),花费\(A\);第二种操作,当且仅当\(n\)为\(k\)的倍数时可以执行,使得\(n /= k\),花费\(B\),求让\(n\)变为\(1\)的最小花费
题解:
直接模拟即可,如果不是\(k\)的倍数就暴力减到\(k\)的倍数;如果是\(k\)的倍数,看是\(\div k\)优还是一步一步\(-1\)到\(\frac{n}{k}\)优即可。复杂度\(\log_kn\),注意特判\(k = 1\)的情况。因为脑残\(fst\)掉了。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f;
long long n,k,a,b;
int main()
{
// freopen("data.txt", "r", stdin);
read(n), read(k), read(a), read(b);
if(k == 1) printf("%I64d", (n - 1) * a);
else
{
long long ans = 0;
while(n > 1)
{
if(n % k == 0)
{
if(b < (n - n/k) * a) ans += b, n /= k;
else ans += (n - n/k) * a, n /= k;
}
else
{
if(n - n%k > 1) ans += a * (n%k), n -= n%k;
else ans += a * (n % k - 1), n = 1;
}
}
printf("%I64d", ans);
}
return 0;
}
C. Phone Numbers
题目大意:
给你一个长度为\(n\)字符串,要求你用字符串里的字符拼凑出一个字典序最小的比该字符串字典序大的长度为\(k\)字符串
题解:
贪心即可。若\(n < k\),直接输出原串,并在末尾加字典序最小的字符;若\(n>=k\),从给的字符串第\(k\)位往前扫,尝试某一位换位字典序比他大的字符里字典序最小的哪一个,如果能换,那么就换掉,后面的用字典序最小的字符填满\(k\)位,就是答案
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const int INF = 0x3f3f3f3f;
const int MAXN = 100000 + 10;
char s[MAXN];
int n,k;
int a[30],tot,b[30], nxt[30];
int main()
{
// freopen("data.txt", "r", stdin);
read(n), read(k);
scanf("%s", s + 1);
for(int i = 1;i <= n;++ i)
if(!b[s[i] - 'a' + 1])
b[s[i] - 'a' + 1] = ++ tot, a[tot] = s[i] - 'a' + 1;
std::sort(a + 1, a + 1 + tot);
for(int i = 1;i <= tot;++ i)
nxt[a[i]] = a[i + 1];
if(k <= n)
{
for(int i = k;i >= 1;-- i)
{
if(nxt[s[i] - 'a' + 1])
{
for(int j = 1;j < i;++ j) printf("%c", s[j]);
printf("%c", nxt[s[i] - 'a' + 1] + 'a' - 1);
for(int j = i + 1;j <= k;++ j) printf("%c", a[1] + 'a' - 1);
return 0;
}
}
}
else
{
printf("%s", s + 1);
for(int i = n + 1;i <= k;++ i) printf("%c", a[1] + 'a' - 1);
}
return 0;
}
D. Alena And The Heater
题目大意:
给你两个长度均为\(n\)的数列\(a\)和\(b\),要求找到任意一组\(l\),\(r\),对于任意\(5 <= i <= n\),满足:
\(b_i?=?0\),如果\(a_i,?a_{i-1},?a_{i-2},?a_{i-3},?a_{i-4}?>?r\)并且\(b_{i-1}=b_{i-2}=b_{i-3}=b_{i-4}=1\)
\(b_i?=?1\),如果\(a_i,?a_{i-1},?a_{i-2},?a_{i-3},?a_{i-4}?< l\)并且\(b_{i-1}=b_{i-2}=b_{i-3}=b_{i-4}=0\)
其余情况\(b_i?=?b_{i-1}\)
题解:
如果\(b_{i-1}=b_{i-2}=b_{i-3}=b_{i-4}=1\)
如果\(b_i?=?0\) 那么 \(a_i,?a_{i-1},?a_{i-2},?a_{i-3},?a_{i-4}?>?r\)
如果\(b_i?=?1\) 那么 \(a_i,?a_{i-1},?a_{i-2},?a_{i-3},?a_{i-4}?\leq\ r\)
如果bi-1 = bi-2 = bi-3 = bi-4 = 0
如果\(b_i?=?0\) 那么 \(a_i,?a_{i-1},?a_{i-2},?a_{i-3},?a_{i-4}?\geq\ ?r\)
如果\(b_i?=?0\) 那么 \(a_i,?a_{i-1},?a_{i-2},?a_{i-3},?a_{i-4}?< l\)
于是可以把l = -INF,r = INF,往里缩小,由于答案一定存在,我们只需让\(a_i,?a_{i-1},?a_{i-2},?a_{i-3},?a_{i-4}?>?r\)的条件令\(r = min(a_i + 1,?a_{i-1} + 1,?a_{i-2} + 1,?a_{i-3} + 1,?a_{i-4} + 1)\),即满足条件的最小值,这样由于答案存在,也就一定能满足所有的\(b_i?=?1\) 那么 \(a_i,?a_{i-1},?a_{i-2},?a_{i-3},?a_{i-4}?\leq\ r\)。l同理。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const long long INF = 1000000000;
const long long MAXN = 1000000 + 10;
/*
如果bi-1 = bi-2 = bi-3 = bi-4 = 1
如果bi = 0 那么 ai,ai-1,ai-2,ai-3,ai-4 > r
如果bi = 1 那么 ai,ai-1,ai-2,ai-3,ai-4 <= r
如果bi-1 = bi-2 = bi-3 = bi-4 = 0
如果bi = 0 那么 ai,ai-1,ai-2,ai-3,ai-4 >= l
如果bi = 1 那么 ai,ai-1,ai-2,ai-3,ai-4 < l
*/
long long l = - INF, r = INF, n, a[MAXN], b[MAXN];
char s[MAXN];
int main()
{
freopen("data.txt", "r", stdin);
read(n);
for(long long i = 1;i <= n;++ i) read(a[i]);
scanf("%s", s + 1);
for(long long i = 1;i <= n;++ i) b[i] = s[i] - '0';
for(long long i = 5;i <= n;++ i)
{
if(b[i - 1] && b[i - 2] && b[i - 3] && b[i - 4])
{
if(!b[i])
r = min(r, min(a[i - 1] - 1, min(a[i - 2] - 1, min(a[i - 3] - 1, min(a[i - 4] - 1, a[i] - 1)))));
}
else if(!b[i - 1] && !b[i - 2] && !b[i - 3] && !b[i - 4])
{
if(b[i])
l = max(l, max(a[i - 1] + 1, max(a[i - 2] + 1, max(a[i - 3] + 1, max(a[i - 4] + 1, a[i] + 1)))));
}
}
printf("%I64d %I64d\n", l, r);
return 0;
}
E
题目大意:
给你一个数列和正数\(c\),你可以把它分成很多段,每个长度为\(l\)的段将会删除前\(\lfloor\frac{l}{c}\rfloor\)小的数,求一种划分,使得划分并删除数字后剩余数字的和最小。
题解:
首先是一个显而易见的套路\(DP\).
\(dp[i]\)表示到i的最小和,\(dp[i] = min(dp[i], dp[i - k] + calc(i - k + 1, i))\);
\(calc(l,r)\)表示\([l,r]\)单独分为一段,删除数后的和
然后就GG了。
想了两分钟想不动了,翻了翻别人的代码。。
cnm。。。
早知道再想想了。。。
首先每一个\(kc + r\),\(k \geq 1\), \(1 \leq r < c\)长度的区间,等价于划分为一个\(kc\)长度的区间和一个\(r\)长度的区间。
接下来考虑每一个\(kc\),\(k > 1\)的区间
把它分为\(k\)个长度为\(c\)的区间,会发现每个区间都有一个在大区间里被删除的数,而且这个数也是小区间内该被删除的数
为什么?反证法即可。如果大区间选的两个数在同一个长度为\(c\)的小区间里,鸽巢原理会导致一个区间空着,空着的那个区间删的数肯定比小区间的数大(否则就会被在大区间里被选了),那这种划分显然不是最优的。
于是可以发现,只需要\(c\)长度的区间就行了!问题转化为从一个数列里选一些长度为\(c\)的小区间,删掉里面最小的数。\(DP\)方程可以这样写:
\(dp[i] = min(dp[i - 1], dp[i - c] + sum[i] - sum[i - c] - min(i - c + 1, i))\)
\(min(i - c + 1, i)\)表示区间\([i - c + 1, i]\)的最小值
sgt或st表维护即可
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
//#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
inline long long min(long long a, long long b)
{
return a < b ? a : b;
}
template <class T>
inline void swap(T &x, T &y)
{
T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 400000 + 10;
long long mi[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(long long o = 1, long long l = 1, long long r = n)
{
if(l == r)
{
mi[o] = a[l];return;
}
long long mid = (l + r) >> 1;
build(o << 1, l, mid);
build(o << 1 | 1, mid + 1, r);
mi[o] = min(mi[o << 1], mi[o << 1 | 1]);
}
long long ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n)
{
if(ll <= l && rr >= r) return mi[o];
long long mid = (l + r) >> 1, re = INF;
if(mid >= ll) re = ask(ll, rr, o << 1, l, mid);
if(mid < rr) re = min(re, ask(ll, rr, o << 1 | 1, mid + 1, r));
return re;
}
int main()
{
//freopen("data.txt", "r", stdin);
read(n), read(c);
for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
build(1,1,n);
for(long long i = c;i <= n;++ i)
dp[i] = max(dp[i - 1], dp[i - c] + ask(i - c + 1, i,1,1,n)), ans = max(ans, dp[i]);
printf("%I64d", sum - ans);
return 0;
}