FZU 2216 The Longest Straight (二分)
2016-08-20 19:42
423 查看
[align=center]Problem 2216 The Longest Straight
[/align]
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
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.
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).
For each test case, output a single integer in a line -- the longest straight ZB can get.
2
7 11
0 6 5 3 0 10 11
8 1000
100 100 100 101 100 99 97 103
5
3
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2216
题目大意:求最长的顺子的长度,0可以代替任何数
题目分析:记录0的个数为cnt0,算出从1到i连成顺子所需要的0的个数num[i],枚举左端点,二分查找加cnt0个0能达到的最远右端点
比如样例1
num[1]=1 num[2]=2 num[3]=2 num[4]=3 num[5]=3 num[6]=3 num[7]=4 num[8]=5 num[9]=6 num[10]=6 num[11]=6
答案是2 3 4 5 6或者3 4 5 6 7
2的时候在num中找大于num[1]+cnt0=3的数字,找到的是7,7-2=5
3的时候在num中找大于num[2]+cnt0=4的数字,找到的是8,8-3=5
[/align]
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 1does 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
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
27 11
0 6 5 3 0 10 11
8 1000
100 100 100 101 100 99 97 103
Sample Output
53
Source
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)题目链接:http://acm.fzu.edu.cn/problem.php?pid=2216
题目大意:求最长的顺子的长度,0可以代替任何数
题目分析:记录0的个数为cnt0,算出从1到i连成顺子所需要的0的个数num[i],枚举左端点,二分查找加cnt0个0能达到的最远右端点
比如样例1
num[1]=1 num[2]=2 num[3]=2 num[4]=3 num[5]=3 num[6]=3 num[7]=4 num[8]=5 num[9]=6 num[10]=6 num[11]=6
答案是2 3 4 5 6或者3 4 5 6 7
2的时候在num中找大于num[1]+cnt0=3的数字,找到的是7,7-2=5
3的时候在num中找大于num[2]+cnt0=4的数字,找到的是8,8-3=5
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int const MAX = 100005; int has[MAX], num[MAX]; int main() { int T; scanf("%d", &T); while(T --) { memset(has, 0, sizeof(has)); memset(num, 0, sizeof(num)); int n, m, cnt0 = 0, x; scanf("%d %d", &n, &m); for(int i = 0; i < n; i++) { scanf("%d", &x); if(x) { has[x] = 1; } else { cnt0 ++; } } for(int i = 1; i <= m; i++) { num[i] = num[i - 1] + !has[i]; } printf("\n"); int ans = 0; for(int i = 1; i <= m; i++) { int cur = upper_bound(num + i, num + m + 1, num[i - 1] + cnt0) - (num + i); ans = max(ans, cur); } printf("%d\n", ans); } }
相关文章推荐
- FZU 2216——The Longest Straight——————【二分、枚举】
- FZU 2216 The Longest Straight (二分)
- FZU Problem 2216 The Longest Straight(二分答案+树状数组)——第六届福建省大学生程序设计竞赛-重现赛
- FZU 2216 The Longest Straight 枚举+二分
- FZU-2216 The Longest Straight (二分枚举)
- FZU 2216 The Longest Straight (二分)
- fzu 2216 The Longest Straight
- 2015福建省赛 fzoj The Longest Straight 2216 (二分&转换)好题
- The Longest Straight(FZUoj2216)
- FZU2216 The Longest Straight
- FZUoj 题目2216 The Longest Straight*(二分)
- The Longest Straight FZU - 2216
- FZU 2216 The Longest Straight (尺取法)
- fzu 2216 The Longest Straight
- 福建省赛--Problem E The Longest Straight(标记+二分)
- FOJ Problem 2216 The Longest Straight
- The Longest Straight(二分,离散化)
- fzu 2216 The Longest Straight 二分
- 福建省赛--Problem E The Longest Straight(标记+二分)
- FOJ 2216 The Longest Straight 第六届福建省大学生程序设计竞赛 E 尺取法乱搞