Problem 2216 The Longest Straight

Accept: 7    Submit: 14 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

题解:

让求最长连续上升的序列长度,可任意摆放次序;其中0可以代表1~M之间的任意一个数;

离散化数组,把里面元素从0开始,出现过就是前一个值,没出现过就是前一个+1;计数0出现的次数n0;然后0~M对于每一个数进行二分查找,找出差值为n0的最大序列长度即可;

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define T_T while(T--)
const int MAXN=100010;
int a[MAXN],b[MAXN];
int erfen(int l,int r,int i,int x){
int mid;
while(l<=r){
mid=(l+r)>>1;
if(b[mid]-b[i]>x)r=mid-1;
else l=mid+1;
}
return l-i-1;
}
int main(){
int T,N,M,x;
SI(T);
T_T{
SI(N);SI(M);
mem(a,0);
int n0=0;
for(int i=0;i<N;i++){
SI(x);
if(x==0)n0++;
else a[x]=1;
}
b[0]=0;
for(int i=1;i<=M;i++){
if(a[i])
b[i]=b[i-1];
else b[i]=b[i-1]+1;
}
int ans=0;
for(int i=0;i<=M;i++){
ans=max(ans,erfen(i,M,i,n0));
}
printf("%d\n",ans);
}
return 0;
}

  

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——————【二分、枚举】

    Problem 2216 The Longest Straight Accept: 17    Submit: 39Time Limit: 1000 mSec    Memory Limit : 32 ...

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

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

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

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

  5. FZU 2216 The Longest Straight 模拟

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

  6. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

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

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

  8. The Longest Straight(FZUoj2216)

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

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

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

随机推荐

  1. MVC上传文件受限制

    mvc自带设置4M一下的字体可以上传,4M以上的字体需要对web.config进行设置 <system.web> <httpRuntime targetFramework=" ...

  2. jQuery实时获取checkbox状态问题

    在最近的项目开发中,使用jQuery操作checkbox时,发现一个问题. Html代码如下: <body> <div> <inputtype="checkbo ...

  3. 5.7.2.4 random() 方法

    Math.random()方法返回大于等于0小于1的一个随机数.对于某些站点来说,这个方法非常实用,因为可以利用它来随机显示一些名人名言和新闻事件.套用下面的公式,就可以利用Math.random() ...

  4. CSS中的特殊性、继承、层叠

    前言 最近在看<CSS权威指南>,书中第三章“结构和层叠”对特殊性的解释十分到位,今天就来整理下思路,记录如下. 初来乍到,有何不妥请多多指点,有时间的话顺便评论下,讨论讨论~ 引入问题

  5. Multiwii 代码解读

    GPS部分 GPS_angle[ROLL]   = (nav[LON]*cos_yaw_x - nav[LAT]*sin_yaw_y) /10;GPS_angle[PITCH]  = (nav[LON ...

  6. MySQL zerofill 的用法

    creata table t(x int(6) zerofill,y int); insert into t(x,y) values(1,1); select x,y from t;

  7. sqlserver、mysql、oracle各自的默认端口号

    sqlserver默认端口号为:1433 URL:"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dbname" D ...

  8. mysql常见错误码

    1062 - Duplicate entry '1' for key 1 唯一性错误

  9. 阿里云ECS每天一件事D5:安装php5.4.34

    原本是想把php和nginx合在一起来说的,不过考虑后,还是分开来做吧,已熟悉的更透彻一些. 1.准备类库 yum install autoconf automake libtool re2c fle ...

  10. 用WebBrowser实现HTML界面的应用和交互 good

    这一篇将继续讨论在使用delphi进行普通应用程序开发的时候,WebBrowser的具体应用,主要是针对使用其进行HTML界面开发的,这也是一篇我在网上找到的资料,大家如要转载,请尊重原作者的知识产权 ...