problemCode=3790">Consecutive Blocks

先离散一下,然后模拟,把一种颜色i所在的位置都放入G[i]中。然后枚举一下终点位置,滑动窗体使得起点和终点间花费不超过K,求中间过程的最大值就可以。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <string>
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. using namespace std;
  14. #define N 100005
  15. set<int>myset;
  16. map<int,int>mp;
  17. set<int>::iterator p;
  18. int n ,k;
  19. int a[N];
  20. vector<int>s[N];
  21. int go(int cur){
  22. int now = k;
  23. int ans = 1, l = 0, siz = s[cur].size();
  24. int len = 1;
  25. for(int i = 1; i < siz; i++){
  26. if(s[cur][i-1] == s[cur][i]-1)
  27. {
  28. len++;
  29. ans = max(ans, len);
  30. continue;
  31. }
  32. now -= s[cur][i]-s[cur][i-1]-1;
  33. if(now>=0)
  34. { len++; ans = max(ans, len);}
  35. else
  36. {
  37. while(now<0)
  38. {
  39. l++;
  40. now += s[cur][l]-s[cur][l-1]-1;
  41. len--;
  42. }
  43. len++;
  44. }
  45. }
  46. return ans;
  47. }
  48. int main()
  49. {
  50. int T ,m,u,v,w, i ,j;
  51. while(~scanf("%d %d",&n,&k)){
  52. myset.clear();
  53. mp.clear();
  54. for(i=1;i<=n;i++)scanf("%d",&a[i]),myset.insert(a[i]);
  55. for(p=myset.begin(), i = 1; p!=myset.end(); p++, i++)
  56. mp.insert(pair<int,int>(*p,i)),s[i].clear();
  57. int top = i;
  58. for(i=1;i<=n;i++)
  59. {
  60. a[i] = mp.find(a[i])->second;
  61. s[a[i]].push_back(i);
  62. }
  63. int ans = 0;
  64. for(i=1;i<top;i++)
  65. ans = max(ans, go(i));
  66. printf("%d\n",ans);
  67. }
  68. return 0;
  69. }
  70. /*
  71. 13 3
  72. 1000000000 2 2 1 1 2 2 5 6 8 2 2 2
  73.  
  74. */

ZOJ 3790 Consecutive Blocks 模拟题的更多相关文章

  1. zoj 3790 Consecutive Blocks 离散化+二分

    There are N (1 ≤ N ≤ 105) colored blocks (numbered 1 to N from left to right) which are lined up in ...

  2. ZOJ 3790 Consecutive Blocks

    大致题意就是给你一个数列,让你最多移除K个数字,使得连续的相同数字的长度最大,并求出最大长度. 我们将此序列按颜色排序,颜色相同的话按位置排序,那么排完序之后颜色相同的blocks就在一起,只是他们的 ...

  3. ZOJ 3790 Consecutive Blocks (离散化 + 暴力)

    题目链接 虽然是一道暴力的题目,但是思路不好想.刚开始还超时,剪枝了以后1200ms,不知道为什么还是这么慢. 题意:给你n个点,每个点有一种颜色ci,给你至多k次删除操作,每次删除一个点,问最多k次 ...

  4. ZOJ 3818 Pretty Poem 模拟题

    这题在比赛的时候WA到写不出来,也有判断ABC子串不一样不过写的很差一直WA 在整理清思路后重写一遍3Y 解题思路如下: 第一种情况:ABABA. 先判断开头的A与结尾的A,得到A的长度, 接着判断A ...

  5. Capture the Flag ZOJ - 3879(模拟题)

    In computer security, Capture the Flag (CTF) is a computer security competition. CTF contests are us ...

  6. Educational Codeforces Round 2 A. Extract Numbers 模拟题

    A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  7. ZOJ1111:Poker Hands(模拟题)

    A poker deck contains 52 cards - each card has a suit which is one of clubs, diamonds, hearts, or sp ...

  8. Codeforces Beta Round #7 B. Memory Manager 模拟题

    B. Memory Manager 题目连接: http://www.codeforces.com/contest/7/problem/B Description There is little ti ...

  9. poj 1008:Maya Calendar(模拟题,玛雅日历转换)

    Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64795   Accepted: 19978 D ...

随机推荐

  1. jQuery Ajax异步处理Json数据详解

    先我们来看一个官方的实例使用 AJAX 请求来获得 JSON 数据,并输出结果: $("button").click(function(){ $.getJSON("dem ...

  2. Maven Jrebel 多模块热部署方案

    近期在构建maven多模块项目时,发现web module依赖的其它模块,每次都要clean install成一个jar包,然后运行web module才能加载. 本生jrebel是配置在了web m ...

  3. 在非MFC程序中使用调试宏 ASSERT(),VERIFY()和 TRACE()

    游戏制作已经开始采用C++了,却鲜有人选择使用MFC.但笔者觉得的 ASSERT(),VERIFY()和 TRACE()这几个宏很好用.所以就想自己写一个版本来适应Windows平台下不同的工程类型. ...

  4. Uva 552 Prime Ring Problem(dfs)

    题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...

  5. Eddy's爱好(dfs+容斥)

    Eddy's爱好 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. JS获取中文拼音首字母,并通过拼音首字母高速查找页面内的中文内容

    实现效果: 图一: 图二: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGVzdGNzX2Ru/font/5a6L5L2T/fontsize/400/f ...

  7. HDoj-1527-取石子游戏

    取石子游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  8. c++,类的组合

    1. 在A类中以B类的对象作为成员变量,称为类的组合(composition). 可以先看看这篇文章理解组合的用处: C++中组合的使用 http://blog.csdn.net/jia_xiaoxi ...

  9. 《算法导论》读书笔记之图论算法—Dijkstra 算法求最短路径

    自从打ACM以来也算是用Dijkstra算法来求最短路径了好久,现在就写一篇博客来介绍一下这个算法吧 :) Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的 ...

  10. 限制QLineEdit的数值输入范围(一共4种限制器:QDoubleValidator, QIntValidator, QRegExpValidator, 和QRegularExpressionValidator)

    在使用QLineEdit输入数值时,经常遇到限制其范围的需要,比如角太阳高度角范围为[-90,90],经度值范围[-180,180],方位角范围[0,360].Qt提供了QIntValidator和Q ...