zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    012 spring retry重试原理的解析
    011 @Retryable的使用
    010 @ControllerAdvice
    009 SpringBoot+Swagger的使用
    008 @Import作用
    007 SpringBoot的@EnableAutoConfiguration注解
    001 品牌管理案例
    000 vue各种基本指令
    013 JS
    002 docker基本的命令
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5924938.html
Copyright © 2011-2022 走看看