Problem 2216 The Longest Straight

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

2

7 11

0 6 5 3 0 10 11

8 1000

100 100 100 101 100 99 97 103

 Sample Output

5

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)的更多相关文章

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

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

  2. The Longest Straight(二分,离散化)

     Problem 2216 The Longest Straight Accept: 7    Submit: 14 Time Limit: 1000 mSec    Memory Limit : 3 ...

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

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

  4. FZU 2216——The Longest Straight——————【二分、枚举】

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

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

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

  6. FZU 2216 The Longest Straight 模拟

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

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

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

  8. LeetCode算法题-Longest Palindrome(五种解法)

    这是悦乐书的第220次更新,第232篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第87题(顺位题号是409).给定一个由小写或大写字母组成的字符串,找到可以用这些字母构 ...

  9. UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)

    题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...

随机推荐

  1. 3 - 简单了解一下springboot中的yml语法 和 使用yml赋值

    1.简单了解yml语法 2.使用yml给实体类赋值 准备工作:导入依赖 <!-- 这个jar包就是为了实体类中使用@ConfigurationProperties(prefix = " ...

  2. cephfs文件系统场景

    创建cephfs文件系统: [cephfsd@ceph-admin ceph]$ cd /etc/ceph [cephfsd@ceph-admin ceph]$ ceph fs ls No files ...

  3. hadoop/spark面试题

    总结于网络 转自:https://www.cnblogs.com/jchubby/p/5449379.html 1.简答说一下hadoop的map-reduce编程模型 首先map task会从本地文 ...

  4. CRLF漏洞浅析

    部分情况下,由于与客户端存在交互,会形成下面的情况 也就是重定向且Location字段可控 如果这个时候,可以向Location字段传点qqgg的东西 形成固定会话 但服务端应该不会存储,因为后端貌似 ...

  5. 队列——Java实现

    1 package struct; 2 3 interface IQueue{ 4 //入队列 5 void add(Object obj); 6 //出队列 7 Object remove(); 8 ...

  6. Linux磁盘与文件系统原理

    这一章主要是原理性的,介绍了Linux文件系统的运作原理.涉及到很多计算机组成和操作系统的原理性知识,这部分知识很多都忘了,在这里复习下.    我们只看本章第1,2节.--------------- ...

  7. 类型类 && .class 与 .getClass() 的区别

    一. 什么是类型类 Java 中的每一个类(.java 文件)被编译成 .class 文件的时候,Java虚拟机(JVM)会为这个类生成一个类对象(我们姑且认为就是 .class 文件),这个对象包含 ...

  8. 【Java】【IDE】【Jetbrain Idea】Intellij IDEA 快捷键整理

    [常规] Ctrl+Shift + Enter,语句完成 "!",否定完成,输入表达式时按 "!"键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更 ...

  9. cookie,sessionStorage,loclaStorage,HTML5应用程序缓存

    cookie Cookie 是一些数据,由服务器生成,发送给浏览器,一旦用户从该网站或服务器退出,Cookie 就存储在用户本地的硬盘上,下一次请求同一网站时会把该cookie发送给服务器.Cooki ...

  10. 《手把手教你》系列技巧篇(五十)-java+ selenium自动化测试-字符串操作-上篇(详解教程)

    1.简介 自动化测试中进行断言的时候,我们可能经常遇到的场景.从一个字符串中找出一组数字或者其中的某些关键字,而不是将这一串字符串作为结果进行断言.这个时候就需要我们对字符串进行操作,宏哥这里介绍两种 ...