The Longest Straight(FZUoj2216)
Accept: 82 Submit: 203
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
7 11
0 6 5 3 0 10 11
8 1000
100 100 100 101 100 99 97 103
Sample Output
3
Source
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)
思路:尺取法,先将哪些点是否出现过标记,并记录0的个数,然后再前缀数组统计1到M中的未出现的数字,
在用尺取法跑一遍区间取最长就可以了。
#include<stdio.h>#include<algorithm>#include<iostream>#include<stdlib.h>#include<string.h>using namespace std;int aa[100005];int bb[100005];int main(void){ int n,i,j,k,p,q; scanf("%d",&k); while(k--) { memset(aa,0,sizeof(aa)); memset(bb,0,sizeof(bb)); scanf("%d %d",&p,&q);int ma=0; for(i=0; i<p; i++) { scanf("%d",&n); aa[n]++; if(ma<n) { ma=n; } } int cnt=aa[0]; bb[0]=0;ma=min(ma+cnt,q); for(i=1; i<=q; i++) { if(!aa[i]) { bb[i]=bb[i-1]+1; } else { bb[i]=bb[i-1]; } } int ll=1; int rr=1; int maxx=0; while(rr<=ma&&ll<=rr+1) { while(bb[rr]-bb[ll-1]<=cnt&&rr<=ma) { rr++; } maxx=max(maxx,(rr-ll)); ll++; } printf("%d\n",maxx); } return 0;}
}
The Longest Straight(FZUoj2216)的更多相关文章
- FZU 2216 The Longest Straight(最长直道)
Description 题目描述 ZB is playing a card game where the goal is to make straights. Each card in the dec ...
- The Longest Straight(二分,离散化)
Problem 2216 The Longest Straight Accept: 7 Submit: 14 Time Limit: 1000 mSec Memory Limit : 3 ...
- FZU-2216 The Longest Straight (二分枚举)
题目大意:给n个0~m之间的数,如果是0,那么0可以变为任意的一个1~m之间的一个数.从中选出若干个数,使构成一个连续的序列.问能构成的最长序列的长度为多少? 题目分析:枚举连续序列的起点,二分枚举二 ...
- 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(尺取法)
Problem 2216 The Longest Straight Accept: 523 Submit: 1663Time Limit: 1000 mSec Memory Limit ...
- FZU 2216 The Longest Straight 模拟
题目链接:The Longest Straight 就是一个模拟就是这样,T_T然而当时恶心的敲了好久,敲完就WA了,竟然有这么简单的方法,真是感动哭了.......xintengziji...zhi ...
- 福建省赛--Problem E The Longest Straight(标记+二分)
Problem E The Longest Straight Accept: 71 Submit: 293 Time Limit: 1000 mSec Memory Limit : 327 ...
- LeetCode算法题-Longest Palindrome(五种解法)
这是悦乐书的第220次更新,第232篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第87题(顺位题号是409).给定一个由小写或大写字母组成的字符串,找到可以用这些字母构 ...
- UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)
题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...
随机推荐
- 简单的Mybatis程序
1.新建一个普通Maven项目,导入 mybatis.mysql.junit(用于测试)3个依赖 Mybatis <dependency> <groupId>org.mybat ...
- CSS区分Chrome和Firefox
CSS区分Chrome和FireFox 描述:由于Chrome和Firefox浏览器内核不同,对CSS解析有差别,因此常会有在两个浏览器中显示效果不同的问题出现,解决办法如下: /*Chrome*/ ...
- hadoop-uber作业模式
如果作业很小,就选择和自己在同一个JVM上运行任务,与在一个节点上顺序运行这些任务相比,当application master 判断在新的容器中的分配和运行任务的开销大于并行运行它们的开销时,就会发生 ...
- 大数据学习day24-------spark07-----1. sortBy是Transformation算子,为什么会触发Action 2. SparkSQL 3. DataFrame的创建 4. DSL风格API语法 5 两种风格(SQL、DSL)计算workcount案例
1. sortBy是Transformation算子,为什么会触发Action sortBy需要对数据进行全局排序,其需要用到RangePartitioner,而在创建RangePartitioner ...
- Sharding-JDBC 简介
什么是Sharding-JDBC 1.是轻量级的 java 框架,是增强版的 JDBC 驱动2. Sharding-JDBC(1)主要目的是:简化对分库分表之后数据相关操作.不是帮我们做分库分表,而是 ...
- Linux系统中安装软件方法总结
Linux系统中安装软件方法总结 [1]Linux系统中安装软件的几种方式 [2] Linux配置yum源(本地源和网络源) [3] SuSE下zypper源配置 [4] SUSE zypper 本地 ...
- Kotlin 学习(2)
属性和字段 1.声明属性 Kotlin中可以使用var关键字声明可变属性,或者用val关键字声明只读属性,属性的类型在后面,变量名在前面,中间加冒号和空格. public class Address ...
- 【Git项目管理】Git分支 - 远程分支
远程分支 远程引用是对远程仓库的引用(指针),包括分支.标签等等. 你可以通过 git ls-remote (remote) 来显式地获得远程引用的完整列表,或者通过 git remote show ...
- AOP与IOC的概念
AOP与IOC的概念(即spring的核心) a) IOC:Spring是开源框架,使用框架可以使我们减少工作量,提高工作效率并且它是分层结构,即相对应的层处理对应的业务逻辑,减少代码的耦合度.而sp ...
- Linux学习 - 文件包处理命令
一.搜索文件find find [搜索范围] [匹配条件] (1) -name(名字查找) <1> find /etc -name init 查找/etc下以 "in ...