The Longest Straight FZU - 2216 (枚举左端点+二分右端点)

14人阅读 评论(0) 收藏 举报
分类:

The Longest Straight

FZU - 2216

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

题意:有n张牌,这些牌的数字在1-m之间,其中如果牌的数字是0代表大王,可以变成任何1-m的数字,问用这n张牌可以构成的最长的连续上升序列是多少(1 2 3 4 5前后数字只相差1)

思路:用前缀和记录1-m中前i个数字中有多少个它没有,也就是前i个数字中我们需要几个0,从头记录到尾,而且我们记录下我们有的零的个数,然后每次枚举左端点从1-m一次枚举,然后巧妙运用upper_bound查找范围从i-最后,查找的值就是zero+左端点之前需要的零(因为是从头记录的嘛)减去左端点地址就得到了当前最大长度,然后每次比较得到总的最长长度

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100005;
int n,m,zero;
int sum[maxn];//前缀和用于记录前i个数字中有多少个数字没有
int card[maxn];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        zero = 0;
        memset(sum,0,sizeof(sum));
        memset(card,0,sizeof(card));
        scanf("%d%d",&n,&m);
        for(int i = 0; i < n; i++){
            int x;
            scanf("%d",&x);
            if(!x) zero++;
            else card[x] = 1;
        }
        for(int i = 1; i <= m; i++){
            sum[i] = sum[i-1] + (!card[i]);//记录没有的牌的个数
        }
        int ans = 0;
        for(int i = 1; i <= m; i++){//枚举左端点
            int len = upper_bound(sum + i,sum + 1 + m,zero + sum[i-1]) - (sum + i);
            //upper_bound查找第一个大于查找值的位置,所以第一个大于查找值位置减去起始位置恰好就是满足题意的值的长度
            //从sum+i开始到sum+1+m最后查找,我们想要找有zero个空缺的一个长度,而因为我们记录的是前缀和是从头开始的,所以我们查找的值实际是zero+之前的sum[i-1]
            ans = max(ans,len);
        }
        printf("%d\n",ans);
    }
    return 0;
}

查看评论

FZU 2216 The Longest Straight 枚举+二分

FZU 2216 The Longest Straight 枚举+二分
  • qq_26071477
  • qq_26071477
  • 2016-08-20 18:29:40
  • 313

fzu 2216 The Longest Straight

Problem 2216 The Longest Straight Accept: 178    Submit: 536 Time Limit: 1000 mSec    Memory Limit :...
  • qq1059752567
  • qq1059752567
  • 2016-04-24 21:09:53
  • 391

FZU 2216 The Longest Straight (二分)

Problem 2216 The Longest Straight Accept: 128    Submit: 369 Time Limit: 1000 mSec    Memory Lim...
  • L_BestCoder
  • L_BestCoder
  • 2016-04-03 17:37:11
  • 329

FZU Problem 2216 The Longest Straight(二分答案+树状数组)——第六届福建省大学生程序设计竞赛-重现赛

FZU Problem 2216 The Longest Straight(二分答案+树状数组)——第六届福建省大学生程序设计竞赛-重现赛...
  • queuelovestack
  • queuelovestack
  • 2016-09-08 16:36:18
  • 416

FZU 2216 The Longest Straight (二分)

FZU 2216 The Longest Straight (二分)
  • Tc_To_Top
  • Tc_To_Top
  • 2016-08-20 19:42:02
  • 346

fzu 2216 The Longest Straight 二分

题目链接http://acm.fzu.edu.cn/problem.php?pid=2216题意给出n张牌,每张牌有个数值,n张牌中最大的值为m。值为0的牌可以代替任意值,问可以凑出最长顺子的长度是多...
  • chen_minghui
  • chen_minghui
  • 2017-10-06 13:31:36
  • 54

CodeForces - 612D (排序+区间计数+扫描线 区间覆盖)

题目链接:http://codeforces.com/problemset/problem/612/D题目大意:给出n条线段,让你算这些线段重合次数大于等于k次的部分输入: 第一行:给出两个数n  k...
  • Dch19990825
  • Dch19990825
  • 2018-04-11 18:54:02
  • 43

HDU 5862Counting Intersections (思维+树状数组)

实际上这种在左端点+1,右端点的下一个-1的题目已经见过很多了。适用于它的查询也是一个区间的。+1,-1相当于维护了一个区间的“高度”,因为查询也是区间的,只要这个区间某一个点的高度被+1了,那这个区...
  • AngOn823
  • AngOn823
  • 2016-08-18 23:29:16
  • 617

SCU2016-02 Q题区间dp入门

Analyse: 区间dp,我们逐步的扩大区间来做。这样大区间就可以转化为两种情况 一种是小区间然后加上成功匹配一对括号。 另一种是各种小区间的合并。 Get: 区间dp的经典模型,枚举区间...
  • jibancanyang
  • jibancanyang
  • 2016-07-06 14:31:45
  • 219

POJ 3667&& NYOJ 534 线段树(求满足条件的最左面区间)

这道题目是月赛时的一道题,也是POJ上的一道原题,月赛时想了一个多小时,没写出来。昨天又写了一天,总算写出来了。这道题和一般线段树不一样,需要在线段树中增加几个量。         lsum代表某个...
  • wmn_wmn
  • wmn_wmn
  • 2012-04-24 08:08:56
  • 881
    个人资料
    持之以恒
    等级:
    访问量: 3万+
    积分: 3990
    排名: 9791
    文章存档
    最新评论