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
Sample Output
题解:
让求最长连续上升的序列长度,可任意摆放次序;其中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(二分,离散化)的更多相关文章
- FZU 2216 The Longest Straight 二分
0可以表示任何1到m的数,求一个最长的连续上升序列长度 因为m的范围在10w,所以以每个节点为起点 进行二分,复杂度mlogm 思路:b[i]表示到 1 到 i 有几个数没有出现,二分的时候注意加等号 ...
- FZU 2216——The Longest Straight——————【二分、枚举】
Problem 2216 The Longest Straight Accept: 17 Submit: 39Time Limit: 1000 mSec Memory Limit : 32 ...
- 福建省赛--Problem E The Longest Straight(标记+二分)
Problem E The Longest Straight Accept: 71 Submit: 293 Time Limit: 1000 mSec Memory Limit : 327 ...
- 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 ...
- 计蒜客 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 ...
- 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之间的一个数.从中选出若干个数,使构成一个连续的序列.问能构成的最长序列的长度为多少? 题目分析:枚举连续序列的起点,二分枚举二 ...
随机推荐
- ViEmu for VS2013-3.2.1 破解(转)
ViEmuVS2013-3.2.1 破解 VS升级到2013后,作为一个Vimer,自然需要更新最新的ViEmu插件,因为现在离了Vim,写代码已经寸步难行了. ViEmu 3.2.1的破解其实和 ...
- c++实现单例
单例宏: //单件定义宏 //------------------------------------- // 在头文件中申明 // DECLARE_SINGLEOBJ( CSampleClass ) ...
- 提供一段Excel获取Title的标题,类似于A、AA、AAA,我们操作Excel的时候通常根据次标题来获取一定的操作范围。
/******************************************** FormatExcelColumTitle Purpose Get excel title like &qu ...
- linux杂记(四)热键[Tab],[ctrl]-c,[ctrl]-d,在线求助man page/info page
[Tab]按键 他具有[命令补全](接在一串指令的第一个字后面)与[档案补齐](接在第一串指令的第二字以后时)的功能.如 [KANO@kelvin ~]$ ca[tab][tab] cabextrac ...
- mongodb数据库调试问题:‘db object already connecting, open cannot be called multiple times’
在微博小系统的调试过程中: (1)登入登出可以正常显示,就是在注册的时候网络连接突然停止,但是用户名和密码已经存入数据库中,报错为:undefined is not a function 错误主要指向 ...
- 美化 input type=file控件
大家都知道input的type=file控件默认样式是不能修改的 可以通过一个小技巧处理下 html: <a href="javascript:;" class=" ...
- poj 2369 Permutations 置换
题目链接 给一个数列, 求这个数列置换成1, 2, 3....n需要多少次. 就是里面所有小的置换的长度的lcm. #include <iostream> #include <vec ...
- 0622 python 基础05
使用双重for循环,打印 0~100 # -*- coding: utf-8 -*- # D:\python\test.py def printOneToHundred(): for i in ...
- junit的安装和使用
一.junit的安装: junit-4.11.jar: http://www.java2s.com/Code/Jar/j/Downloadjunit411jar.htm hamcrest-core.j ...
- Struts 和Spring的核心控制器
Struts 核心控制器是FilterDispatch Spring核心控制器是DispatchServlet