码迷,mamicode.com
首页 > 其他好文 > 详细

The Cow Lineup_找规律

时间:2016-10-01 00:01:29      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:

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

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_找规律

标签:

原文地址:http://www.cnblogs.com/iwantstrong/p/5924938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!