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
【experience】:题意真爽!
先是看到样例以为是最长递增子序列,交了5发WA。
然后曲解题意,以为是找连续的子串,没调出来。
经队友提示,才真正明白这道题的意思是干啥!md...
【分析】:
题意是从手中的卡片中排出一个最长的连续序列,与输入的顺序无关!!0可以代表任意任意数字。
用数组c标记哪个数字存在,同时记下0的数量。
设两个下标,i和last,扫一遍,i在前,last在后。过程中保证last~i之间的空位可以用0补全。
【代码】:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int c[101010];
int n,m,x;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(c,0,sizeof(c));
int r0=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
if(x==0)r0++;
c[x]=1;
}
int ans=0;
for(int i=1,c0=0,last=0;i<=m;i++)
{
if(!c[i])c0++;
while(c0>r0)
{
last++;
if(!c[last])c0--;
}
ans=max(ans,i-last);
}
cout<<ans<<endl;
}
}