Chinaunix首页 | 论坛 | 博客
  • 博客访问: 350632
  • 博文数量: 122
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 1191
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 11:12
文章分类

全部博文(122)

文章存档

2010年(122)

我的朋友

分类: C/C++

2010-05-06 10:40:36

一、问题描述

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.

 

二、解题思路

    从头到尾遍历一次,每次找齐全部1-K时,res+=1。最后输出结果res+1。

三、代码

 

#include<stdio.h>
#include<iostream>
#include<memory>
#include<string.h>
using namespace std;
int s[1000005];
bool b[100005];
int N,K;
int cnt;
int res;
int main()
{
    int i;
    scanf("%d%d",&N,&K);
    for(i=0;i<N;++i)
        scanf("%d",&s[i]);

    memset(b,0,sizeof(b));
    cnt=0;
    for(i=0;i<N;++i)
    {
        if(b[ s[i] ] ==false)
        {
            b[ s[i] ]=true;
            cnt+=1;
        }
        if(cnt==K)
        {
            res+=1;
            memset(b,0,sizeof(b));
            cnt=0;
        }
    }
    printf("%d\n",res+1);
    return 0;
}


阅读(1474) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~