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. ServiceStack.OrmLite 笔记8 -还是有用的姿势

    复杂点的使用2 InsertAll, UpdateAll and DeleteAll 的参数要是IEnumerables Each关键字 返回 IEnumerable 并且是延迟加载的 全局设置 当字 ...

  2. 函数(def)

    一.为什么要使用函数: 面向过程的编程在代码里有很多代码是可以重复利用的,如果使用面向过程编程会使代码显得纷繁复杂,不利于他人和自己日后的阅读.而在函数式编程里,则可以把每个可重复利用的功能封装在一个 ...

  3. STL--queue

    queue-概述: 队列是一种特殊的线性表,它只允许在表的前端(Front)进行删除操作,而在表的后端(Rear)进行插入操作. l进行插入操作的端称为队尾,进行删除操作的端称为队头.队列中没有元素时 ...

  4. 加快Win7整体运行速度的12个小技巧

    在整体运行速度方面,微软Windows 7系统超越了它的前任Vista,拥有明显的提升;但是相比最新的Windows 8,似乎又有所不及,至少很少有Windows用户能够体会到15秒的开机速度.虽然如 ...

  5. ZOJ-2338 The Towers of Hanoi Revisited 输出汉诺塔的最优解移动过程

    题意:给定N(1<= N <=64)个盘子和M(4<= M <= 65)根柱子,问把N个盘子从1号柱子移动到M号柱子所需要的最少步数,并且输出移动过程. 分析:设f[i][j] ...

  6. iOS - Swift SQLite 数据库存储

    前言 采用 SQLite 数据库来存储数据.SQLite 作为一中小型数据库,应用 iOS 中,跟前三种保存方式相比,相对比较复杂一些. 注意:写入数据库,字符串可以采用 char 方式,而从数据库中 ...

  7. MyEclipse + Tomcat 热部署问题

    myEclipse设置对应的tomcat时,只需要在jdk的Optional Java VM arguments中添加如下设置: -Xms256m -Xmx512m-Dcom.sun.manageme ...

  8. nodejs学习笔记<二>简单的node服务器

    在环境搭建好后,就可以开始动手架设(node驱动)一个简单的web服务器. 首先,nodejs还是用js编写.先来看一段node官网上的实例代码. var http = require('http') ...

  9. css+JS实现遮罩弹框

    <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta nam ...

  10. RedHat 7配置yum源

    卸载自带的yum软件包 rpm -e yum-utils--.el7.noarch --nodeps rpm -e yum-rhn-plugin--.el7.noarch --nodeps rpm - ...