2015福建省赛 fzoj The Longest Straight 2216 (二分&转换)好题
2015-12-27 21:09
435 查看
![](http://acm.fzu.edu.cn/image/problem.gif)
Problem 2216 The Longest Straight
Accept: 5 Submit: 12
Time Limit: 1000 mSec Memory Limit : 32768 KB
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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).
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Output
For each test case, output a single integer in a line -- the longest straight ZB can get.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Sample Input
27 11
0 6 5 3 0 10 11
8 1000
100 100 100 101 100 99 97 103
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Sample Output
53
//题意:
现在玩一个游戏(棋牌类的),你有n张牌在1~~m之间取值,0可以表示成任意一张牌(也就是王牌),
现在问你你手里的牌最长有几张牌是连续的。
//思路:
咋一看很迷茫,不知从何下手,但我们可以转换一下,就是将其转换成01串,
就相对简单了,但怎么转换呢,定义一个数组a[],表示对应数,若手中
有这张牌,将对应位置标记为1,否则为0,zero记录输入的0的个数。
然后定义一个数组b[]记录从0-i之间的0的个数。然后用二分查找,
找出从i到m之间的满足0的个数等于zero的值的位置r,那么其连续的长
度即为r-i,然后在比较找出最大的r-i就行了。
具体看代码:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int a[100010]; int b[100010]; int main() { int t,n,m,x; int i,j,k; scanf("%d",&t); while(t--) { memset(a,0,sizeof(a)); int zero=0; scanf("%d%d",&n,&m); for(i=0;i<n;i++) { scanf("%d",&x); if(x) a[x]=1; else zero++; } b[0]=0; for(i=1;i<=m;i++) { if(a[i]) b[i]=b[i-1]; else b[i]=b[i-1]+1; } int mm=0; for(i=0;i<=m;i++) { int l=i,r=m; int mid; while(l<=r) { mid=(l+r)/2; if(b[mid]-b[i]>zero) r=mid-1; else l=mid+1; } mm=max(mm,r-i); } printf("%d\n",mm); } return 0; }
相关文章推荐
- Contains Duplicate II 找出数组中是否有重复元素,长度小于k
- 数值优化(Numerical Optimization)学习系列-非线性约束最优化(Nonlinear Constrained Optimization)
- 福建省赛--Problem E The Longest Straight(标记+二分)
- 福建省赛--Problem E The Longest Straight(标记+二分)
- 数值优化(Numerical Optimization)学习系列-带约束最优化(Constrained Optimization)
- 数值优化(Numerical Optimization)学习系列-大规模无约束最优化(Large-Scale Unconstrained Optimization)
- 浅析pthread_cond_wait
- pthread_cond_signal和pthread_cond_wait简介
- AIDL服务(跨进程访问)
- OC下retain copy nomatic等属性的解析
- 23种设计模式(22)_行为型_责任链模式(Chain of Responsibility Pattern)
- CommunicationsException: Communications link failure
- 【leetcode】217. Contains Duplicate
- vim-airline的theme
- 3+1个大公司开源的人工智能合集
- codeforces C. Sorting Railway Cars 贪心
- 自动化管理工具Saltstack之Grains篇(4)
- 本地socket unix domain socket
- LeetCode --- 70. Climbing Stairs
- 【杭电】[2674]N!Again