• 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: 
        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
    ",ans+1);
        }
        return 0;
    }
  • 相关阅读:
    禁止 git 自动转换换行符
    一个单元测试问题的解决
    关于脏读、幻象读、不可重复读的理解
    PKCS7 的 attached 和 detached 方式的数字签名
    关于DES加密中的 DESede/CBC/PKCS5Padding
    解决grep的结果无法显示文件名的问题
    解决64位操作系统下运行psql的问题
    一个用于将sql脚本转换成实体类的js代码
    批量将代码中的 get_XXX 替换成 XXX
    关于数据库中密码的存储
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5924938.html
Copyright © 2020-2023  润新知