FOJ 2216 The Longest Straight 第六届福建省大学生程序设计竞赛 E 尺取法乱搞

The Longest Straight
Accept: 161    Submit: 479
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可以当“会”(赖子)填补空隙,问多一次出多少张牌。

思路:先sort然后unique,把0的数量记录下来,用一个队列来放0,每个元素代表0装作的那个数,然后把所有的0初始放到第一个前面,比如第一个数是9,有3个0,那么队列里就显示6,7,8.然后就尺取法从左往右走,遇到坑就从队列里拿0出来填,更新答案为当前数字-拿出的那个0装作的数字。最后要注意一点,他把那些数字限制在一个m范围里,所以结果要跟m做一次min



#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <iomanip>
#include <time.h>
#include <set>
#include <map>
#include <stack>
using namespace std;

const int maxn=100010;

int a[maxn];

queue<int> que;

int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--){
        int n,m;
        cin>>n>>m;
        while(!que.empty())
            que.pop();
        int cnt=0;//num of zeros
        for(int i=0;i<n;i++){
            cin>>a[i];
            if (!a[i]){
                cnt++;
//                que.push(0);
            }
        }
        sort(a,a+n);
        int ed=unique(a,a+n)-a;
        int st=(a[0]==0?1:0);
        for(int i=a[st]-cnt;i<a[st];i++)
            que.push(i);

        if (n==cnt){
            cout<<min(cnt,m)<<endl;
            continue;
        }

        int ans=cnt+1,cur=1+cnt;
        for(int i=st+1;i<ed;i++){
            if (a[i]-a[i-1]==1)cur++;
            else if (a[i]-a[i-1]-1<=cnt){
                for(int j=1;j<=a[i]-a[i-1]-1;j++){
                    int k=que.front();
                    cur=a[i]-k;
                    que.pop();
                    que.push(a[i-1]+j);
                }

            }
//                cur++;
            else{
                for(int j=a[i]-cnt;j<a[i];j++){
                    que.pop();
                    que.push(j);
                }
                cur=cnt+1;
            }
            ans=max(ans,cur);
//            cout<<"i:"<<i<<","<<cur<<","<<ans<<endl;
        }
        cout<<min(ans,m)<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值