Problem 2216 The Longest Straight

Accept: 82    Submit: 203
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

ZB is playing a card game where the goal is to make straights. Each card in the deck has a number between 1 and M(including 1 and M). A straight is a sequence of cards with consecutive values. Values do not wrap around, so 1 does not come after M. In addition to regular cards, the deck also contains jokers. Each joker can be used as any valid number (between 1 and M, including 1 and M).

You will be given N integers card[1] .. card[n] referring to the cards in your hand. Jokers are represented by zeros, and other cards are represented by their values. ZB wants to know the number of cards in the longest straight that can be formed using one or more cards from his hand.

 Input

The first line contains an integer T, meaning the number of the cases.

For each test case:

The first line there are two integers N and M in the first line (1 <= N, M <= 100000), and the second line contains N integers card[i] (0 <= card[i] <= M).

 Output

For each test case, output a single integer in a line -- the longest straight ZB can get.

 Sample Input

2

7 11

0 6 5 3 0 10 11

8 1000

100 100 100 101 100 99 97 103

 Sample Output

5

3

 Source

第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)

思路:尺取法,先将哪些点是否出现过标记,并记录0的个数,然后再前缀数组统计1到M中的未出现的数字,

在用尺取法跑一遍区间取最长就可以了。

#include<stdio.h>#include<algorithm>#include<iostream>#include<stdlib.h>#include<string.h>using namespace std;int aa[100005];int bb[100005];int main(void){    int n,i,j,k,p,q;    scanf("%d",&k);    while(k--)    {        memset(aa,0,sizeof(aa));        memset(bb,0,sizeof(bb));        scanf("%d %d",&p,&q);int ma=0;        for(i=0; i<p; i++)        {            scanf("%d",&n);            aa[n]++;            if(ma<n)            {                ma=n;            }        }        int cnt=aa[0];        bb[0]=0;ma=min(ma+cnt,q);        for(i=1; i<=q; i++)        {            if(!aa[i])            {                bb[i]=bb[i-1]+1;            }            else            {                bb[i]=bb[i-1];            }        }        int ll=1;        int rr=1;        int maxx=0;        while(rr<=ma&&ll<=rr+1)        {            while(bb[rr]-bb[ll-1]<=cnt&&rr<=ma)            {                rr++;            }            maxx=max(maxx,(rr-ll));            ll++;        }        printf("%d\n",maxx);    }    return 0;}

}

最新文章

  1. jquery.cookie() 的使用(原)
  2. Car---hdu5935(简单题)
  3. PHP 全局变量 $_SERVER
  4. 开发完iOS应用,接下去你该做的事
  5. IntelliJ IDEA:Getting Started with Spring MVC, Hibernate and JSON实践
  6. 如何写计算机会议的rebuttal
  7. 转: sqlserver常用sql语句,更改字段,建立唯一键,多个字段去重复等
  8. 一个页面,多个flash(刚学jq插件)
  9. Eclipse3.6 添加JUnit源代码
  10. gridView 编辑单元格获取单元格焦点位置(位于单元格的焦点位置)
  11. C++异常处理基本思想
  12. Pat1128:N Queens Puzzle
  13. [开发技巧]&#183;Numpy广播机制的深入理解与应用
  14. 01.pandas
  15. git GUI设置长期记住密码
  16. 笔记:mysql升序排列asc,降序排列desc
  17. c++并行计算库TBB和PPL的基本用法
  18. e.stopPropagation()
  19. js md5类(支持中文)
  20. @componentscan注解的用法和作用

热门文章

  1. 31-Longest Common Prefix
  2. cmd到指定目录并执行命令 mysql到bin目录并执行命令 cmd bat进入指定文件夹中并执行命令
  3. 学习java的第五天
  4. oracle中的控制语句
  5. 常见排序——Java实现
  6. springboot 设置项目路劲后不能访问首页
  7. 1888-jerry99的数列--factorial
  8. 淘宝网购物车jquery源码和网易新用户注册页面表单验证的练习
  9. 查看MySQL正在执行的线程
  10. JuiceFS 缓存策略详解