前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >poj-1989 The Cow Lineup

poj-1989 The Cow Lineup

作者头像
ShenduCC
发布2018-04-25 17:37:09
6010
发布2018-04-25 17:37:09
举报
文章被收录于专栏:算法修养

The Cow Lineup Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5587 Accepted: 3311 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

代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>

using namespace std;
#define MAX 100000
int a[MAX+5];
int b[MAX+5];
int n,k;
bool judge()
{
    for(int i=1;i<=k;i++)
        if(b[i]==0)
            return false;
    return true;
}
int main()
{
    int ans;
    int num;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        ans=0;
        num=0;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++)
        {
            if(!b[a[i]])
            {
                b[a[i]]=1;
                num++;
            }
            if(num==k)
            {
                ans++;
                num=0;
                memset(b,0,sizeof(b));
            }

        }
        printf("%d\n",ans+1);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-01-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档