GMOI R2 T2 猫耳小 官方题解
本题原来的数据范围是
首先特判
否则,对于
时间复杂度
因此,我们只需要考虑将所有修改的位置都改为
同样的理由,我们不需要考虑原数列中包含
在子任务三中,一段区间的
引理二:存在一种最优方案,使得所有被修改的位置都是上述区间的右端点。
证明:考虑任意一种最优方案,假设一次修改没有改右端点,则改右端点一定不劣。原因是这样可以尽量缩减问题的规模,由于
子任务三的贪心算法正确性得证。根据上述推理,原问题的贪心算法正确性得证。
代码:
//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do{freopen(s".in","r",stdin);freopen(s".out","w",stdout);}while(false)
#define likely(exp) __builtin_expect(!!(exp), 1)
#define unlikely(exp) __builtin_expect(!!(exp), 0)
using namespace std;
typedef long long ll;
mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
int randint(int L, int R) {
uniform_int_distribution<int> dist(L, R);
return dist(rnd);
}
template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}
const int N = 5e3+5;
int n, k, a[N], cnt[N], mex, ans;
int main() {
scanf("%d%d", &n, &k);
rep(i, 1, n) scanf("%d", &a[i]);
if(!k) {
rep(i, 1, n) if(a[i]) ++ans;
}
else {
for(int l = 0, r = 1; r <= n; r++) {
if(a[r] > k) continue;
if(a[r] == k) {
while(++l < r) if(a[l] < k) --cnt[a[l]];
mex = 0;
}
else {
++cnt[a[r]];
while(cnt[mex]) ++mex;
if(mex == k) {
++ans;
while(++l < r) if(a[l] < k) --cnt[a[l]];
--cnt[a[r]];
mex = 0;
}
}
}
}
printf("%d\n", ans);
return 0;
}