FZU 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
Sample Output
Source
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)
解题思路:首先明确,这几个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
*/
FZU 2216——The Longest Straight——————【二分、枚举】的更多相关文章
- FZU 2216 The Longest Straight 二分
0可以表示任何1到m的数,求一个最长的连续上升序列长度 因为m的范围在10w,所以以每个节点为起点 进行二分,复杂度mlogm 思路:b[i]表示到 1 到 i 有几个数没有出现,二分的时候注意加等号 ...
- FZU 2216 The Longest Straight(最长直道)
Description 题目描述 ZB is playing a card game where the goal is to make straights. Each card in the dec ...
- FZU 2216 The Longest Straight 模拟
题目链接:The Longest Straight 就是一个模拟就是这样,T_T然而当时恶心的敲了好久,敲完就WA了,竟然有这么简单的方法,真是感动哭了.......xintengziji...zhi ...
- The Longest Straight(二分,离散化)
Problem 2216 The Longest Straight Accept: 7 Submit: 14 Time Limit: 1000 mSec Memory Limit : 3 ...
- FZU-2216 The Longest Straight(尺取法)
Problem 2216 The Longest Straight Accept: 523 Submit: 1663Time Limit: 1000 mSec Memory Limit ...
- The Longest Straight(FZUoj2216)
Problem 2216 The Longest Straight Accept: 82 Submit: 203Time Limit: 1000 mSec Memory Limit : ...
- FZU-2216 The Longest Straight (二分枚举)
题目大意:给n个0~m之间的数,如果是0,那么0可以变为任意的一个1~m之间的一个数.从中选出若干个数,使构成一个连续的序列.问能构成的最长序列的长度为多少? 题目分析:枚举连续序列的起点,二分枚举二 ...
- 福建省赛--Problem E The Longest Straight(标记+二分)
Problem E The Longest Straight Accept: 71 Submit: 293 Time Limit: 1000 mSec Memory Limit : 327 ...
- uva 12587 二分枚举
思路:维护一个森林,二分枚举最小的最大值. #include<set> #include<map> #include<cmath> #include<queu ...
随机推荐
- RequireJS 主入口加载模块经常会加载失败的问题
在接入requirejs时,在main入口遇到了这样的问题,使用jquery,刷新10次页面会有3-4次加载失败,找不到$符号等等 require.config({ 'baseUrl': './mod ...
- 【SQL】- 基础知识梳理(五) - 触发器
触发器的概念 触发器对表进行插入.更新.删除的时候会自动执行的特殊存储过程 触发器的语法 create trigger tgr_name on table_name with encrypion –加 ...
- I-team 博客全文检索 Elasticsearch 实战
一直觉得博客缺点东西,最近还是发现了,当博客慢慢多起来的时候想要找一篇之前写的博客很是麻烦,于是作为后端开发的楼主觉得自己动手丰衣足食,也就有了这次博客全文检索功能Elasticsearch实战,这里 ...
- 20165219 《Java程序设计》实验三(敏捷开发与XP实践)实验报告
20165219 <Java程序设计>实验三(敏捷开发与XP实践)实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:王彦博 学号:20165219 成绩: 指导教 ...
- 第二篇:git创建流程
1.创建组织 2.创建 3.点击项目 创建完: 4.选择管理——>选择公钥——>添加个人公钥: 5.怎样生成公钥 5.1.如何生成ssh公钥 你可以按如下命令来生成 sshkey: ssh ...
- f(!gotop.length) return false;
if (!$("p").length) return; if语句接收一个布尔值,如果布尔值为true则执行接下来的语句,布尔值为false则执行else关键词后的语句. JavaS ...
- VS2010设置默认浏览器
1.选择项目中起始页右键,然后选择浏览方式 2.然后就可以设置默认浏览器了
- spark执行例子eclipse maven打包jar
首先在eclipse Java EE中新建一个Maven project具体选项如下 点击Finish创建成功,接下来把默认的jdk1.5改成jdk1.8 然后编辑pom.xml加入spark-cor ...
- DOS远程桌面连接命令[佚名]
DOS远程桌面连接命令 mstsc /v: 192.168.1.250 /console cmd 运行 command 删除文件 rd 文件名/S 创建文件 MD 文件名 net user admin ...
- unix网络编程源码编译问题
##获得更多资料欢迎进入我的网站或者 csdn或者博客园 今天在温习计算机网络时,突然将我很久前买的<unix网络编程第三版卷一>拿出来看看了.放了两年了,哈哈.主要讲的套接字,之前编写 ...