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. C++ STL算法之:copy

    C++ STL算法:copy 目录(?)[+] 前面十二个算法所展现的都属于非变易算法(Non-mutating algorithms)系列,现在我们来看看变易算法.所谓变易算法(Mutating a ...

  2. C#表格GridView显示2位百分比

    <asp:BoundField HeaderText="占比" DataField="number" DataFormatString="{0: ...

  3. adverb

    An adverb is a word or an expression that modifies a verb, adjective, another adverb, determiner [限定 ...

  4. ES5中改变this指向的三种方法

    ES5中提供了三种改变函数中this指针指向的方法,分别如下 1.call() var obj = {username:"孙悟空"}; //没有任何修饰的调用函数,函数中的this ...

  5. 30个类手写Spring核心原理之环境准备(1)

    本文节选自<Spring 5核心原理> 1 IDEA集成Lombok插件 1.1 安装插件 IntelliJ IDEA是一款非常优秀的集成开发工具,功能强大,而且插件众多.Lombok是开 ...

  6. Tomcat(2):配置Tomcat

    1,打开IDEA创建一个项目 2,配置Tomcat服务器 3,运行 5,成功 t t

  7. SpringBoot(4):整合Mybatis

    1. 导入mybatis所需要的依赖 1 <!-- 引入 myBatis,这是 MyBatis官方提供的适配 Spring Boot的--> 2 <dependency> 3 ...

  8. SpringCloud微服务-Eureka服务注册与发现

    一. Eureka 是什么? Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移.服务注册与发现对微服务 ...

  9. 2.ElasticSearch集群的搭建

    1.创建elasticsearch-cluster文件夹,在内部复制三个elasticsearch服务 2.修改elasticsearch-cluster\node*\config\elasticse ...

  10. 【Linux】【CentOS】【FTP】FTP服务器安装与配置(vsftpd、lftp)

    [初次学习.配置的笔记,如有不当,欢迎在评论区纠正 -- 萌狼蓝天 @ 2021-12-02] 基本概念 FTP访问方式 实体账号:本地账户 来宾账户:guest 匿名登录:anonymous fp ...