• 福建省赛--Problem E The Longest Straight(标记+二分)


    Problem E The Longest Straight

    Accept: 71    Submit: 293
    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

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    int num[100100],flog[100100];
    int main()
    {
    	int t;
    	scanf("%d",&t);
    	while(t--)
    	{
    		int n,m,s,zero=0;
    		memset(flog,0,sizeof(flog));
    		memset(num,0,sizeof(num)); 
    		scanf("%d%d",&n,&m);
    		for(int i=0;i<n;i++)
    		{
    			scanf("%d",&s);
    			if(s)
    				num[s]=1;
    			else 
    				zero++;
    		}
    		flog[0]=0;
    		for(int i=1;i<=m;i++)
    		{
    			if(num[i])
    			flog[i]=flog[i-1];
    			else
    			flog[i]=flog[i-1]+1;
    		}
    		int L=-1000000;
    		for(int i=0;i<=m;i++)
    		{
    			int l=i,r=m;
    			int mid;
    			while(l<=r)
    			{
    				mid=(l+r)/2;
    				if(flog[mid]-flog[i]>zero)
    				r=mid-1;
    				else
    				l=mid+1;
    			}
    			L=max(L,r-i);
    		}
    		printf("%d
    ",L);
    	}
    	return 0;
    }


  • 相关阅读:
    改造我们的学习:有钱不会花,抱着金库抓瞎
    (转)我奋斗了18年才和你坐在一起喝咖啡
    初学者要知道的十件事
    [转]C#图像处理 (各种旋转、改变大小、柔化、锐化、雾化、底片、浮雕、黑白、滤镜效果)
    C#调用系统的复制、移动、删除文件对话框
    SQLite数据类型
    jquery禁用dropdownlist中某一项
    C# winform无标题窗体随意移动
    注册.NET Framework
    jQuery同步/异步调用后台方法
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273593.html
Copyright © 2020-2023  润新知