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. C# 实现磁性窗体

    可以实现窗体的 吸附 移动 分离     using System; using System.Drawing; using System.Collections.Generic; using Sys ...

  2. wp8.1开发系列之安装包URI方案

    应用文件使用"ms-appdata:///"开头的URI地址,安装包使用的是"ms-appx:///"开头的URI地址. 比如:读取安装包Image文件夹下的t ...

  3. linux杂记(十二?) 关于账号和密码的二三事

    关于密码的二三事 关于账号和密码的二三事 久了不更linux的相关知识,实在是懒得想内容点(纯粹是懒).那么今天就来谈谈关于linux密码和账号的重要概念. 假如你的主机遭到入侵,那么对方的第一个侵入 ...

  4. EditText 双击才能获取点击事件

    在获取EditText点击事件的过程中,发现EditText setOnClickListener事件响应中,只有获取焦点的时候才会响应, 如当焦点在别的控件上时,只能先点击获取焦点,第二次点击才会响 ...

  5. poj 3469 Dual Core CPU 最小割

    题目链接 好裸的题....... 两个cpu分别作为源点和汇点, 每个cpu向元件连边, 权值为题目所给的两个值, 如果两个元件之间有关系, 就在这两个元件之间连边, 权值为消耗,这里的边应该是双向边 ...

  6. 脑波设备mindwave TGCD接口开发示例

    对于TGCD的开发,神念科技提供的文件包括,头文件thinkgear.h,thinkgear.lib,thinkgear.dll,有这三个文件,在win32下开发就不是什么难事了吧 如果是java语言 ...

  7. hbase0.96 put流程 源码分析

    无意间多瞄了一眼hbase0.98的代码,想复习下put流程.发现htable里面已经找不到processBatchOfPuts()奇怪了.看了半天原来变化还真大事实上0.96就没这个了,于是又搞了个 ...

  8. 编译器DIY——词法分析

    在上一篇文章中已经介绍了读文件的操作,那么这一篇文章中将会细致解释词法分析. 在源文件里解析出的单词流必须识别为保留字,标识符,常量,操作符和界符五大类 1.显然我们须要列举出全部的保留字,而这里与保 ...

  9. web浏览器中的javascript 1

    Html 文档嵌入客户端有4种方式. 1. 内联.放置在<script>和</script>标签对之间. 2.放置在<script>标签的src属性指定的外部文件中 ...

  10. Asp.Net MVC4.0 官方教程 入门指南之五--控制器访问模型数据

    Asp.Net MVC4.0 官方教程 入门指南之五--控制器访问模型数据 在这一节中,你将新创建一个新的 MoviesController类,并编写代码,实现获取影片数据和使用视图模板在浏览器中展现 ...