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 ...
随机推荐
- FileTracker:error FTK1011编译错误的原因和解决办法
原因: 今天创建好项目名字完成关了VS,后感觉文件夹名字不太对改了一下,后来程序就调试不了出现FileTracker:error FTK1011编译错误0.0,经过网络查询应该是路径问题 解决方法: ...
- 利用find同时查找多种类型文件
find . -name "*.c" -o -name "*.cpp" -o -name "*.h" 就可以列出当前目录下面所有的c,cpp ...
- [Algorithm]图
一.图的算法 邻接矩阵表示的数据结构 1 #define INFINITY INT_MAX // 无穷大 2 #define MAX_VERTEX_NUM 20 // 限制顶点最大数值为20个 3 # ...
- selenium面试题
selenium中如何判断元素是否存在? selenium中没有提供原生的方法判断元素是否存在,一般我们可以通过定位元素+异常捕获的方式判断. # 判断元素是否存在 try: dr.find_elem ...
- 题解 CF500D 【New Year Santa Network】
题目链接 这道题首先是要看看该如何化简,先把三元组化成二元组. 之后统计经过某条边的 次数$*$权值 的和. 最后除以总基数 $tot$ 其中,每条边被计算的次数为 子树的点数$*$非子树的点数 ( ...
- linux系统安全及应用——系统引导和登录控制
一.开关机安全控制 1)调整BIOS将第一引导设备设为当前系统所在硬盘 2)调整BIOS禁止从其他设备(光盘.U盘.网络)引导系统 3)调整BIOS将安全级别设为setup,并设置管理员密码 4)禁用 ...
- 关于logging的那些坑
python的logging日志记录模块非常强大,使用也很简单,但是特别容易出各种意外状况,打印各种出乎意料的log.最近对logging的一些原理进行了学习,再此做个记录,以备忘. 首先全面的了解一 ...
- 如果将markdown视作一门编程语言可以做哪些有趣的事情?
如题,然后就有了为解决这个好奇而开的项目:https://github.com/racaljk/llmd 源码主要是parser+interpreter,其中parser使用sundown,然后生成l ...
- 对于自我管理 ObjectContextManager的测试
书接上文, 把代码改为多线程, public class Threads { public static void allStart() { for (int i = 0; i < 10; ...
- BZOJ3065 带插入区间K小值 || 洛谷P4278
这是一道让我崩溃的题...... 然鹅洛谷上时限被改然后只有20分......好像所有人都被卡了(雾) 由于替罪羊树不是依靠旋转操作而是依靠暴力重构的方式维护树的平衡,所以我们可以考虑使用替罪羊树套区 ...