POJ2376 Cleaning Shifts 【贪心】
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 11542 | Accepted: 3004 |
Description
the last being shift T.
Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval.
Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.
Input
* Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.
Output
Sample Input
3 10
1 7
3 6
6 10
Sample Output
2
Hint
INPUT DETAILS:
There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10.
OUTPUT DETAILS:
By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.
Source
用最少的线段覆盖全部的点
#include <stdio.h>
#include <string.h>
#include <algorithm> #define maxn 25005 struct Node {
int u, v;
} E[maxn]; bool cmp(Node a, Node b) {
return a.u < b.u;
} int main() {
int N, T, flag, ans, right, i;
while(scanf("%d%d", &N, &T) == 2) {
for(i = 0; i < N; ++i)
scanf("%d%d", &E[i].u, &E[i].v);
std::sort(E, E + N, cmp);
ans = right = 0;
flag = 1;
i = 0;
while(flag <= T) {
for( ; i < N && E[i].u <= flag; ++i)
if(E[i].u <= flag && E[i].v > right)
right = E[i].v;
if(right >= flag) flag = right + 1, ++ans;
else break;
}
if(flag <= T) ans = -1;
printf("%d\n", ans);
}
return 0;
}
POJ2376 Cleaning Shifts 【贪心】的更多相关文章
- POJ2376 Cleaning Shifts
题意 POJ2376 Cleaning Shifts 0x50「动态规划」例题 http://bailian.openjudge.cn/practice/2376 总时间限制: 1000ms 内存限制 ...
- POJ 2376 Cleaning Shifts 贪心
Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
- POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)
Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...
- poj 2376 Cleaning Shifts 贪心 区间问题
<pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS Memory ...
- poj2376 Cleaning Shifts【线段树】【DP】
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32561 Accepted: 7972 ...
- poj-2376 Cleaning Shifts (排序+贪心)
http://poj.org/problem?id=2376 john有n头牛做打扫工作,他想在t时间内每个时间都至少有一头牛在做打扫工作,第一头牛在1,最后一头牛在t时间,每一头牛工作都有一个开始时 ...
- poj2376 Cleaning Shifts(区间贪心,理解题意)
https://vjudge.net/problem/POJ-2376 题意理解错了!!真是要仔细看题啊!! 看了poj的discuss才发现,如果前一头牛截止到3,那么下一头牛可以从4开始!!! # ...
- poj2376 Cleaning Shifts 区间贪心
题目大意: (不说牛了) 给出n个区间,选出个数最少的区间来覆盖区间[1,t].n,t都是给出的. 题目中默认情况是[1,x],[x+1,t]也是可以的.也就是两个相邻的区间之间可以是小区间的右端与大 ...
- POJ 2376 Cleaning Shifts (贪心,区间覆盖)
题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...
随机推荐
- 理解 PHP output buffer
在需要使用输出缓存区的时候,一般会在代码中加上ob_start()这个函数. 这是因为php.ini中output_buffering设置为off时,则缓存区处于关闭状态,需要用ob_start()打 ...
- 【mysql优化 3】嵌套循环连接算法
原文地址:Nested-Loop Join Algorithms mysql在表之间执行连接操作,包括了使用循环嵌套算法或者其他在此基础上的变形. 循环嵌套连接算法: 一个简单的嵌套循环连接(NLJ: ...
- 阿里云 fatal: Authentication failed 退出码 128
阿里云拉取代码时需要输入的用户名密码 跟我们登录aliyun的时候的账号密码不一样,需要先重置一下的,重置之前要配置好邮箱 https://code.aliyun.com/profile/passwo ...
- [Err] 1022 - Can't write; duplicate key in table '#sql-1500_26'
今天用powerdesigner修改了一些外键关系,有两个外键的名字取一样的,忘记改了.然后在用navicat运行sql文件时,报出[Err] 1022 - Can't write; dupl ...
- js方法encodeURI后,关于get请求url长度的限制测试与总结
(本文仅作个人记录只用比较啰嗦,重点只看红字部分即可) Test.jsp 请求test1.jsp IE11环境下,get请求url最多4096个字节: 请求的是http://localhost:908 ...
- bzoj3924 [Zjoi2015]幻想乡战略游戏 点分树,动态点分
[BZOJ3924][Zjoi2015]幻想乡战略游戏 Description 傲娇少女幽香正在玩一个非常有趣的战略类游戏,本来这个游戏的地图其实还不算太大,幽香还能管得过来,但是不知道为什么现在的网 ...
- [暑假集训--数论]poj2657 Comfort
Description A game-board consists of N fields placed around a circle. Fields are successively number ...
- element-ui select组件使用需要注意的
当在使用select组件的时候,要注意 <el-select v-model="scope.row.state" @change="editDriftStatus& ...
- 字蛛(font-spider)-单独压缩字体(解决页面少有的特殊字体的字体包引用)
特别想独立的把这个问题写成一篇内容,分享给大家. 反正我是这个字体压缩使用的受益者,不是打广告. 很久以前,设计师总是爱用一些奇奇怪怪的字体放在页面上,而作为前端我们很容易的就能直接使用TA们用到的字 ...
- HDU6214 Smallest Minimum Cut
题目链接:HDU6214 留一个链式前向星+Dinic模板(希望不要被某人发现,嘿嘿嘿). #include <cstdio> #include <cstring> #incl ...