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. 将文件读取到内存、打印pe结构

    #include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <string.h&g ...

  2. CATransition(过渡)

    属性动画只对图层的可动画属性起作用,所以如果要改变一个不能动画的属性(比如图片),或者从层级关系中添加或者移除图层,属性动画将不起作用. 于是就有了过渡的概念.过渡并不像属性动画那样平滑地在两个值之间 ...

  3. HDU 1698 Just a Hook(线段树成段更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  4. STL--stack

    stack--概述: 栈(Stack)是一种特殊的线性表,只能在某一端插入和删除的特殊线性表.它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶.栈也称为先进后出表(LIFO). ...

  5. [转]-Android Studio 快捷键整理分享-SadieYu

    文章编辑整理:Android Studio 中文组 - SadieYu Alt+回车 导入包,自动修正 Ctrl+N   查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L  格式化代码 ...

  6. jqurey click和blur执行时间冲突

    参考资料:http://stackoverflow.com/questions/10652852/jquery-fire-click-before-blur-event

  7. 硬盘坏道 检测/修复 Windows

    1. 主要参看了:http://jingyan.baidu.com/article/2c8c281dfd93df0008252a9b.html 2. 2.1.hdtunepro.zip 是在 http ...

  8. poj1927Area in Triangle

    链接 物理渣只能搜题解了.. 分三种情况 1.len>=a+b+c 2.len<内切圆半径 圆的面积是最大的 -->以len为周长的圆 3.看这篇http://blog.sina.c ...

  9. Eclipse中配置Tomcat服务器

    在首先外部安装好tomcat,然后在eclipse配置Tomcat服务器: 选择要配置的tomcat版本: 选择tomcat的安装的路径,选择你安装的JRE: 到此tomcat服务器就算是配置好了,接 ...

  10. python一个注意的地方

    https://www.zhihu.com/question/25874136 class test: l=[] def init(self): self.l=['1','2','7'] a1=tes ...