The Cow Lineup
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4965 | Accepted: 2966 |
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.
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.
* 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 ", ans + 1); return 0; }