(贪心) poj 1989

The Cow Lineup

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5342   Accepted: 3184

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.

Source

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
int n,k;
bool vis[10010];
int main()
{
    int ans=0,cnt=0,x;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        if(!vis[x])
        {
            vis[x]=1;
            cnt++;
        }
        if(cnt==k)
        {
            memset(vis,0,sizeof(vis));
            cnt=0;
            ans++;
        }
    }
    printf("%d\n",ans+1);
    return 0;
}

  

时间: 2024-10-16 06:38:38

(贪心) poj 1989的相关文章

贪心/POJ 3069 Saruman&#39;s Army

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int n,r,ans; 6 int a[1010]; 7 int cmax(int x,int y){return x>y?x:y;} 8 int main() 9 { 10 scanf("%d%d",&r,&n); 11 while (n!=-1 &

贪心 —— POJ 1017 Packets

对应POJ题目:点击打开链接 Packets Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 46584   Accepted: 15752 Description A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These pro

[贪心] poj 2376 Cleaning Shifts

题意: 给n个线段,问你覆盖满[1,T]内的每个点,最少需要多少条线段. 思路: 贪心,按照x坐标升序,y坐标降序排. 默认先放入第一条. 然后根据从属关系继续放线段. 注意的一点是 [1,2]和[3,4]就可以覆盖满[1,4]了. 然后注意因为默认放入了第一条,所第一条的状态要特判. 包括 第一条如果是[1,T]的话 输出1. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #i

贪心/poj 1017 Packets

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 5 int main() 6 { 7 int a1,a2,a3,a4,a5,a6; 8 scanf("%d%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5,&a6); 9 while ((a1+a2+a3+a4+a5+a6)!=0) 10 { 11 int ans=a6+a5

贪心/poj 1328 Radar Installation

1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 struct node 7 { 8 double right,left; 9 }; 10 node a[1010]; 11 int ans,n,d; 12 bool flag; 13 14 bool cmp(node x,node y) 15 { 16

贪心/poj 1456 Supermarket

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 struct node 6 { 7 int value; 8 int ddl; 9 }; 10 node a[10010]; 11 bool day[10010]; 12 int n,ans; 13 bool cmp(node x,node y) 14 { 15 return x.value>y

贪心/poj 1042 Gone Fishing

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 struct node 5 { 6 int d; 7 int fish; 8 int ans; 9 }; 10 node a[30],b[30]; 11 int t[30]; 12 int n,h,sum; 13 int main() 14 { 15 scanf("%d",&n); 16 while (n!=0) 17 { 18

贪心/poj 2231 Moo Volume

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; long long a[10010]; int main() { int n; scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%lld",&a[i]); sort(a+1,a+n+1); long long ans

贪心/poj 1922 Ride to School

1 #include<cstdio> 2 #include<cmath> 3 using namespace std; 4 int n,ans,v,t; 5 int main() 6 { 7 scanf("%d",&n); 8 while (n!=0) 9 { 10 ans=0x7fffffff; 11 for (int i=1;i<=n;i++) 12 { 13 scanf("%d%d",&v,&t); 14