Cleaning Shifts POJ - 2376 (贪心题)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 31194 | Accepted: 7677 |
Description
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.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=; int n,t,lst=,ans;
struct nde
{
int st,en;
}cw[];
bool cmp(nde p,nde q)
{
if(p.st!=q.st)
return p.st<q.st;
return p.en>q.en;
}
int main()
{
int i,j,k,l,f;
cin>>n>>t;
for(i=;i<=n;i++)
cin>>cw[i].st>>cw[i].en;
sort(cw+,cw+n+,cmp);
for(i=;i<=n;i++)
{
// cout<<lst<<" ";
f=;
if(lst<cw[i].st-)
{
cout<<"-1"<<endl; //没有连续或者交集的时间段肯定做不完
return ;
}
for(j=,k=lst;cw[j].st<=lst+ && j<=n;j++)
if(cw[j].en>k)
{
k=cw[j].en;
i=j;
f=;
}
if(f)
ans++;
lst=k;
}
if(lst<t)
{
cout<<"-1"<<endl;
return ;
}
cout<<ans<<endl;
}
Cleaning Shifts POJ - 2376 (贪心题)的更多相关文章
- Greedy:Cleaning Shifts(POJ 2376)
牛的大扫除 题目大意:农夫有N只牛,这些牛要帮助打扫农舍,这些牛只能打扫固定位置(千万要注意这个位置不是连续的),每一段区间必须至少有一只牛打扫,问你至少需要多少只牛?(如果区间不能完全被覆盖,则 ...
- Cleaning Shifts(POJ 2376 贪心)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15143 Accepted: 3875 ...
- bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 -- 贪心
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MB Description 一天有 ...
- 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开始!!! # ...
- POJ 2376 贪心
题意:FJ希望它的牛做一些清洁工作.有N只牛和T个时间段,每只牛可以承担一段时间内的工作.FJ希望让最小数量的牛覆盖整个T,求出其数量.若无法覆盖整个T,则输出-1. 分析:首先要注意T表示T个时间段 ...
- 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(贪心)
Cleaning Shifts Descriptions: 原文是English,我这就直接上Chinese了,想看原文的点一下链接哦 大表哥分配 N (1 <= N <= 25,000) ...
随机推荐
- 应用的入口——Startup
应用的入口——Startup 一个ASP.NET Core应用被启动之后就具有了针对请求的处理能力,而这个能力是由管道赋予的,所以应用的启动同时意味着管道的成功构建.由于管道是由注册的服务器和若干中间 ...
- C - AtCoDeerくんと選挙速報 / AtCoDeer and Election Report
ceil有毒啊..用ceil一直错. 思路就是模拟吧,设当前的答案是ansx和ansy. 如果比例是小于ansx的,那么就要乘以一个倍数k1,使得a * k1 >= ansx的. 所以就用cei ...
- 宝塔面板安装的mysql5.5用命令行kill -9后启动不了
1.查看mysql版本方法一:status;方法二:select version(); 2.Mysql启动.停止.重启常用命令a.启动方式1.使用 service 启动:[root@localhost ...
- iOS Runtime常用方法整理
关于runtime的学习网上有很多博客,在学习之前也查过很多资料,觉得南峰子老师博客中对 runtime 的讲解挺详细的,博客地址:http://southpeak.github.io/categor ...
- 13.JAVA-包package、import使用
1.包的定义 之前我们学习java时,生成的class文件都是位于当前目录中,假如出现了同名文件,则会出现文件覆盖问题,因此就需要设置不同的目录(定义包),来解决同名文件冲突问题. 并且在大型项目中, ...
- Appium基础二:Appium的安装(基Windows)
1.JAVA环境配置: 1.1安装jdk: 1.2配置JAVA_Home.Path配置.java验证 Path: 输入C:\Program Files\Java\jdk1.8.0_121\bin:C: ...
- JS根据选择的日期计算年龄
本例中用的是easyUI的datebox $('#cal_birthday').datebox({ onSelect: function(date){ //根据选则的日期计算年龄 //alert(da ...
- Sqoop基础学习(1)
1. Sqoop的导入过程 在开始导入之前,Sqoop会通过JDBC来获得所需要的数据库元数据 1.导入表的列名.数据类型等: 2.接着这些数据库的数据类型(varchar.number等)会把映射成 ...
- JPA + EclipseLink + SAP云平台 = 运行在云端的数据库应用
JPA(Java Persistence API)的实现Provider有Hibernate,OpenJPA和EclipseLink等等. 本文介绍如何通过JPA + Eclipse连接SAP云平台上 ...
- Android(java)学习笔记121:BroadcastReceiver之 自定义广播
广播使用: 电台:对外发送信号.---------电台发送广播(可以自定义) 收音机:接收电台的信号.-----广播接收者 这里,我们就说明自定 ...