Problem 2216 The Longest Straight

Accept: 17    Submit: 39
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

2
7 11
0 6 5 3 0 10 11
8 1000
100 100 100 101 100 99 97 103

 Sample Output

5
3

 Source

第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)

 
 
题目大意:Zb在玩卡牌游戏,给你n张牌,一个m,每个牌面是在1~m之间的一个数,包括1和m。但是可能会有王牌,王牌可以变成1~m中的任意一个,这里王牌用0表示。问你最长的连续上升的牌的长度。

解题思路:首先明确,这几个0是应该连续填放在空里,这样能连成最长的。我们首先记录哪些牌是有的,哪些没有。然后我们枚举左端点,然后通过二分去查找需要0的个数小于总的0的个数的最长的右端点。 mlogm的复杂度。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 1e6+200;
int card[maxn],presum[maxn];
int BinSearch(int l,int r, int k,int key){
int mi = (l+r)/2;
while(l < r){
mi = (l+r)/2;
if(presum[mi]-k > key){
r = mi;
}else{
l = mi+1;
}
}
if(presum[l]-k > key)
l--;
return l;
}
int main(){
int T,n,m;
scanf("%d",&T);
while(T--){
int a, Zero = 0;
scanf("%d%d",&n,&m);
memset(card,0,sizeof(card));
for(int i = 1; i <= n; i++){
scanf("%d",&a);
card[a] = 1;
if(!a) Zero++;
}
for(int i = 1; i <= m; i++){
presum[i] = presum[i-1] + (!card[i]);
}
int ans = 1;
for(int i = 1; i <= m; i++){
int idx = BinSearch(i,m,presum[i-1],Zero);
// printf("%d ",idx);
ans = max(ans, idx-i+1);
}
printf("%d\n",ans);
}
return 0;
} /* 55
11 15
0 0 1 2 4 5 8 9 11 13 14
*/

  

最新文章

  1. HDU 1394 Minimum Inversion Number
  2. POJ 2140
  3. 关于VS2013连接远程数据库服务器的蛋疼问题
  4. AndroidAsyncHttp 临时修复 JsonHttpResponseHandler 避免死循环
  5. (一)Redis在windows下的安装和使用
  6. 【 js 基础 】【 源码学习 】backbone 源码阅读(三)浅谈 REST 和 CRUD
  7. 前端MVC Vue2学习总结(七)——ES6与Module模块化、Vue-cli脚手架搭建、开发、发布项目与综合示例
  8. 01-JDK环境配置
  9. php签名认证
  10. C#批量向数据库插入数据
  11. Spring Boot属性配置文件详解
  12. 从零开始学Kotlin-基础语法(1)
  13. 文件通过svn updata更新不到,并且svn st显示被删除的解决办法
  14. 【CF316G3】Good Substrings 后缀自动机
  15. [转]ASP.NET Core配置环境变量和启动设置
  16. ReactJS表单handleChange
  17. Flume的介绍和简单操作
  18. C++ 函数的扩展③--函数重载
  19. 创建Azure scheduler完成日常任务
  20. 《梦断代码Dreaming In Code》阅读计划

热门文章

  1. [转载] C++ namespaces 使用
  2. C# 抽象(2)
  3. 201621123012 《java程序设计》第4周学习总结
  4. 钩子(hook)编程
  5. 基于 Token 的身份验证:JSON Web Token(JWT)
  6. cf555e
  7. 洛谷P4518 [JSOI2018]绝地反击(计算几何+二分图+退流)
  8. delphi 给字符指针分配内存
  9. Azure KUDU工具
  10. TX2 上使用opencv 调用板载mipi摄像头