zoukankan      html  css  js  c++  java
  • POJ 1989 The Cow Lineup

    The Cow Lineup
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 4965   Accepted: 2966

    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
    题目大意:给定一个由1~k组成的整数序列。对于一个由1~k中,某些数字组成的,长度为n的短序列中,不是k的子序列的最小的n为多少。其中,短序列中各元素在原序列中的位置不要求连续,但必须按原顺序出现在原序列中。
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int cow[100005];
    int visited[100005];
    
    int main()
    {
        int n, k, nCount = 0, ans = 0;
        scanf("%d%d", &n, &k);
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &cow[i]);
        }
        for(int i = 0; i < n; i++)
        {
            if (!visited[cow[i]])
            {
                nCount++;
                visited[cow[i]] = 1;
            }
            if (nCount == k)
            {
                ans++;
                memset(visited, 0, sizeof(visited));
                nCount = 0;
            }
        }
        printf("%d
    ", ans + 1);
        return 0;
    }
  • 相关阅读:
    VS2015 update3 安装 asp.net core 失败
    connection timeout 和command timeout
    安装.NET Core
    xamarin 学习笔记02- IOS Simulator for windows 安装
    xamarin 学习笔记01-环境配置
    BotFramework学习-02
    BotFramework学习-01
    正则表达式
    获取指定字节长度的字符串
    pdf生成器
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3228289.html
Copyright © 2011-2022 走看看