poj 2376 Cleaning Shifts 贪心 区间问题
<pre name="code" class="html">
Cleaning Shifts
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 14425 | Accepted: 3700 |
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.
<span style="font-size:18px;color:#3366ff;">#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
struct Node{
int s;
int e;
}node[25005];
int cmp(Node a,Node b)
{
if(a.s!=b.s)
return a.s<b.s;
else
return a.e>b.e;
}
int main()
{
int n,q;
while(~scanf("%d %d",&n,&q))
{
for(int i=1;i<=n;i++)
scanf("%d %d",&node[i].s,&node[i].e);
sort(node+1,node+n+1,cmp);
if(node[1].s!=1)
{
printf("-1\n");
continue;
}
int cnt=1,i=1,r=node[1].e,temp=0;
while(r<q)
{
temp=0;
for(int j=i+1;node[j].s<=node[i].e+1&&j<=n;j++)
if(node[j].e>r)
{
temp=j;
r=node[j].e;
}
if(temp==0)
break;
else
{
cnt++;
r=node[temp].e;
i=temp;
}
}
if(r<q)
printf("-1\n");
else
printf("%d\n",cnt);
}
return 0;
}
</span><span style="font-size: 14px;">
</span>
poj 2376 Cleaning Shifts 贪心 区间问题的更多相关文章
- 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 贪心
Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
- poj 2376 Cleaning Shifts 最小区间覆盖
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40751 Accepted: 9871 ...
- POJ 2376 Cleaning Shifts (贪心,区间覆盖)
题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...
- POJ 2376 Cleaning Shifts(轮班打扫)
POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Farmer ...
- poj 2376 Cleaning Shifts
http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 2376 Cleaning Shifts【贪心】
POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...
- 【原创】poj ----- 2376 Cleaning Shifts 解题报告
题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K ...
- POJ 2376 Cleaning Shifts 区间覆盖问题
http://poj.org/problem?id=2376 题目大意: 给你一些区间的起点和终点,让你用最小的区间覆盖一个大的区间. 思路: 贪心,按区间的起点找满足条件的并且终点尽量大的. 一开始 ...
随机推荐
- 如何使用加多宝(jdb)在linux下调试Java程序
毕业时写了一段时间的C,那时候调试使用gdb,后来转了java,当时就想java程序怎么调试,找了一下,果然,那就是jdk自带的jdb windows里是这样的 Linux下是这样的 一般我在linu ...
- MySQL中的数据库对象
1.数据库中一般包含下列对象 表.约束.索引.触发器.序列.视图: 可以使用图形用户界面或通过显式执行语句来创建这些数据库对象.用于创建这些数据库对象的语句称为“数据定义语言”(DDL),它们通常以关 ...
- Specialization For SCCM
JUST A LINK FOR ALL SCCM QUESTION http://eskonr.com/
- 网页上图片点击放大js代码
//图片弹出事件 function showPict(path) { src = path; var mask = "<div style = 'position: absolute; ...
- 【原创】大数据基础之Oozie(4)oozie使用的spark版本升级
oozie默认使用的spark是1.6,一直没有升级,如果想用最新的2.4,需要自己手工升级 首先看当前使用的spark版本的jar # oozie admin -oozie http://$oozi ...
- 动画方案 Lottie 学习(二)之实战
代码 $('.success-info-title').append('<p class="normal_finish" id="normal_finish_ani ...
- shell脚本视频学习1
一.知识点:变量,参数传递 练习1:使用shell脚本,输出当前所在的目录 练习2:计算/etc目录下有多少个文件,用shell脚本实现 ls -l--->数一下, ls -l|wc -l ( ...
- spring JdbcTemplate如何返回多个结果集
最近很少发博客,先是去了***公司呆了几年,完全不能上外网,后来又出来了,能上外网了,但项目太忙一直在打码,用的语言也从C#换成了JAVA. 好在两者比较相似,转起来还算方便,近日在操作sqlserv ...
- postman 接口测试(一)
一.postman 应用场景 开发接口快速的调用接口,以便调试 方便的调用接口,通过不同的参数去测试接口的输出 这些接口调用时需要保存下来的反复运行的 在运行中如果有断言(检查点 <预期 和现实 ...
- Tomcat7设置环境变量供java代码读取
前两天要做一个后台线程分片处理任务功能,把应用放在tomcat中部署在多个服务器上,每个服务器分片处理一些任务,这里需要在java代码中获取到tomcat的信息和服务器信息.在网上找了好久,终于找到了 ...