题解 gym102586A 【Cookies】
为什么鸽笼原理总会以一种奇怪的方式出现在时间复杂度分析中 /youl
暴力模拟是
不妨先考虑 S 的情况。设第
- 按
j 从前到后扫一遍c_j 。 - 若
c_j < a_i ,我们可以不选择c_j 转而选择a_i 、如果原c_j 在后面有用还可以留给后面。 - 于是我们此时
\operatorname{swap}(c_j, a_i) 即可。 - 不难发现扫到最后会剩下一个
a_i ,显然这个数后面都用不到了,将其加入答案。
考虑将其扩展到一般情况,不难发现我们只需要根据
注意到对于一个
- 若
s_i = S,当a_i > \min(c_j) ,用a_i 替换\min(c_j) 。 - 若
s_i = B,当a_i < \max(c_j) ,用a_i 替换\max(c_j) 。
把连续段缩起来,用堆维护即可。时间复杂度为 甚至比刚刚那个东西更劣了 /ng
人类智慧地,注意到对于两个相邻的 S/B 连续段,若前者的
然后你发现这样写就直接过了,下面我们来分析一下时间复杂度:
- 设一开始的连续段为
S_1, B_1, S_2, B_2, \cdots, S_p, B_p (忽略两端的\leq 2 段)。 - 由于
\displaystyle\sum_{i = 1}^p (|S_i| + |B_i|) \leq m ,则根据鸽笼原理,一定存在\geq \frac{p}{2} 个i ,使得|S_i| + |B_i| \leq \frac{2m}{p} 。 - 设有势能
\phi(i) 表示S_i 中< \max(B_i) 的元素个数加上B_i 中> \min(S_i) 的元素个数,则讨论一下可得每操作一次S_i, B_i ,\phi(i) 至少减少1 。 - 在
\frac{2m}{p} 次操作后,每个位置i 有三种情况:自己势能归零而被交换、i - 1 被交换、i + 1 被交换。 - 于是第一种情况的次数总和必然
\geq \frac{p}{6} ,则此时p 至少会减少至\frac{5p}{6} 。 - 设
T(p) 表示p 给定时的时间复杂度,则T(p) = T(\frac{5p}{6}) + O(m \log m) ,解得T(p) = O(m \log^2 m) 。
于是时间复杂度为
代码:
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef struct {
int ls;
int rs;
int dis;
int val;
} Node;
const int mod = 1e9;
int id = 0;
int a[200007], b[200007], cnt[7], type[7][200007], root[7][200007];
char s[200007];
Node tree[22000007];
inline int new_node(int x){
id++;
tree[id].dis = 1;
tree[id].val = x;
return id;
}
int merge(int type, int x, int y){
if (x == 0) return y;
if (y == 0) return x;
if ((type == 0) == (tree[x].val > tree[y].val)) swap(x, y);
tree[x].rs = merge(type, tree[x].rs, y);
if (tree[tree[x].ls].dis < tree[tree[x].rs].dis) swap(tree[x].ls, tree[x].rs);
tree[x].dis = tree[tree[x].rs].dis + 1;
return x;
}
inline void push(int type, int &x, int k){
x = merge(type, x, new_node(k));
}
inline void pop(int type, int &x){
x = merge(type, tree[x].ls, tree[x].rs);
}
int main(){
int n, m;
ll last_ans = 0;
scanf("%d", &n);
for (register int i = 1; i <= n; i++){
scanf("%d", &a[i]);
}
scanf("%d", &m);
for (register int i = 1; i <= m; i++){
scanf("%d", &b[i]);
}
scanf("%s", &s[1]);
for (register int i = 1; i <= m; ){
int nxt = i;
while (nxt < m && s[i] == s[nxt + 1]) nxt++;
type[0][++cnt[0]] = s[i] == 'S' ? 0 : 1;
for (register int j = i; j <= nxt; j++){
push(type[0][cnt[0]], root[0][cnt[0]], b[j]);
}
i = nxt + 1;
}
for (register int i = 1; i <= n; i++){
int cur = i & 1, pre = cur ^ 1;
a[i] = (int)((a[i] + last_ans) % mod);
for (register int j = 1; j <= cnt[pre]; j++){
int x = tree[root[pre][j]].val;
if ((type[pre][j] == 0) == (a[i] > x)){
pop(type[pre][j], root[pre][j]);
push(type[pre][j], root[pre][j], a[i]);
a[i] = x;
}
}
last_ans += a[i];
cout << last_ans << " ";
cnt[cur] = 0;
for (register int j = 1; j <= cnt[pre]; j++){
if (cnt[cur] == 0){
cnt[cur]++;
type[cur][cnt[cur]] = type[pre][j];
root[cur][cnt[cur]] = root[pre][j];
} else if (type[cur][cnt[cur]] == type[pre][j]){
root[cur][cnt[cur]] = merge(type[pre][j], root[cur][cnt[cur]], root[pre][j]);
} else if (cnt[cur] > 1 && (type[cur][cnt[cur]] == 0) == (tree[root[cur][cnt[cur]]].val > tree[root[pre][j]].val)){
root[cur][cnt[cur] - 1] = merge(type[pre][j], root[cur][cnt[cur] - 1], root[pre][j]);
} else {
cnt[cur]++;
type[cur][cnt[cur]] = type[pre][j];
root[cur][cnt[cur]] = root[pre][j];
}
}
}
return 0;
}