The longest straight (FZUoj2216)

Source: Internet
Author: User

problem 2216 the longest straightaccept:82 submit:203
Time limit:1000 mSec Memory limit:32768 KB problem Description

ZB is playing a card game where the goal are to make straights. Each card of 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 is used as any valid number (between 1 and M, including 1 and M).

You'll be given N integers card[1]. CARD[N] Referring to the cards in your hand. Jokers is represented by zeros, and other cards is represented by their values. ZB wants to know the number of cards in the longest straight the can is 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 is 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 Input2

7 11

0 6 5 3 0 10 11

8 1000

100 100 100 101 100 99 97 103

Sample Output5

3

Source Sixth Fujian University College student Program design Contest-Replay (thank you to the organizer of Huaqiao University)

Idea: The ruler method, which points are marked first, and the number of records 0, and then the prefix array statistics 1 to M in the non-appearing numbers,

In the use of a ruler to run the interval to take the longest.

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
usingnamespaceStd
intaa[100005];
intbb[100005];
intMainvoid)
{
intN,i,j,k,p,q;
scanf"%d", &k);
while(k--)
{
memset (AA,0,sizeof(AA));
memset (BB,0,sizeof(BB));
scanf"%d%d", &p,&q);intMa=0;
for(i=0; i<p; i++)
{
scanf"%d", &n);
aa[n]++;
if(ma<n)
{
Ma=n;
}
}
intcnt=aa[0];
bb[0]=0; Ma=min (MA+CNT,Q);
for(i=1; i<=q; i++)
{
if(!aa[i])
{
bb[i]=bb[i-1]+1;
}
Else
{
bb[i]=bb[i-1];
}

}
intLl=1;
intRr=1;
intmaxx=0;

while(rr<=ma&&ll<=rr+1)
{
while(bb[rr]-bb[ll-1]&LT;=CNT&AMP;&AMP;RR&LT;=MA)
{
rr++;
}
Maxx=max (Maxx, (RR-LL));
ll++;
}
printf"%d\n", Maxx);
}
return0;}

}

The longest straight (FZUoj2216)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.