Problem 2216 The Longest Straight
Accept: 31 Submit: 63
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
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)
恩,题目大意就是说,给出一列数,其中0可以变成其他任意数,问这些数中可以形成的连续的序列的最长长度
恩,这一种是康神的思想,暴力左端点二分右端点,其中要一数组记录由1到当前共有多少个空要填
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 100010
using namespace std;
bool vis[maxn];
int rec[maxn];
int bsearch(int l,int r,int v)
{
int left=l;
while(l<=r)
{
int mid=(l+r)>>1;
if(rec[mid]-rec[left]>v)
r=mid-1;
else//有相等的要尽量往右边找
l=mid+1;
}
return r;//跳出循环前一步若是<=而l加则r为准确值l大1 若是>而r减则r仍为准确值l大1
}//所以若返回l后面要为tem-i-1
int main()
{
int t,n,m,a,cnt;
scanf("%d",&t);
while(t--)
{
cnt=0;
scanf("%d%d",&n,&m);
memset(vis,false,sizeof(vis));
for(int i=0;i<n;++i)
{
scanf("%d",&a);
if(!a)
cnt++;
else if(!vis[a])
vis[a]=true;
}
memset(rec,0,sizeof(rec));
for(int i=1;i<=m;++i)
{
if(vis[i])
rec[i]=rec[i-1];
else
rec[i]=rec[i-1]+1;
}//这里在不连续时例如1 2 5 rec[5]==rec[4]=2,而rec[0]==rec[1]=0;
int ans=0;
for(int i=0;i<=m;++i)//所以这里从0开始
{
int tem=bsearch(i,m,cnt);
//printf("i=%d tem=%d\n",i,tem);
ans=max(ans,tem-i-1);//所以这里直接tem-i而不是tem-i-1
}
printf("%d\n",ans);
}
return 0;
}
恩,这个是看了好久大神的带注释的代码才懂的。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100010
using namespace std;
bool vis[maxn];
int rec[maxn];
struct node
{
int num;//记录当前序列的个数
int emp;//记录当前序列中不连续的差值
int sta;//记录当前序列开始元素
};
node edge[maxn];//记录一可连续序列带有一个不连续
int main()
{
int t,n,m,a;
scanf("%d",&t);
while(t--)
{
int cnt=0,k=0;
scanf("%d%d",&n,&m);
memset(vis,false,sizeof(vis));
for(int i=0;i<n;++i)
{
scanf("%d",&a);
if(!a)
cnt++;
else if(!vis[a])
{
vis[a]=true;
rec[k++]=a;
}
}
sort(rec,rec+k);
if(cnt>=m)
{
printf("%d\n",m);
continue;
}
if(k<=1)
{
if(cnt+k>=m)
printf("%d\n",m);
else
printf("%d\n",cnt+k);
continue;
}
int x=0,len=1,ans=cnt,mark=0,tem=cnt;
edge[0].sta=rec[0];
for(int i=1;i<k;++i)
{
if(rec[i]-rec[i-1]!=1)
{
if(rec[i]-rec[i-1]-1<=tem)
{
edge[x].emp=rec[i]-rec[i-1]-1;
edge[x].num=rec[i]-edge[x].sta;
len+=rec[i]-rec[i-1];
tem=tem-(rec[i]-rec[i-1]-1);
x++;
edge[x].sta=rec[i];
}
else if(rec[i]-rec[i-1]-1>cnt)
{
len=1;
tem=cnt;
mark=x;
edge[x].sta=rec[i];
}
else
{
while(rec[i]-rec[i-1]-1>tem)
{
tem+=edge[mark].emp;
len-=edge[mark].num;
mark++;
}
tem=tem-(rec[i]-rec[i-1]-1);
edge[x].emp=rec[i]-rec[i-1]-1;
edge[x].num=rec[i]-edge[x].sta;
len+=rec[i]-rec[i-1];
x++;
edge[x].sta=rec[i];
}
}
else
len++;
ans=max(ans,len+tem);
}
if(ans>m)
printf("%d\n",m);
else
printf("%d\n",ans);
}
return 0;
}