FZU 2216 The Longest Straight(最长直道)
|
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. |
ZB正在玩一个纸牌游戏叫做造直道。这幅卡组中的每张卡都有一个1到M间的数字(包括1与M)。一条直道是连续的卡牌序列。值不能循环,因此1不能在M后面。注意除了基本牌外,卡组里还有大小王。每张王可以当成任意一个有效数字(1到M,包括1与M)。 给你N个整数card[1] .. card[n]表示你的手牌。大小王由0表示,其他牌由他们的值表示。ZB想知道自己手牌可以造出的直道最大长度是多少。 |
|
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). |
输入的首行是一个整数T,表示测试样例的数量。 对于每个测试样例: 第一行有2个整数N和M(1 <= N, M <= 100000),并且第二行有N个整数card[i] (0 <= card[i] <= M)。 |
|
Output |
输出 |
|
For each test case, output a single integer in a line -- the longest straight ZB can get. |
对于每个测试样例,输出一行一个整数——ZB可以得到的最大直道长度。 |
|
Sample Input - 输入样例 |
Sample Output - 输出样例 |
|
2 7 11 0 6 5 3 0 10 11 8 1000 100 100 100 101 100 99 97 103 |
5 3 |
【题解】
从数据来看,时间复杂度O(N2)必定爆炸(反正我没优化出来……),请使用O(Nlog2N)。
因此推测出算法,确定左端点(N),二分查(log2N)找在鬼牌允许使用范围内的右端点。
记录当前点前面有多少个0(空缺),然后两端一减就得到需要几张鬼牌填充了。
【代码 C++】
空间比较任性,速度还能优化……
#include<cstdio>
#include<cstring>
int data[], sot[];
int length(int L, int R, int i0){//[ L, R]
if (L == R){
if (sot[L] || i0) return ;
return ;
}
int mid = L + R >> ;
if (data[mid] - data[L - ] > i0) return length(L, mid, i0);
return mid - L + + length(mid + , R, i0 - (data[mid] - data[L - ]));
}
int main(){
int T, N, M, i, j, opt;
scanf("%d", &T);
while (T--){
memset(sot, , sizeof(sot));
scanf("%d%d", &N, &M);
for (data[] = i = ; i < N; ++i) scanf("%d", &j), ++sot[j];
for (i = ; i <= M; ++i){
if (sot[i]) data[i] = data[i - ];
else data[i] = data[i - ] + ;
}
for (opt = i = ; i <= M; ++i){
j = length(i, M, sot[]);
if (j>opt) opt = j;
}
printf("%d\n", opt);
}
return ;
}
FZU 2216
FZU 2216 The Longest Straight(最长直道)的更多相关文章
- FZU 2216——The Longest Straight——————【二分、枚举】
Problem 2216 The Longest Straight Accept: 17 Submit: 39Time Limit: 1000 mSec Memory Limit : 32 ...
- FZU 2216 The Longest Straight 模拟
题目链接:The Longest Straight 就是一个模拟就是这样,T_T然而当时恶心的敲了好久,敲完就WA了,竟然有这么简单的方法,真是感动哭了.......xintengziji...zhi ...
- FZU 2216 The Longest Straight 二分
0可以表示任何1到m的数,求一个最长的连续上升序列长度 因为m的范围在10w,所以以每个节点为起点 进行二分,复杂度mlogm 思路:b[i]表示到 1 到 i 有几个数没有出现,二分的时候注意加等号 ...
- 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 : ...
- 福建省赛--Problem E The Longest Straight(标记+二分)
Problem E The Longest Straight Accept: 71 Submit: 293 Time Limit: 1000 mSec Memory Limit : 327 ...
- leetcode 5 Longest Palindromic Substring--最长回文字符串
问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
随机推荐
- java笔试题: ——将e:/source文件夹下的文件打个zip包后拷贝到f:/文件夹下面
将e:/source文件夹下的文件打个zip包后拷贝到f:/文件夹下面 import java.io.*; import java.util.zip.ZipEntry; import java.uti ...
- 161208、Java enum 枚举还可以这么用
在大部分编程语言中,枚举类型都会是一种常用而又必不可少的数据类型,Java中当然也不会例外.然而,Java中的Enum枚举类型却有着许多你意想不到的用法,下面让我们一起来看看. 先来看一段代码示例: ...
- #import vs. @class
You #import or #include when there is a physical dependency. Otherwise, you use forward declarations ...
- fedora 20 yum出错
需要利用linux做项目,所以在win10装了vmvare 以及 fedora,据说这个linux比较稳定.. 1.系统装好以后,需要先把terminal调处理,这才符合程序猿的习惯嘛,具体方法如下 ...
- Android网络传输中必用的两个加密算法:MD5 和 RSA (附java完成测试代码)
MD5和RSA是网络传输中最常用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. 一.MD5算法 首先MD5是不可逆的,只能加密而不能解密.比如明 ...
- GCD 多线程同步
@property (strong, nonatomic) dispatch_queue_t barrierQueue; _barrieQueue = dispatch_queue_create(&q ...
- Eclipse中Outline里各种图标的含义
在使用Eclipse或者MyEclipse开发的时候,你一定看到过Outline和Package Explorer中小图标,很多刚刚接触编程的童鞋们可能不会在意它们代表的含义,但如果你花几分钟的时间了 ...
- linux进程调度方法(SCHED_OTHER,SCHED_FIFO,SCHED_RR)
转于:http://blog.csdn.net/maray/article/details/2900689 Linux内核的三种调度方法: 1,SCHED_OTHER 分时调度策略, 2,SCHED_ ...
- linux新内核的时钟机制代码
http://blog.chinaunix.net/uid-22810130-id-384173.html 如果说cfs是linux的一个很有创意的机制的话,那么linux中另一个创意就是nohz,我 ...
- poj1703 Lost Cows
给定集合{1,2,...,n}的一个置换,指定每个位置上在其左方且比其小的数的个数,求该置换. 这题我目前还只会O(n^2)的做法. 以后再用更高效的算法解决. http://poj.org/prob ...