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;
    }
  • 相关阅读:
    Asp.Net MVC 常用开发方式之EF Code First
    整理一下Entity Framework的查询
    C#中yield return用法分析
    SQL Server表和字段说明的增加和更新
    C#中一个问号和两个问号(a ?? b)的作用
    你应该知道的25道Javascript面试题
    ASP.NET Core Razor 页面路由
    ASP.NET Core MVC – Tag Helper 组件
    ASP.NET Core 防止跨站请求伪造(XSRF/CSRF)攻击
    ASP.NET Core 使用Cookie验证身份
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3228289.html
Copyright © 2011-2022 走看看