题解:CF2117C 酷分割
题目传送门
CF。
洛谷。
思路
首先题目说了:
如果对于每个分段
b_j 来说,b_j 中的所有元素也出现在b_{j + 1} 中
很明显我们需要用 set 维护一下。
具体的,我们拿两个 set
这里有些同学可能会不明白:为啥不是
时间复杂度 unordered_map 或 unordered_set。
代码
既然你都做到了 CF 的 C 题,那我就当你是会 set 啦。不过不会的可以去 oi-wiki 上学习。
还是不懂的可以看注释。
#include <bits/stdc++.h>
using namespace std;
int n, a[200001];
set<int> s1, s2;
int main() {
int t;
scanf("%d", &t);
for (; t--; ) {
scanf("%d", &n);
s1.clear(), s2.clear(); //多测清空
int ans = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
s1.insert(a[i]);
s2.insert(a[i]);
if (s1.size() == s2.size()) { //可以划分
++ans;
s1.clear();
}
}
printf("%d\n", max(ans, 1)); //别忘了把整个序列作为答案也是一种
}
}
提交记录。
说句闲话
为啥我要来写橙题题解。