#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <cctype>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <limits>
#include <fstream>
using namespace std;
#define mem(A, X) memset(A, X, sizeof A)
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define rep1(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#define rep2(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
typedef long long int64;
int gcd(const int64 &a, const int64 &b) {return b == 0 ? a : gcd(b, a % b);}
int64 int64pow(int64 a, int64 b){if(b == 0) return 1;int64 t = int64pow(a, b / 2);if(b % 2) return t * t * a;return t * t;}
const int inf = 1 << 29;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int MAX_N = 100000 + 5;
struct node {
int color, pos;
bool operator < (const node &t) const{
return color != t.color ? color < t.color : pos < t.pos;
}
};
node arr[MAX_N];
int n, m;
void solve()
{
rep2(i, 1, n) {
scanf("%d", &arr[i].color);
arr[i].pos = i;
}
sort(arr + 1, arr + n + 1);//最长长度定是由相同颜色砖块得到的
int ans = 1, tmpans = 1, cnt = 1;
int l = 1, op = m;
rep2(i, 2, n) {
if (arr[i].color == arr[i - 1].color) {
op -= arr[i].pos - arr[i - 1].pos - 1;
++cnt;
while (op < 0) {
op += arr[l + 1].pos - arr[l].pos - 1;//颜色相同情况下长度+1所需要消耗的操作数
++l;
--cnt;
}
tmpans = max(tmpans, cnt);//保留在符合要求的操作数下长度最大值
}
else {
ans = max(ans, tmpans);
l = i, op = m;
tmpans = 1, cnt = 1;
}
}
ans = max(ans, tmpans);
cout << ans << endl;
}
int main()
{
while (cin >> n >> m) {
solve();
}
return 0;
}
ZOJ 3790 Consecutive Blocks
最新推荐文章于 2016-03-23 19:18:11 发布