---恢复内容开始---
///难题坐不上,简单题思路错,问曰:何以战?自答曰:20投。
fzu刷起!
![](http://acm.fzu.edu.cn/image/problem.gif)
Accept: 132 Submit: 378
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
Sample Output
Source
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学) 1 ///这里是先把数对应到数组里,这样我们就可以知道哪里有空缺了,然后如果想到刘汝佳的计算前N项和的累加值的办法就会想到计算到这步要的0 2 ///只需在脑子里想一想这个题的模型,很容易想到暴力,因为序列是连续的必属于其中一段,暴力左端点,二分右端点。 3 #include<stdio.h> 4 #include<string.h> 5 int a[110100],b[110100]; 6 int erfen(int l,int r,int zero){ 7 int k=b[l-1]; 8 while(l<r){ 9 int mid=(l+r)>>1; 10 if(b[mid]>k+zero){///如果比要找的值大的话,那我就往下找 11 r=mid; 12 } 13 else{///否则相等或小于我们找下一个,相等找下个的目的是找的不消耗的最长的,这样我们就会找到第一个大于找到的,我们减一, 14 l=mid+1;///不过有的时候会找到末尾没找完,所以我们比较是否大于,如大于减一,这样就全面了 15 } 16 } 17 if(b[l]-k>zero) 18 l--; 19 return l; 20 } 21 int main() 22 { 23 int t,n,m,temp,zero; 24 scanf("%d",&t); 25 while(t--){ 26 zero=0; 27 scanf("%d%d",&n,&m); 28 memset(a,0,sizeof(a)); 29 for(int i=0;i<n;i++){ 30 scanf("%d",&temp); 31 if(temp==0) zero++; 32 else a[temp]=1; 33 } 34 memset(b,0,sizeof(b)); 35 for(int i=1;i<=m;i++){ 36 b[i]=b[i-1]+(!a[i]); 37 } 38 int maxn=0; 39 for(int i=1;i<=m;i++){ 40 if(erfen(i,m,zero)-i+1>maxn) 41 maxn=erfen(i,m,zero)-i+1; 42 } 43 printf("%d ",maxn); 44 } 45 }
---恢复内容结束---