首页 > 代码库 > The Cow Lineup_找规律
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 515325134425123
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_找规律
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。