您的位置:首页 > 其它

福建省赛E

2015-12-27 18:31 459 查看

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
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

27 110 6 5 3 0 10 118 1000100 100 100 101 100 99 97 103



Sample Output

53

Cached at 2015-12-27 18:27:38.

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int num[1000010];
bool vis[100010];
struct Node{
int numm,temp,posi;
}A[100010];
int main()
{
int t,n,m,i,j,k,a;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
int ans=0,k=0;
memset(vis,false,sizeof(vis));
for(i=0;i<n;++i){
scanf("%d",&a);
if(a==0){
ans++;continue;
}
else if(!vis[a]){
num[k++]=a;vis[a]=true;
}
}
if(ans==n){
printf("%d\n",m);
continue;
}
if(k==1){
maxn=ans+1;
if(maxn>m)maxn=m;
printf("%d\n",maxn);
continue;
}
sort(num,num+k);
int maxn=ans,temp=1,x=0,jok=ans,posm=0;
A[0].posi=num[0];
for(i=1;i<k;++i){
if(num[i]-num[i-1]!=1){
if(num[i]-num[i-1]-1<=jok){
jok=jok-(num[i]-num[i-1]-1);
A[x].numm=num[i]-A[x].posi;
A[x].temp=num[i]-num[i-1]-1;
x++;
A[x].posi=num[i];
temp+=num[i]-num[i-1];
}
else {
if(num[i]-num[i-1]-1>ans){
temp=1;jok=ans;
posm=x;A[x].posi=num[i];
}
else{
while(num[i]-num[i-1]-1>jok){
temp-=A[posm].numm;jok+=A[posm].temp;
posm++;
}
jok=jok-(num[i]-num[i-1]-1);
A[x].numm=num[i]-A[x].posi;
A[x].temp=num[i]-num[i-1]-1;
x++;
A[x].posi=num[i];
temp+=num[i]-num[i-1];
}
}
}
else {
temp++;
}
maxn=max(maxn,temp+jok);
}
if(maxn>m)maxn=m;
printf("%d\n",maxn);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  福建省赛E