fzu 2216 The Longest Straight
2016-04-24 21:09
148 查看
Problem 2216 The Longest Straight
Accept: 178 Submit: 536
Time Limit: 1000 mSec Memory Limit : 32768 KB
ProblemDescription
ZB is playing a card game where the goal is to makestraights. Each card in the deck has a number between 1 and M(including 1 andM). A straight is a sequence of cards with consecutive values. Values do notwrap around, so 1 does not come after
M. In addition to regular cards, the deckalso contains jokers. Each joker can be used as any valid number (between 1 andM, including 1 and M).
You will be given N integers card[1] .. card
referringto the cards in your hand. Jokers are represented by zeros, and other cards arerepresented by their values. ZB wants to know the number of cards in thelongest straight that can be formed
using one or more cards from his hand.
Input
The first line contains an integer T, meaning the numberof the cases.
For each test case:
The first line there are two integers N and M in thefirst line (1 <= N, M <= 100000), and the second line contains N integerscard[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
SampleOutput
5
3
Source
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)
一道令我伤心了好久的题目,省赛那时我非常弱鸡,这题没做出来,与奖失之交臂。。。
题目大意:给你一些牌(0表示鬼牌,可以代替任何牌)求可以组成的顺子的最大长度。(斗地主的顺子知道吧)
思路:二分+枚举,每次枚举左端点,求这个左端点能组成的最大长度。
当然,也有更快的做法,时间复制度为O(N)
Accept: 178 Submit: 536
Time Limit: 1000 mSec Memory Limit : 32768 KB
ProblemDescription
ZB is playing a card game where the goal is to makestraights. Each card in the deck has a number between 1 and M(including 1 andM). A straight is a sequence of cards with consecutive values. Values do notwrap around, so 1 does not come after
M. In addition to regular cards, the deckalso contains jokers. Each joker can be used as any valid number (between 1 andM, including 1 and M).
You will be given N integers card[1] .. card
referringto the cards in your hand. Jokers are represented by zeros, and other cards arerepresented by their values. ZB wants to know the number of cards in thelongest straight that can be formed
using one or more cards from his hand.
Input
The first line contains an integer T, meaning the numberof the cases.
For each test case:
The first line there are two integers N and M in thefirst line (1 <= N, M <= 100000), and the second line contains N integerscard[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
SampleOutput
5
3
Source
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)
一道令我伤心了好久的题目,省赛那时我非常弱鸡,这题没做出来,与奖失之交臂。。。
题目大意:给你一些牌(0表示鬼牌,可以代替任何牌)求可以组成的顺子的最大长度。(斗地主的顺子知道吧)
思路:二分+枚举,每次枚举左端点,求这个左端点能组成的最大长度。
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int maxn = 100005; int n, m, zero; int num[maxn]; //num表示从1到i成为顺子所需鬼牌的个数 int card[maxn]; int main() { int T; scanf("%d", &T); while (T--) { zero = 0; memset(num, 0, sizeof(num)); memset(card, 0, sizeof(card)); scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); if (!x) zero++; else card[x] = 1; } for (int i = 1; i <= m; i++) num[i] = num[i - 1] + (1-card[i]); int ans = 0; for (int i = 1; i <= m; i++) { int k = upper_bound(num + i, num + m + 1, zero + num[i-1]) - (num + i); ans = max(ans, k); } printf("%d\n", ans); } return 0; }
当然,也有更快的做法,时间复制度为O(N)
#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 */
相关文章推荐
- 小技巧,关于OC打印指针地址和arc下的retaincount
- 浅谈特定型人工智能设计
- leetcode-70. Climbing Stairs
- adv7180 驱动 设置ain 输入
- TensorFlow人工智能引擎入门教程之九 RNN/LSTM循环神经网络长短期记忆网络使用
- 稀疏编码(Sparse Coding)的前世今生(一) 转自http://blog.csdn.net/marvin521/article/details/8980853
- STL map的遍历与pair的使用
- 生产者消费者模型——wait/notify/notifyAll使用
- BNU 51640 Training Plan DP
- Airbase-ng帮助
- Capturing 'self' strongly in this block is likely to lead to a retain cycle
- Failure to transfer org.apache.maven.plugins:maven-war-plugin:pom:2.2 from https://repo.maven.apache
- init done opengl support available
- 172. Factorial Trailing Zeroes
- HDU 2389 Rain on your Parade (HK二分图)
- rt3070无线网卡工作在staion模式----fl2440开发板
- leetcode-217. Contains Duplicate
- 5、Jenkins Email Extension Plugin插件使用说明
- hdu3487Play with Chain【splay】
- 计算机视觉和人工智能领域:教程、网站、博客记录(陆续更新)