The Cow Lineup_找规律
Posted 阿宝的锅锅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The Cow Lineup_找规律相关的知识,希望对你有一定的参考价值。
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
Hint
All the single digit ‘sequences‘ appear. Each of the 25 two digit sequences also appears. Of the three digit sequences, the sequence 2, 2, 4 does not appear.
【题意】给定一串长度为n,各位上的数小于等于m的正数序列。问最短的不属于该序列子串的串长度为多少。
【思路】找规律。
前8个数字已经使1,2,3,4,5都至少出现一次。
所以前8个数字可以归为一个集合。
然后再发现后6个数字也是1,2,3,4,5都至少出现了一次,也归为一个集合。
最后的答案数是集合数+1.
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int N=100005; const int M=10005; int a[N],vis[M]; int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } memset(vis,0,sizeof(vis)); int sum=0,ans=0; for(int i=1;i<=n;i++) { if(vis[a[i]]==0&&a[i]<=m) { vis[a[i]]=1; sum+=vis[a[i]]; if(sum==m) { sum=0; memset(vis,0,sizeof(vis)); ans++; } } } printf("%d\n",ans+1); } return 0; }
以上是关于The Cow Lineup_找规律的主要内容,如果未能解决你的问题,请参考以下文章
[Luogu3069][USACO13JAN]牛的阵容Cow Lineup