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无解,然后, 忽略小 ...
随机推荐
- 《程序员思维修炼》读书笔记——week4
<程序员思维修炼>读书笔记——week4 PB16061441 陈昶金 这周读的是Andy Hunt的著作<程序员思维修炼>,这本书对于我这种刚刚入门的新手很友好,大多是讲一些 ...
- struts拦截器详解
拦截器是Struts2最强大的特性之一,它是一种可以让用户在Action执行之前和Result执行之后进行一些功能处理的机制. 说到拦截器interceptor,就会想到过滤器filter: 过滤器f ...
- Linux Shell系列教程之(一)Shell简介
本文是Linux Shell系列教程的第(一)篇,更多shell教程请看:Linux Shell系列教程 想要学习linux,shell知识必不可少,今天就给大家来简单介绍下shell的基本知识. S ...
- 扩展kmp--模板解析
扩展kmp: 用于求串的各个后缀与原串的最长公共前缀的长度: 上图的是字符串自匹配的过程: 图一: 假设现在匹配到i-1了,开始求next [ i ] 的值,此时,k记录的是到目前为止匹配到的最远的位 ...
- 解决使用webbrowser请求url时数据传递丢失问题
问题: 使用“ this.webBrowser.Url = new Uri(webBrowserUrl);”方式请求Action(Java Web)并传递数据,在webBrowserUrl中携带的参数 ...
- 应用defineProperty简单实现vue的双向数据绑定
双向数据绑定简易版本如何应用defineProperty的getter setter 方法 有这样HTML片段 <input type="text" id="dem ...
- LeetCode OJ——Longest Valid Parentheses
http://oj.leetcode.com/problems/longest-valid-parentheses/ 最大括号匹配长度,括号是可以嵌套的 #include <string> ...
- [Web Tools] 实用的Web开发工具
模拟http请求:Postman https://www.getpostman.com 生成Json数据 http://www.json-generator.com/
- k8s之pod控制器
1.生产中,很少会跑一个自主式pod,一般由控制器去创建pod,其配置文件中内嵌了pod的创建方式. pod控制器:ReplicaSet.Deployment.DaemonSet.Job.Cronjo ...
- CF768
Codeforces Round #406 (Div. 1) A.Berzerk 考虑先手必胜态,一定是先手移动到某一个位置以后,这个位置是后手的必败态 考虑先手必败态,一定是无论先手如何移动,先手所 ...