The Longest Straight
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.
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).
For each test case, output a single integer in a line -- the longest straight ZB can get.
2 7 11 0 6 5 3 0 10 11 8 1000 100 100 100 101 100 99 97 103
5 3
确实是个好题啊,一开始读错题意了,简直就是fuck,dp了两个小时,题意说就是给你一把牌,最大的牌为m,0代表王牌,可以使任何一个数字,这些牌随便排列,然后让你找出最长的连续的,直接枚举就可以,拿不同的起点
My ugly code
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int maxn=100000+10;
int t;
int n,m;
int a[maxn];
int b[maxn];
int main(){
scanf("%d",&t);
while(t--){
memset(a,0,sizeof(a));
scanf("%d%d",&n,&m);
int numZero=0;
for(int i=1;i<=n;i++){
int tmp;
scanf("%d",&tmp);
if(!tmp) numZero++;
else a[tmp]=1;
}
b[0]=0;
for(int i=1;i<=m;i++){
if(!a[i])
b[i]=b[i-1]+1;
else
b[i]=b[i-1];
}
int mm=0;
for(int i=0;i<=m;i++){
int l=i,r=m;
int mid;
while(l <= r){
mid=(l+r)/2;
if(b[mid]-b[i] > numZero)
r=mid-1;
else
l=mid+1;
}
mm=max(mm,r-i);
}
printf("%d\n",mm);
}
return 0;
}