#116 div2
一个长度为n的序列,每个格子被涂成了m种颜色中的一种,让你删除不超过k个格子,使得得到的新序列中,同样颜色的子序列长度最大,并输出这个长度
首先预处理,用vector把同种颜色的格子的下标记录下来,然后用双指针枚举相同颜色的前后区间,通过下标的加减来计算不同颜色的格子的数目,如果小于等于k那就更新答案
#include <iostream>
#include <vector>
#include <utility>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define rep(i, j, k) for(int i = j; i <= k; i++)
using namespace std;
int n, m, k, ans = 0;
vector<int> f[100009];
int a[200009];
int main ()
{
cin >> n >> m >> k;
rep (i, 1, n)
scanf ("%d", &a[i]);
rep (i, 1, n)
f[a[i]].push_back (i);
rep (i, 1, m)
{
//printf ("color %d\n", i);
int t = f[i].size ();
int r = 0, now = 0;
rep (l, 0, t - 1)
{
while (r < t - 1 && now + f[i][r + 1] - f[i][r] - 1 <= k)
r++, now += f[i][r] - f[i][r - 1] - 1;
//cout << l << ' ' << r << endl;
ans = max (ans, r - l + 1);
now -= (f[i][l + 1] - f[i][l] - 1);
}
}
cout << ans << endl;
return 0;
}