FZUoj 题目2216 The Longest Straight*(二分)
2015-12-29 20:35
483 查看
Problem 2216 The Longest Straight
Accept: 22 Submit: 44
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.
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.
27 110 6 5 3 0 10 118 1000100 100 100 101 100 99 97 103
53
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)
题目大意:一个n,m,后边n个数,0的个数代表鬼牌,龟牌什么都可以当,问选出的最大的连续的值得长度是多少
思路:暴力左端点二分右端点,,
ac代码
Accept: 22 Submit: 44
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
53
Source
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)题目大意:一个n,m,后边n个数,0的个数代表鬼牌,龟牌什么都可以当,问选出的最大的连续的值得长度是多少
思路:暴力左端点二分右端点,,
ac代码
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<iostream> #include<math.h> #include<algorithm> #define LL long long #define INF 0x3f3f3f3f using namespace std; int vis[100010],num[100010]; void init(int m) { int i; for(i=0;i<=m;i++) vis[i]=0; } int bseach_l(int l,int r,int val) { int ans=r; int tl=l; while(l<=r) { int mid=(l+r)>>1; if(num[mid]-num[tl]>val) { // ans=mid; //l=mid+1; r=mid-1; } else l=mid+1; } return r; } int main() { int t; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); int i; int cnt=0; memset(vis,0,sizeof(vis)); for(i=0;i<n;i++) { int x; scanf("%d",&x); if(x==0) { cnt++; continue; } vis[x]=1; } int ans=0,tc=cnt,pt; memset(num,0,sizeof(num)); for(i=1;i<=m;i++) { if(vis[i]) num[i]=num[i-1]; else num[i]=num[i-1]+1; } for(i=0;i<=m;i++) { int temp=bseach_l(i,m,cnt); ans=max(temp-i,ans); } printf("%d\n",ans); } }
相关文章推荐
- 多线程的使用(1)-performSelectorOnMainThread
- Canvas与Paint的初级使用
- PKU1273Drainage Ditches
- 思杰成为ADC领域在Container容器中提供其产品的第一家主要厂商
- 浅谈RAID写惩罚(Write Penalty)与IOPS计算
- 关于Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.的问题
- 后台数据校验
- assign与retain
- @property (nonatomic, retain)
- time_wait详解
- RAID的各级特性及软RAID的创建
- 怎么解决啊???"Thread-14" HibernateException: Could not obtain transaction-.....
- ibm aix I/O ERROR DETECTED BY LVM
- 自定义View时,用到Paint Canvas的一些温故,讲讲平时一些效果是怎么画的(基础篇 二,图像遮盖,Canvas静态变化)
- 自定义View时,用到Paint Canvas的一些温故,讲讲平时一些效果是怎么画的(基础篇 二,图像遮盖,Canvas静态变化)
- Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
- EC纠删码(Erasure Code)校验码算法相比传统RAID5有哪些优势?
- 关于工作,学习中定时备份的几个方法(cron,git,mail)
- 打造终端下mutt收发邮件环境(fbterm,fetchmail,msmtp,procmail,mutt)
- 3D空间中的AABB(轴向平行包围盒, Aixe align bounding box)的求法