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

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

 
一开始还以为是dp,我都没敢下手啊。。。果然是比较笨。
比较简单的贪心。思路如下:
      因为若是序列包含长度为l的子序列,必然有序列可以划分为l段,每段都包含k个互不相同的数字,则最短的不被包含的子序列长度为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 namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < (b); i++)
#define MAXN 100005 int num[MAXN];
bool vis[];
int main()
{
int n, k;
while(~scanf("%d%d", &n, &k))
{
repu(i, , n) scanf("%d", &num[i]);
int len = , c = ;
memset(vis, false, sizeof(vis));
repu(i, , n) {
if(vis[num[i]]) ;
else vis[num[i]] = true, c++;
if(c == k) {
len++, c = ;
memset(vis, false, sizeof(vis));
}
}
printf("%d\n", len + );
}
return ;
}

H-The Cow Lineup(POJ 1989)的更多相关文章

  1. [USACO2004][poj1989]The Cow Lineup(乱搞)

    http://poj.org/problem?id=1989 题意:求一个序列的最短非子序列长度l,即长度小于l的所有数的排列都是原序列的子序列(不一定要连续的),求l得最小值. 分析: 我们从左到右 ...

  2. (01背包变形) Cow Exhibition (poj 2184)

    http://poj.org/problem?id=2184   Description "Fat and docile, big and dumb, they look so stupid ...

  3. DP:Cow Bowling(POJ 3176)

    北大教你怎么打保龄球 题目很简单的,我就不翻译了,简单来说就是储存每一行的总数,类似于状态压缩 #include <stdio.h> #include <stdlib.h> # ...

  4. 洛谷P3069 [USACO13JAN]牛的阵容Cow Lineup(尺取法)

    思路 考虑比较朴素的解法,枚举每个长度为\(k+1\)的区间,然后统计区间中出现次数最多的颜色.这样的话复杂度为\(O(n*k)\)的,显然不行. 观察到统计每个区间中出现次数最多的颜色中,可以只用看 ...

  5. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  6. POJ 2018 Best Cow Fences(二分答案)

    题目链接:http://poj.org/problem?id=2018 题目给了一些农场,每个农场有一定数量的奶牛,农场依次排列,问选择至少连续排列F个农场的序列,使这些农场的奶牛平均数量最大,求最大 ...

  7. 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)

    Charm Bracelet    POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...

  8. (动规 或 最短路)Help Jimmy(poj 1661)

    http://poj.org/problem?id=1661 Description "Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度各不相同的 ...

  9. Games:取石子游戏(POJ 1067)

    取石子游戏 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37662   Accepted: 12594 Descripti ...

随机推荐

  1. SqlServer 存储过程分页

    适用于2005以上版本 create procedure [dbo].[SP_GetPageList] ( @columns nvarchar(max), --查询字段 @tablename nvar ...

  2. svn使用(服务器端和客户端)

    http://www.cnblogs.com/tugenhua0707/p/3969558.html 网址如上. 很详细.

  3. Nofollow

    今天整理一下SEO中经常用到的nofollow属性. nofollow它是HTML标签中的一个属性值,作用是告诉搜索引擎不要跟踪带有改属性值的链接, 用于指示搜索引擎不要追踪(即抓取)网页上的带有no ...

  4. iOS - Swift 与 C 语言交互编程

    前言 作为一种可与 Objective-C 相互调用的语言,Swift 也具有一些与 C 语言的类型和特性,如果你的代码有需要,Swift 也提供了和常见的 C 代码结构混合编程的编程方式. 1.基本 ...

  5. iOS - UILabel

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UILabel : UIView <NSCoding> @available(iOS 2.0, *) p ...

  6. eclipse 技巧

    1. eclipse中xml中提示需有xsd文档 如在线eclipse将自动网络获取.xsd 否则 手动本地添加(在xml catalog参数设置选项) 2.当明确实现功能时,可将已有方法抽取成接口, ...

  7. 小度Wifi_设置

    PS:现在我用的小度Wifi驱动的 安装程序的版本为:“XiaoduWiFi140923_M_3.0.9.rar”(保存于“百度云 OsSkill --> 软件安装包 > 小度Wifi__ ...

  8. wpf datagrid 如何让标头 及内容居中

    头就是这么居中<DataGrid> <DataGrid.ColumnHeaderStyle> <Style TargetType="DataGridColumn ...

  9. python操作postgresql数据库

    import psycopg2 conn = psycopg2.connect(database=") cur = conn.cursor() cur.execute("CREAT ...

  10. Android activity四种基本启动模式

    standard:默认的模式,每次启动会新创建一个activity对象 singleTop:在当前任务栈中,判断栈顶是否为当前的activity,如果是,就直接使用,如果不是,就会创建新的activi ...