• 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
    ", res);
    52     return 0;
    53 }
  • 相关阅读:
    extern C的作用详解
    UIWindow in iOS
    iOS会议和组织
    Fantageek翻译系列之《使用Autolayout显示变化高度的UITableViewCell》
    KVO的概述的使用
    Struts2 基于XML校验(易百教程)
    Maven项目中添加JDBC驱动
    org.dom4j.DocumentException: null Nested exception: null解决方法
    struts2中的数据类型自动转换
    struts2的拦截器
  • 原文地址:https://www.cnblogs.com/Recoder/p/5724138.html
Copyright © 2020-2023  润新知