Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 31194   Accepted: 7677

Description

Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and 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

* Line 1: Two space-separated integers: N and T

* 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

* Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

Sample Input

3 10
1 7
3 6
6 10

Sample Output

2

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

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 (贪心题)的更多相关文章

  1. Greedy:Cleaning Shifts(POJ 2376)

      牛的大扫除 题目大意:农夫有N只牛,这些牛要帮助打扫农舍,这些牛只能打扫固定位置(千万要注意这个位置不是连续的),每一段区间必须至少有一只牛打扫,问你至少需要多少只牛?(如果区间不能完全被覆盖,则 ...

  2. Cleaning Shifts(POJ 2376 贪心)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15143   Accepted: 3875 ...

  3. bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 -- 贪心

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MB Description     一天有 ...

  4. poj-2376 Cleaning Shifts (排序+贪心)

    http://poj.org/problem?id=2376 john有n头牛做打扫工作,他想在t时间内每个时间都至少有一头牛在做打扫工作,第一头牛在1,最后一头牛在t时间,每一头牛工作都有一个开始时 ...

  5. poj2376 Cleaning Shifts(区间贪心,理解题意)

    https://vjudge.net/problem/POJ-2376 题意理解错了!!真是要仔细看题啊!! 看了poj的discuss才发现,如果前一头牛截止到3,那么下一头牛可以从4开始!!! # ...

  6. POJ 2376 贪心

    题意:FJ希望它的牛做一些清洁工作.有N只牛和T个时间段,每只牛可以承担一段时间内的工作.FJ希望让最小数量的牛覆盖整个T,求出其数量.若无法覆盖整个T,则输出-1. 分析:首先要注意T表示T个时间段 ...

  7. POJ 2376 Cleaning Shifts 贪心

    Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...

  8. POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)

    Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...

  9. 【POJ - 2376】Cleaning Shifts(贪心)

    Cleaning Shifts Descriptions: 原文是English,我这就直接上Chinese了,想看原文的点一下链接哦 大表哥分配 N (1 <= N <= 25,000) ...

随机推荐

  1. is null 的优化

    bk1gx7xwj9du6SELECT *  FROM HEADERS GJH WHERE ATTRIBUTE10 IS NULL FOR UPDATE 优化建议如下: 短期来说:建立一个索引来优化: ...

  2. mui的picker组件填坑

    在公司项目用到mui,vue进行开发 import mui from './assets/js/mui.min.js'后 加window.mui = mui再 import './assets/js/ ...

  3. Unity注入

    [此文引用别人,作为随笔自己看.]今天写<WCF技术剖析(卷2)>关于<WCF扩展>一章,举了“如何通过WCF扩展实现与IoC框架(以Unity为例)集成”(<通过自定义 ...

  4. It is not the destination so much as the journey, they say.

    It is not the destination so much as the journey, they say. 人家说目的地不重要,重要的是旅行的过程.<加勒比海盗>

  5. 【css】css2实现两列三列布局的方法

    前言 对于 flex 弹性布局相信大家都有所了解,它是 css3 中的属性,然而它具有一定的兼容性问题.楼主前几天面试时遇到了面试官需要设计一个两列布局,我当然就说父元素 flex 吧哩吧啦,然而需要 ...

  6. css 02

    Css 02 Url  ./  http://www. Src  引入 拿取过来内容 Href 引用 连接前往 a  link 现在所有的命名 请按照下面我说的去命名 可以使用字母 数字 下划线组成  ...

  7. 织梦ckeditor编辑器 通过修改js去除img标签内的width和height样式

    1. 文件\include\ckeditor\plugins\image\dialogs\image.js 2. 使用工具美化js代码 3. 搜索 setStyle('width', CKEDITOR ...

  8. [nmon]使用nmon工具监控系统资源

    1.下载nmon 下载正确的nmon版本, 查看linux服务器版本,命令:lsb_release -a,查看到当前系统为RedHat 6.4 然后我们根据我们的linux版本,下载相应nmon版本, ...

  9. Netweaver和CloudFoundry的服务器日志

    Netweaver 事务码SMICM,Goto->HTTP Plug-In->Server Logs: CloudFoundry 假设我部署本地应用到CloudFoundry之后,应用的状 ...

  10. python_99_面向对象多态

    #多态:一种接口,多种实现.主要作用:实现接口重用 #方法1: class Animal(object): def __init__(self,name): self.name=name class ...