zoukankan      html  css  js  c++  java
  • The Longest Straight(二分,离散化)

     Problem 2216 The Longest Straight

    Accept: 7    Submit: 14 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

    题解:

    让求最长连续上升的序列长度,可任意摆放次序;其中0可以代表1~M之间的任意一个数;

    离散化数组,把里面元素从0开始,出现过就是前一个值,没出现过就是前一个+1;计数0出现的次数n0;然后0~M对于每一个数进行二分查找,找出差值为n0的最大序列长度即可;

    代码:

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<vector>
    using namespace std;
    const int INF=0x3f3f3f3f;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define PI(x) printf("%d",x)
    #define T_T while(T--)
    const int MAXN=100010;
    int a[MAXN],b[MAXN];
    int erfen(int l,int r,int i,int x){
    	int mid;
    	while(l<=r){
    		mid=(l+r)>>1;
    		if(b[mid]-b[i]>x)r=mid-1;
    		else l=mid+1;
    	}
    	return l-i-1;
    }
    int main(){
    	int T,N,M,x;
    	SI(T);
    	T_T{
    		SI(N);SI(M);
    		mem(a,0);
    		int n0=0;
    		for(int i=0;i<N;i++){
    			SI(x);
    			if(x==0)n0++;
    			else a[x]=1;
    		}
    		b[0]=0;
    		for(int i=1;i<=M;i++){
    			if(a[i])
    				b[i]=b[i-1];
    			else b[i]=b[i-1]+1;
    		}
    		int ans=0;
    		for(int i=0;i<=M;i++){
    			ans=max(ans,erfen(i,M,i,n0));
    		}
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    android —— textview解析html富文本
    android toolbar及可折叠标题栏
    dp——cf1327F
    cf1327D,E——组合数学
    技能表自查
    python 之禅 import this
    转--Python标准库之一句话概括
    python 爆破
    python -- 算法
    python 代码模板
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5080894.html
Copyright © 2011-2022 走看看