Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >poj-1989 The Cow Lineup

poj-1989 The Cow Lineup

作者头像
ShenduCC
发布于 2018-04-25 09:37:09
发布于 2018-04-25 09:37:09
63300
代码可运行
举报
文章被收录于专栏:算法修养算法修养
运行总次数:0
代码可运行

The Cow Lineup Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5587 Accepted: 3311 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

脑洞题,做这种题目需要灵感。思路就是这个数列中包含1到k所有数字的子序列为一个集合,答案是就是集合数加1

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>

using namespace std;
#define MAX 100000
int a[MAX+5];
int b[MAX+5];
int n,k;
bool judge()
{
    for(int i=1;i<=k;i++)
        if(b[i]==0)
            return false;
    return true;
}
int main()
{
    int ans;
    int num;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        ans=0;
        num=0;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++)
        {
            if(!b[a[i]])
            {
                b[a[i]]=1;
                num++;
            }
            if(num==k)
            {
                ans++;
                num=0;
                memset(b,0,sizeof(b));
            }

        }
        printf("%d\n",ans+1);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-01-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
POJ-2018 Best Cow Fences(二分加DP)
Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10174 Accepted: 3294 Description Farmer John’s farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains a certain number of cows, 1 <= ncows
ShenduCC
2018/04/26
6460
Code Forces 645C Enduring Exodus
C. Enduring Exodus time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output In an attempt to escape the Mischievous Mess Makers’ antics, Farmer John has abandoned his farm and is traveling to the o
ShenduCC
2018/04/26
5780
POJ--2158--------------Milking Grid(最小覆盖字符矩阵)---(开二维kmp)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6169 Accepted: 2573 Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that is R (1 <= R <= 10,000) rows by C (1 <= C <= 75) c
Gxjun
2018/03/22
6230
POJ-3275:奶牛排序Ranking the Cows(Floyd、bitset)
Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ would like to order his cows according to these rates from the fastest milk producer to the slowest.
ACM算法日常
2018/08/07
7570
poj 3613 Cow Relays
For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout the pasture.
全栈程序员站长
2022/07/10
2340
POJ-2184 Cow Exhibition(01背包变形)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Description “Fat and docile, big and dumb, they look so stupid, they aren’t much fun…” - Cows with Guns by Dana Lyons The cows want to p
ShenduCC
2018/04/26
8100
一道有趣的树状数组题
Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gathering of cows from around the world. MooFest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer, and of course, mooing. When the cows all stand in line for a particular event, they moo so loudly that the roar is practically deafening. After participating in this event year after year, some of the cows have in fact lost a bit of their hearing.
ACM算法日常
2020/02/16
5170
一道有趣的树状数组题
POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 10
Angel_Kitty
2018/04/08
1.5K0
POJ-2181 Jumping Cows(贪心)
Jumping Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7329 Accepted: 4404 Description Farmer John’s cows would like to jump over the moon, just like the cows in their favorite nursery rhyme. Unfortunately, cows can not jump
ShenduCC
2018/04/25
8770
hdu 2838 Cow Sorting(树状数组)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2239 Accepted Submission(s): 711
全栈程序员站长
2022/07/07
2610
POJ 3616 Milking Time
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15650 Accepted: 6644 Description
风骨散人Chiam
2020/10/28
3750
P2880 [USACO07JAN]平衡的阵容Balanced Lineup
题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的选择和所有牛的身高 (1 <= 身高 <= 1,000,000). 他想知道每一组里面最高和最低的牛的身高差别. 输入: 第1行:N,Q 第2到N+1行:每头牛的身高 第N+2到N+Q
attack
2018/04/11
6390
poj-3660-cows contest(不懂待定)
N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.
瑾诺学长
2018/09/21
3880
POJ2375 Cow Ski Area 【强连通分量】+【DFS】
Time Limit: 1000MS Memory Limit: 65536K
全栈程序员站长
2022/08/26
2430
Catch That Cow (POJ - 3278)(简单BFS)
题解:给你x、y,x可以加1、减1、或者变成2*x,问通过最少的次数来让x等于y,这是最基础的bfs,就是把x通过一次的+1、-1、*2得到的数都放到队列里面,再把这些通过一次操作得到的数进行相同的操作+1、-1、*2,因为用个结构体来存放这个数是第几次操作得到的,所以只要一旦发现这个数,一定是通过最小的次数得到的。
Lokinli
2023/03/09
2700
POJ3622 Gourmet Grazers(FHQ Treap)
Description Like so many others, the cows have developed very haughty tastes and will no longer graze on just any grass. Instead, Farmer John must purchase gourmet organic grass at the Green Grass Grocers store for each of his N (1 ≤ N ≤ 100,000) cows. Ea
attack
2018/04/11
6510
【POJ 3176】Cow Bowling(DP)
The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this:
饶文津
2020/05/31
4830
HDUOJ-----2838Cow Sorting(组合树状数组)
Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2163    Accepted Submission(s): 671 Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Ea
Gxjun
2018/03/22
7060
POJ3617 Best Cow Line(贪心水题)
FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges.
天道Vax的时间宝藏
2021/08/11
2630
POJ - 3278 Catch That Cow 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
风骨散人Chiam
2020/10/28
3560
相关推荐
POJ-2018 Best Cow Fences(二分加DP)
更多 >
交个朋友
加入腾讯云官网粉丝站
蹲全网底价单品 享第一手活动信息
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验