POJ 1989 The Cow Lineup
2013-07-31 17:25
281 查看
[b]The Cow Lineup[/b]
Description
Farmer John's N cows (1 <= N <= 100,000) are lined up in a row.Each cow is labeled with a number in the range 1...K (1 <= K <=10,000) identifying her breed. For example, a line of 14 cows might have these breeds:
Farmer John's acute mathematical mind notices all sorts of properties of number sequences like that above. For instance, he notices that the sequence 3 4 1 3 is a subsequence (not necessarily contiguous) of the sequence of breed IDs above. FJ is curious what is the length of the shortest possible sequence he can construct out of numbers in the range 1..K that is NOT a subsequence of the breed IDs of his cows. Help him solve this problem.
Input
* Line 1: Two integers, N and K
* Lines 2..N+1: Each line contains a single integer that is the breed ID of a cow. Line 2 describes cow 1; line 3 describes cow 2; and so on.
Output
* Line 1: The length of the shortest sequence that is not a subsequence of the input
Sample Input
Sample Output
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4965 | Accepted: 2966 |
Farmer John's N cows (1 <= N <= 100,000) are lined up in a row.Each cow is labeled with a number in the range 1...K (1 <= K <=10,000) identifying her breed. For example, a line of 14 cows might have these breeds:
1 5 3 2 5 1 3 4 4 2 5 1 2 3
Farmer John's acute mathematical mind notices all sorts of properties of number sequences like that above. For instance, he notices that the sequence 3 4 1 3 is a subsequence (not necessarily contiguous) of the sequence of breed IDs above. FJ is curious what is the length of the shortest possible sequence he can construct out of numbers in the range 1..K that is NOT a subsequence of the breed IDs of his cows. Help him solve this problem.
Input
* Line 1: Two integers, N and K
* Lines 2..N+1: Each line contains a single integer that is the breed ID of a cow. Line 2 describes cow 1; line 3 describes cow 2; and so on.
Output
* Line 1: The length of the shortest sequence that is not a subsequence of the input
Sample Input
14 5 1 5 3 2 5 1 3 4 4 2 5 1 2 3
Sample Output
3 题目大意:给定一个由1~k组成的整数序列。对于一个由1~k中,某些数字组成的,长度为n的短序列中,不是k的子序列的最小的n为多少。其中,短序列中各元素在原序列中的位置不要求连续,但必须按原顺序出现在原序列中。
#include <stdio.h> #include <iostream> #include <string.h> using namespace std; int cow[100005]; int visited[100005]; int main() { int n, k, nCount = 0, ans = 0; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &cow[i]); } for(int i = 0; i < n; i++) { if (!visited[cow[i]]) { nCount++; visited[cow[i]] = 1; } if (nCount == k) { ans++; memset(visited, 0, sizeof(visited)); nCount = 0; } } printf("%d\n", ans + 1); return 0; }
相关文章推荐
- POJ1989 The Cow Lineup——贪心+扫描技巧
- POJ 1989 The Cow Lineup [dp]
- poj 1989 The Cow Lineup 最短非子序列问题
- poj 1989 The Cow Lineup 数组
- poj 1989 The Cow Lineup
- [USACO2004][poj1989]The Cow Lineup(乱搞)
- POJ1989 The Cow Lineup 额,贪心??
- POJ 1989 The Cow Lineup 笔记
- poj-1989 The Cow Lineup
- POJ 1989 The Cow Lineup【最短非子序列】
- poj-1989 The Cow Lineup
- poj 1989 The Cow Lineup
- H-The Cow Lineup(POJ 1989)
- poj 3180 The Cow Prom 强连通分量
- POJ 3267 The Cow Lexicon
- POJ-3278 Catch the cow(BFS)
- POJ 3267-The Cow Lexicon (动态规划)
- POJ 3267, The Cow Lexicon
- POJ 3267 The Cow Lexicon
- POJ 3267 The Cow Lexicon