您的位置:首页 > 其它

POJ 1989 The Cow Lineup

2013-07-31 17:25 281 查看
[b]The Cow Lineup[/b]

Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 4965Accepted: 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:

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: