福建省赛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 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 110 6 5 3 0 10 118 1000100 100 100 101 100 99 97 103
Sample Output
53Cached 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; }
相关文章推荐
- STM32开关总中断(转贴)
- java异常处理
- java NIO教程(七)--FileChannel
- sha-1 加密
- java NIO教程(六)--Selector
- 计算机学院大学生程序设计竞赛(2015’11)1005 ACM组队安排
- Android 开源项目EventBus详解
- 关于封装了几个NSUserDefault方法
- 【IOS学习笔记】为UICollectionView设置自适应屏幕宽度以及点击效果
- 23种设计模式(7)_结构型_桥接模式(Bridge Pattern)
- JS第一节表格
- iOS --- 多线程之GCD
- 浅析pthread_cond_wait
- SQL Server 事务、异常和游标
- 计算机学院大学生程序设计竞赛(2015’11)1004 质方数
- LINUX启动运行的脚本
- java NIO教程(五)--通道之间的数据传输
- 封装了AFNetWorking
- 仿淘宝商品详情-点击显示大图,可滑动
- [ahk]批量定义热键