zoukankan      html  css  js  c++  java
  • H-The Cow Lineup(POJ 1989)

    The Cow Lineup
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 5367   Accepted: 3196

    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. 

    Source

     
    一开始还以为是dp,我都没敢下手啊。。。果然是比较笨。
    比较简单的贪心。思路如下:
          因为若是序列包含长度为l的子序列,必然有序列可以划分为l段,每段都包含k个互不相同的数字,则最短的不被包含的子序列长度为l+1。
    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <algorithm>
    using namespace std;
    #define ll long long
    #define _cle(m, a) memset(m, a, sizeof(m))
    #define repu(i, a, b) for(int i = a; i < (b); i++)
    #define MAXN 100005
    
    int num[MAXN];
    bool vis[10005];
    int main()
    {
        int n, k;
        while(~scanf("%d%d", &n, &k))
        {
            repu(i, 0, n) scanf("%d", &num[i]);
            int len = 0, c = 0;
            memset(vis, false, sizeof(vis));
            repu(i, 0, n) {
              if(vis[num[i]]) ;
              else vis[num[i]] = true, c++;
              if(c == k) {
                    len++, c = 0;
                     memset(vis, false, sizeof(vis));
              }
            }
            printf("%d
    ", len + 1);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Python
    算法的时间复杂度和空间复杂度-总结
    列表自动滚动
    React 结合ant-mobile 省市区级联
    5G从小就梦想着自己要迎娶:高速率、低时延、大容量三个老婆
    一文读懂GaussDB(for Mongo)的计算存储分离架构
    cpu占用过高排查
    我是如何从零开始自学转行IT并进入世界500强实现薪资翻倍?
    Linux重定向用法详解
    说出来也许你不信,我被 Linux 终端嘲笑了……
  • 原文地址:https://www.cnblogs.com/sunus/p/4476934.html
Copyright © 2011-2022 走看看