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
*/

  

FZU 2216——The Longest Straight——————【二分、枚举】的更多相关文章

  1. FZU 2216 The Longest Straight 二分

    0可以表示任何1到m的数,求一个最长的连续上升序列长度 因为m的范围在10w,所以以每个节点为起点 进行二分,复杂度mlogm 思路:b[i]表示到 1 到 i 有几个数没有出现,二分的时候注意加等号 ...

  2. FZU 2216 The Longest Straight(最长直道)

    Description 题目描述 ZB is playing a card game where the goal is to make straights. Each card in the dec ...

  3. FZU 2216 The Longest Straight 模拟

    题目链接:The Longest Straight 就是一个模拟就是这样,T_T然而当时恶心的敲了好久,敲完就WA了,竟然有这么简单的方法,真是感动哭了.......xintengziji...zhi ...

  4. The Longest Straight(二分,离散化)

     Problem 2216 The Longest Straight Accept: 7    Submit: 14 Time Limit: 1000 mSec    Memory Limit : 3 ...

  5. FZU-2216 The Longest Straight(尺取法)

     Problem 2216 The Longest Straight Accept: 523    Submit: 1663Time Limit: 1000 mSec    Memory Limit ...

  6. The Longest Straight(FZUoj2216)

     Problem 2216 The Longest Straight Accept: 82    Submit: 203Time Limit: 1000 mSec    Memory Limit : ...

  7. FZU-2216 The Longest Straight (二分枚举)

    题目大意:给n个0~m之间的数,如果是0,那么0可以变为任意的一个1~m之间的一个数.从中选出若干个数,使构成一个连续的序列.问能构成的最长序列的长度为多少? 题目分析:枚举连续序列的起点,二分枚举二 ...

  8. 福建省赛--Problem E The Longest Straight(标记+二分)

    Problem E The Longest Straight Accept: 71    Submit: 293 Time Limit: 1000 mSec    Memory Limit : 327 ...

  9. uva 12587 二分枚举

    思路:维护一个森林,二分枚举最小的最大值. #include<set> #include<map> #include<cmath> #include<queu ...

随机推荐

  1. Xamarin.Forms之UserDialogs 重制版本

    在 forms 里面,目前使用比较多的弹出组件是 Acr.UserDialogs ,但是这个组件有些小问题,比如 loading .hide 会同时把 toast 给一起关掉,android 下的 t ...

  2. 在构造函数和析构函数中调用虚函数------新标准c++程序设计

    在构造函数和析构函数中调用虚函数不是多态,因为编译时即可确定调用的是哪个函数.如果本类有该函数,调用的就是本类的函数:如果本类没有,调用的就是直接基类的函数:如果基类没有,调用的就是间接基类的函数,以 ...

  3. 使用C/C++代码编写Python模块

    假如我们要用C语言实现下面的python脚本bird.py import os def fly(name): print(name + " is flying.\n") 调用脚本m ...

  4. Kotlin 在kotlin内使用Java的一些注意(长篇)

    首先呢,大部分的java在kotlin内是可以使用的. 但是有些java的关键字和kotlin的一样,而意义不一样就需要转义.(单引号括起来的)这一点需要注意. 这是一个长篇 我会不断更新.毕竟我也在 ...

  5. Codeforces Round #545 (Div. 2)D(KMP,最长公共前后缀,贪心)

    #include<bits/stdc++.h>using namespace std;const int N=1000007;char s1[N],s2[N];int len1,len2; ...

  6. SignalR-001

    SignalR 是什么? ASP.NET Core SignalR 是一个开放源代码库,它简化了向应用添加实时 web 功能. 实时 web 功能立即使服务器端代码能够将内容推送到客户端. 一.有这么 ...

  7. flink学习笔记-split & select(拆分流)

    说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...

  8. JDBC_ResultSet结果集用法_游标原理_关闭连接问题

    依次关闭使用对象及连接 ResultSet---Statement--Connectionimport java.sql.Connection;import java.sql.DriverManage ...

  9. 启动Tomcat报错

    如果发现引入jar包有问题时,看jar包是否损坏,变成了0kb.如果是这样,在网上试尽解决办法也是有问题的. 一般Tomcat启动报错,要引入这两个jar包.

  10. C#实现,一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第35位数是多少, 用递归算法实现

    方法函数可以通过调用自身来进行递归.计算理论可以证明递归的作用可以完全取代循环. static void Main(string[] args) { Console.WriteLine(Ra()); ...