跳到正文

个人记录

题解 gym102586A 【Cookies】

Leasier约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年6月10日
篇幅
约 8 分钟
获赞
2

版本

  1. v1当前

为什么鸽笼原理总会以一种奇怪的方式出现在时间复杂度分析中 /youl


暴力模拟是 O(nmlog⁡n)O(nm \log n) 的,且看上去并没有多大的优化空间 /ng

不妨先考虑 si=s_i = S 的情况。设第 ii 个学生最终取到的蔬菜的营养值为 cic_i,此时我们每插入一个 aia_i,cjc_j 发生的变化形如:

  • 按 jj 从前到后扫一遍 cjc_j。
  • 若 cj<aic_j < a_i,我们可以不选择 cjc_j 转而选择 aia_i、如果原 cjc_j 在后面有用还可以留给后面。
  • 于是我们此时 swap⁡(cj,ai)\operatorname{swap}(c_j, a_i) 即可。
  • 不难发现扫到最后会剩下一个 aia_i,显然这个数后面都用不到了,将其加入答案。

考虑将其扩展到一般情况,不难发现我们只需要根据 sjs_j 是啥决定符号是 <,><, > 即可。时间复杂度为 O(nm)O(nm)。

注意到对于一个 sis_i 相等的段,数集的变化是:

  • 若 si=s_i = S,当 ai>min⁡(cj)a_i > \min(c_j),用 aia_i 替换 min⁡(cj)\min(c_j)。
  • 若 si=s_i = B,当 ai<max⁡(cj)a_i < \max(c_j),用 aia_i 替换 max⁡(cj)\max(c_j)。

把连续段缩起来,用堆维护即可。时间复杂度为 O(nmlog⁡n)O(nm \log n)。甚至比刚刚那个东西更劣了 /ng

人类智慧地,注意到对于两个相邻的 si=s_i = S/B 连续段,若前者的 min⁡\min 大于后者的 max⁡\max,则讨论一下可以发现这两个段交换后无影响,于是我们可以通过交换再把相邻且 sis_i 相同的连续段缩起来。

然后你发现这样写就直接过了,下面我们来分析一下时间复杂度:

  • 设一开始的连续段为 S1,B1,S2,B2,⋯ ,Sp,BpS_1, B_1, S_2, B_2, \cdots, S_p, B_p(忽略两端的 ≤2\leq 2 段)。
  • 由于 ∑i=1p(∣Si∣+∣Bi∣)≤m\displaystyle\sum_{i = 1}^p (|S_i| + |B_i|) \leq m,则根据鸽笼原理,一定存在 ≥p2\geq \frac{p}{2} 个 ii,使得 ∣Si∣+∣Bi∣≤2mp|S_i| + |B_i| \leq \frac{2m}{p}。
  • 设有势能 ϕ(i)\phi(i) 表示 SiS_i 中 <max⁡(Bi)< \max(B_i) 的元素个数加上 BiB_i 中 >min⁡(Si)> \min(S_i) 的元素个数,则讨论一下可得每操作一次 Si,BiS_i, B_i,ϕ(i)\phi(i) 至少减少 11。
  • 在 2mp\frac{2m}{p} 次操作后,每个位置 ii 有三种情况:自己势能归零而被交换、i−1i - 1 被交换、i+1i + 1 被交换。
  • 于是第一种情况的次数总和必然 ≥p6\geq \frac{p}{6},则此时 pp 至少会减少至 5p6\frac{5p}{6}。
  • 设 T(p)T(p) 表示 pp 给定时的时间复杂度,则 T(p)=T(5p6)+O(mlog⁡m)T(p) = T(\frac{5p}{6}) + O(m \log m),解得 T(p)=O(mlog⁡2m)T(p) = O(m \log^2 m)。

于是时间复杂度为 O(nlog⁡m+mlog⁡2m)O(n \log m + m \log^2 m)。

代码:

C++
#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;
}
原文