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

Let’s imagine that you’re playing the following simple computer game. The screen displays n lined-up cubes. Each cube is painted one of m colors. You are allowed to delete not more than k cubes (that do not necessarily go one after another). After that, the remaining cubes join together (so that the gaps are closed) and the system counts the score. The number of points you score equals to the length of the maximum sequence of cubes of the same color that follow consecutively. Write a program that determines the maximum possible number of points you can score.

Remember, you may delete no more than k any cubes. It is allowed not to delete cubes at all.

Input

The first line contains three integers n, m and k (1?≤?n?≤?2·105,?1?≤?m?≤?105,?0?≤?k?<?n). The second line contains n integers from 1 to m — the numbers of cube colors. The numbers of colors are separated by single spaces.

Output

Print the maximum possible number of points you can score.

Sample test(s)

Input

10 3 2

1 2 1 1 3 2 1 1 2 2

Output

4

Input

10 2 2

1 2 1 2 1 1 2 1 1 2

Output

5

Input

3 1 2

1 1 1

Output

3

Note

In the first sample you should delete the fifth and the sixth cubes.

In the second sample you should delete the fourth and the seventh cubes.

In the third sample you shouldn’t delete any cubes.

对颜色分组,然后组内用尺取法就行了

/*************************************************************************
    > File Name: CF-116-E.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年03月30日 星期一 19时53分05秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 200100;
vector <int> cols[100100];

int main()
{
    int n, m, k;
    while (~scanf("%d%d%d", &n, &m, &k))
    {
        for (int i = 1; i <= m; ++i)
        {
            cols[i].clear();
        }
        int val;
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d", &val);
            cols[val].push_back(i);
        }
        int ans = 0;
        for (int i = 1; i <= m; ++i)
        {
            int cnt = cols[i].size();
            int s = 0;
            int e = s;
            int num = 0;
            while (1)
            {
                while (e < cnt && num <= k)
                {
                    ++e;
                    if (e >= cnt)
                    {
                        break;
                    }
                    num += cols[i][e] - cols[i][e - 1] - 1;
                }
                ans = max(ans, e - s);
                if (e >= cnt)
                {
                    break;
                }
                ++s;
                num -= cols[i][s] - cols[i][s - 1] - 1;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-11-10 18:50:16

Codeforces Round #116 (Div. 2, ACM-ICPC Rules)---E. Cubes的相关文章

codeforces水题100道 第十八题 Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table (brute force)

题目链接:http://www.codeforces.com/problemset/problem/509/A题意:f[i][1]=f[1][i]=1,f[i][j]=f[i-1][j]+f[i][j-1],求f[n][n].C++代码: #include <iostream> using namespace std; int n, f[11][11]; int main() { cin >> n; for (int i=1;i<=n;i++) f[i][1] = f[1][

Codeforces Round #289 (Div. 2, ACM ICPC Rules)——B贪心——Painting Pebbles

There are n piles of pebbles on the table, the i-th pile contains ai pebbles. Your task is to paint each pebble using one of the k given colors so that for each color c and any two piles i and j the difference between the number of pebbles of color c

Codeforces Round #289 (Div. 2, ACM ICPC Rules) (A, B, C, E)

A:水题,根据题目预处理一下输出即可 B:先把最大和最小找出来,可以让最小全是1,然后最大比最小多出的部分就放1,2,3,4,5...所以如果MAX - MIN > k就是NO,不然就根据这个构造出答案 C:贪心的策略,每次要让数字尽量小,那么就和上一个数字比较,如果需要的和比上一个小,就先找到一个新数字,使得和小于所需数字,并且该数字是大于上一个数字的最小值,找的方法就是从末尾不断放0进位.那么现在情况就只剩下需要的和比上一个大的了,这个就贪心,从末尾尽量变成9即可 E:一个计数问题,其实只要

Codeforces Round #289 (Div. 2, ACM ICPC Rules)

A题: 有一个n*n的矩阵,矩阵的第一行和第一列的值都为1,其余的有: a[i][j]=a[i-1][j]+a[i][j-1]; 现在给出一个n求出这个n*n的矩阵中最大的数. 显然,最大的数就是a[n][n]. 因为n<=10,所以先预处理出一个10*10的矩阵,然后每输入一个n,直接输出a[n][n]. 1 #include<cstdio> 2 int maze[11][11]; 3 int main() 4 { 5 for(int i=1;i<=10;i++) 6 maze[

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, "/S

Codeforces Round #297 (Div. 2) E题. Anya and Cubes (中途相遇法)

题目地址:Anya and Cubes 比赛的时候居然没想起中途相遇法...这题也是属于想起来就很简单系列. 中途相遇法也叫折半搜索.就是处理前一半,把结果储存起来,再处理后一半,然后匹配前一半存储的结果. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un

Codeforces Round #259 (Div. 2) 解题报告

终于重上DIV1了.... A:在正方形中输出一个菱形 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月01日 星期五 23时27分55秒 4 5 #include<vector> 6 #include<set> 7 #include<deque> 8 #include<stack> 9 #include<bitset> 10 #inclu

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T