Problem E The Longest Straight

Accept: 71    Submit: 293
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

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int num[100100],flog[100100];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,s,zero=0;
memset(flog,0,sizeof(flog));
memset(num,0,sizeof(num));
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&s);
if(s)
num[s]=1;
else
zero++;
}
flog[0]=0;
for(int i=1;i<=m;i++)
{
if(num[i])
flog[i]=flog[i-1];
else
flog[i]=flog[i-1]+1;
}
int L=-1000000;
for(int i=0;i<=m;i++)
{
int l=i,r=m;
int mid;
while(l<=r)
{
mid=(l+r)/2;
if(flog[mid]-flog[i]>zero)
r=mid-1;
else
l=mid+1;
}
L=max(L,r-i);
}
printf("%d\n",L);
}
return 0;
}

福建省赛--Problem E The Longest Straight(标记+二分)的更多相关文章

  1. FZU 2216——The Longest Straight——————【二分、枚举】

    Problem 2216 The Longest Straight Accept: 17    Submit: 39Time Limit: 1000 mSec    Memory Limit : 32 ...

  2. The Longest Straight(二分,离散化)

     Problem 2216 The Longest Straight Accept: 7    Submit: 14 Time Limit: 1000 mSec    Memory Limit : 3 ...

  3. FZU-2216 The Longest Straight (二分枚举)

    题目大意:给n个0~m之间的数,如果是0,那么0可以变为任意的一个1~m之间的一个数.从中选出若干个数,使构成一个连续的序列.问能构成的最长序列的长度为多少? 题目分析:枚举连续序列的起点,二分枚举二 ...

  4. The Longest Straight(FZUoj2216)

     Problem 2216 The Longest Straight Accept: 82    Submit: 203Time Limit: 1000 mSec    Memory Limit : ...

  5. FZU-2216 The Longest Straight(尺取法)

     Problem 2216 The Longest Straight Accept: 523    Submit: 1663Time Limit: 1000 mSec    Memory Limit ...

  6. FZU 2216 The Longest Straight(最长直道)

    Description 题目描述 ZB is playing a card game where the goal is to make straights. Each card in the dec ...

  7. FZU 2216 The Longest Straight 模拟

    题目链接:The Longest Straight 就是一个模拟就是这样,T_T然而当时恶心的敲了好久,敲完就WA了,竟然有这么简单的方法,真是感动哭了.......xintengziji...zhi ...

  8. 【Floyd最短路】第七届福建省赛 FZU Problem 2271 X

    http://acm.fzu.edu.cn/problem.php?pid=2271 [题意] 给定一个n个点和m条边的无向连通图,问最多可以删去多少条边,使得每两个点之间的距离(最短路长度)不变. ...

  9. (Problem 14)Longest Collatz sequence

    The following iterative sequence is defined for the set of positive integers: n n/2 (n is even) n 3n ...

随机推荐

  1. angular-resource

    上一篇中讲到使用$http同服务器进行通信,但是功能上比较简单,angularjs还提供了另外一个可选的服务$resource,使用它可以非常方便的同支持restful的服务单进行数据交互. 安装 n ...

  2. synchronized与static synchronized 的差别、synchronized在JVM底层的实现原理及Java多线程锁理解

    本Blog分为例如以下部分: 第一部分:synchronized与static synchronized 的差别 第二部分:JVM底层又是怎样实现synchronized的 第三部分:Java多线程锁 ...

  3. 使用excel进行数据挖掘(3)----类别检測

    使用excel进行数据挖掘(3)----类别检測 在配置环境后,能够使用excel进行数据挖掘. 环境配置问题可參阅: http://blog.csdn.net/xinxing__8185/artic ...

  4. HTML5简单进度环插件

    前几天做了一个进度条的插件.今天我用HTML5的arc做一个简单的进度环的插件. 代码演示 事实上非常easy的.相同,我们先用一个实例: 配置js代码 var setting = { id: &qu ...

  5. Navgationcontroller 的pop

    1.NavgationController pop 回来不进入viewdisload,利用原来载入的视图 不是啊,他pop回来的时候不进viewdidload 直接进去viewwillApper这种方 ...

  6. Restful技术

    一.概述 Restful技术是一种架构风格(Representational State Transfer)表现层状态转化,而不是一种编程标准. 之前前后端混在一起,前端通过mapping映射找到后端 ...

  7. hive 运行sqlclient异常

    FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. java.lang.Runtim ...

  8. Non-ASCII character &#39;\xe8&#39; in file xxx.py on line 8, but no encoding declared

    使用网上某个python程序.编译时报错: File "xxx.py", line 8         SyntaxError: Non-ASCII character '\xe8 ...

  9. 2015多校联合训练赛hdu 5301 Buildings 2015 Multi-University Training Contest 2 简单题

    Buildings Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  10. Kali linux 2016.2(Rolling)中的Exploits模块详解

    简单来将,这个Exploits模块,就是针对不同的已知漏洞的利用程序. root@kali:~# msfconsole Unable to handle kernel NULL pointer der ...