Fzuoj 2216 The Longest Straight 【二分 || 模拟】
2016-01-04 10:30
507 查看
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 additionto 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 11
0 6 5 3 0 10 11
8 1000
100 100 100 101 100 99 97 103
Sample Output
53
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; }
相关文章推荐
- Saiku去掉登录模块
- Facebook人工智能负责人Yann LeCun谈深度学习的局限性
- QDU65 again and again(字典序暴力or字典树)
- 151219DuoTaiDemo
- 趣说游戏AI开发:对状态机的褒扬和批判
- [Android Pro] 网络流量安全测试工具Nogotofail
- VT-x is not available (VERR_VMX_NO_VMX) 的解决方案
- OC对象的retainCount(引用计数器)
- VMfailVaild 错误号
- 【science封面文章】Human-level concept learning through probabilistic program induction
- LeetCode之Climbing Stairs与斐波那契数列的联想
- KMP字符串模式匹配详解http://blog.csdn.net/lin_bei/article/details/1252686
- Develop--Training(六)Getting Started--Working with System Permissions
- POJ 2135 Farm Tour && HDU 2686 Matrix && HDU 3376 Matrix Again 费用流求来回最短路
- 彻底解决INSTALL_FAILED_UPDATE_INCOMPATIBLE的安装错误
- Installation error: INSTALL_FAILED_NO_MATCHING_ABIS
- RAID原理以及分类
- org.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs)
- 理解cookie的path和domain属性
- 被误传的Cookie的Domain属性