codeforces 180E Cubes 双指针经典题

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值