fzu 2216 The Longest Straight
2017-08-13 17:03
363 查看
Description
ZB is playing a card game where the goal is tom make straights. Each card in the deck has a number between 1 and M, inclusive. 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 regularcards, the deck also contains jokers. Each joker can be used as any valid number (between 1 and M, inclusive).You will be giben N integers card[1]...cardreferring 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 ormore 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 <= 10^5), and the second line contains N integers card[i](0 <= card[i] <= M).Output
For each test case, output a single integer 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时间复制度为O(N)[cpp] view plain copy#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int maxn = 1e5 + 5; int card[maxn], num[maxn], pos[maxn]; int main() { int T; scanf("%d", &T); while (T--) { memset(pos, 0, sizeof(pos)); memset(num, 0, sizeof(num)); memset(card, 0, sizeof(card)); int m, n, zero = 0; scanf("%d%d", &m, &n); for (int i = 1; i <= m; i++) { int x; scanf("%d", &x); if (x == 0) zero++; else card[x] = 1; } int ans = 0,k; pos[0] = 1; for (int i = 1; i <= n; i++) { num[i] = num[i - 1] + (1 - card[i]); if (!card[i]) pos[num[i]] = i; k = num[i] - zero; if (k <= 0) { ans = i; continue; } ans = max(ans, i - pos[k]); } printf("%d\n", ans); } return 0; } /* 22 7 11 0 6 5 3 0 10 11 8 1000 100 100 100 101 100 99 97 103 3 5 1 2 3 3 2 1 2 3 3 5 1 2 0 3 5 2 3 4 3 5 2 0 4 */
相关文章推荐
- FZU 2216——The Longest Straight——————【二分、枚举】
- FZU 2216 The Longest Straight (二分)
- The Longest Straight(FZUoj2216)
- FZU 2216 The Longest Straight (二分)
- FZU-2216 The Longest Straight (二分枚举)
- FZU 2216 The Longest Straight (尺取法)
- FZU 2216 The Longest Straight (二分)
- The Longest Straight FZU - 2216
- fzu 2216 The Longest Straight
- FZU2216 The Longest Straight
- FZU 2216 The Longest Straight 枚举+二分
- FZU Problem 2216 The Longest Straight(二分答案+树状数组)——第六届福建省大学生程序设计竞赛-重现赛
- FZUoj 题目2216 The Longest Straight*(二分)
- FOJ 2216 The Longest Straight 第六届福建省大学生程序设计竞赛 E 尺取法乱搞
- 2015福建省赛 fzoj The Longest Straight 2216 (二分&转换)好题
- FZOJ 2216 The Longest Straight (尺取法)
- FOJ Problem 2216 The Longest Straight
- The Longest Straight
- 福建省赛--Problem E The Longest Straight(标记+二分)
- 2015福建省赛 fzoj The Longest Straight