H-the Cow Lineup (POJ 1989)

Source: Internet
Author: User

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

Description

Farmer John ' s n cows (1 <= n <= 100,000) is lined up in a row. Each cow are labeled with a number in the range 1...K (1 <= K <=10,000) identifying her breed. For example, a line of cows might has 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 the properties of number sequences like that above. For instance, he notices and the sequence 3 4 1 3 is a subsequence (not necessarily contiguous) of the sequence of breed IDs above. FJ is curious, the length of the shortest possible sequence he can construct out of numbers in the range 1..K that is a subsequence of the breed IDs of he cows. Help him solve this problem.

Input

* Line 1:two integers, N and K

* Lines 2..n+1:each line contains a single integer so 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 which is not a subsequence of the input

Sample Input

14 515325134425123

Sample Output

3

Hint

All the digit ' sequences ' appear. Each of the digit sequences also appears. Of the three digit sequences, the sequence 2, 2, 4 does not appear.

Source

Usaco 2004 U S Open at first I thought it was DP, I didn't dare to do it ... Sure enough, it was rather stupid. More simple greed. The idea is as follows: Because if the sequence contains a subsequence of length l, there must be a sequence can be divided into the L segment, each section contains K distinct numbers, the shortest is not included in the length of the subsequence is 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 namespacestd;#definell Long Long#define_cle (M, a) memset (M, A, sizeof (m))#defineRepu (I, A, b) for (int i = A; I < (b); i++)#defineMAXN 100005intNUM[MAXN];BOOLvis[10005];intMain () {intN, K;  while(~SCANF ("%d%d", &n, &k)) {Repu (I,0, N) scanf ("%d", &Num[i]); intLen =0, C =0; memset (Vis,false,sizeof(VIS)); Repu (i,0, N) {          if(Vis[num[i]]); ElseVis[num[i]] =trueC++; if(c = =k) {len+ +, C =0; memset (Vis,false,sizeof(VIS)); }} printf ("%d\n", Len +1); }    return 0;}
View Code

H-the Cow Lineup (POJ 1989)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.