zoukankan      html  css  js  c++  java
  • FZU-2216 The Longest Straight(尺取法)

     Problem 2216 The Longest Straight

    Accept: 523    Submit: 1663
    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

    第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)
    题意:

    你有n张牌在1~~m之间取值,0可以表示成任意一张牌(也就是王牌),

    现在问你你手里的牌最长有几张牌是连续的。

    思路:

    记录卡牌的值,和零的值,求出1至M之间缺少的个数是否小于等于零的个数,求出最大值,

    队友竟然用了尺取法!

    代码

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    using namespace std;
    int main()
    {
            std::ios::sync_with_stdio(false);
            int n,m,t,i,j,l,r,a,num[100001],b[100001],ans;
            cin>>t;
            while(t--)
            {
                    ans=0;
                    memset(num,0,sizeof(num));
                    memset(b,0,sizeof(b));
                    cin>>n>>m;
                    for(i=0;i<n;i++)
                    {
                            cin>>a;
                            num[a]++;
                    }
                    ans=0;
                    for(i=1;i<=m;i++)
                            if(num[i]==0)b[i]=b[i-1]+1;else b[i]=b[i-1];
                    int l=0,r=0;
                    while(r<=m)
                    {
                            if(b[r]-b[l]<=num[0])
                            {
                                    ans=max(ans,r-l);
                                    r++;
                            }
                            else l++;
                    }
                    cout<<ans<<endl;
            }
    
    }
    

      

  • 相关阅读:
    python 读csv文件时,在csv类型上执行类型转换
    python 读csv文件对列名进行合法性验证
    python 读csv数据 通过改变分隔符去掉引号
    python 写csv文件
    python 简单了解namedtuple
    python 读csv格式的文件
    python 序列化,反序列化
    PyQt5 笔记(05):信号/槽
    PyQt5 笔记(04):主窗口卡死问题
    PyQt5 笔记(03):弹出窗口大全
  • 原文地址:https://www.cnblogs.com/luowentao/p/8997034.html
Copyright © 2011-2022 走看看