Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)

题目链接:http://codeforces.com/problemset/problem/180/E

给你n个数,每个数代表一种颜色,给你1到m的m种颜色。最多可以删k个数,问你最长连续相同颜色的序列的长度是多少。

将相同颜色的下标存到对应颜色的容器中,比如ans[a[i]].push_back(i)就是将下标为i颜色为a[i]的数存到ans[a[i]]容器中。

对于每种颜色序列,尺取一下 在差距小于k的情况下取能取到的最大长度。

 1 //#pragma comment(linker, "/STACK:102400000, 102400000")
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cstdio>
 7 #include <vector>
 8 #include <cmath>
 9 #include <ctime>
10 #include <list>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef long long LL;
15 typedef pair <int, int> P;
16 const int N = 1e5 + 5;
17 vector <int> ans[N];
18 int a[N*2];
19 
20 int solve(int u, int k) {
21     int len = ans[u].size(), notcnt = 0, cnt = 1, l = 0, res = 1;
22     //notcnt表示中间删除数的多少,cnt表示颜色为u的序列的长度
23     for(int i = 1; i < len; ++i) {
24         int v = ans[u][i];
25         notcnt += v - ans[u][i - 1] - 1; 
26         cnt++;
27         while(notcnt > k && l <= i) { 
28             notcnt -= ans[u][l + 1] - ans[u][l] - 1;
29             cnt--;
30             l++;
31         }
32         res = max(cnt, res);
33     }
34     return res;
35 }
36 
37 int main()
38 {
39     int n, m, k;
40     scanf("%d %d %d", &n, &m, &k);
41     for(int i = 1; i <= n; ++i) {
42         scanf("%d", a + i);
43         ans[a[i]].push_back(i);
44     }
45     int res = 1;
46     for(int i = 1; i <= m; ++i) {
47         if(ans[i].size()) { //要是存在颜色为i的数
48             res = max(res, solve(i, k));
49         }
50     }
51     printf("%d\n", res);
52     return 0;
53 }

 

转载于:https://www.cnblogs.com/Recoder/p/5724138.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值