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. (转) cocos 里面scrollView一些方法

    void setBounceEnabled (bool enabled)设置当滚动到边界时,是否内部容器发生弹回(bounce)效果 bool isBounceEnabled () const获取边界 ...

  2. 举例实用详解sc.textFile()和wholeTextFiles()

    谈清楚区别,说明白道理,从案例开始: 1 数据准备 用hdfs存放数据,且结合的hue上传准备的数据,我的hue截图: 每个文件下的数据: 以上是3个文件的数据,每一行用英文下的空格隔开: 2 测试 ...

  3. 《javascript设计模式》笔记之第十章 和 第十一章:门面模式和适配器模式

    第十章:门面模式 一:门面模式的作用 简化已有的api,使其更加容易使用 解决浏览器的兼容问题 二:门面模式的本质 门面模式的本质就是包装已有的api来简化操作   三:门面模式的两个简单例子 下面这 ...

  4. Eclipse Debug模式和断点调试

    1行号上双击,打断点:再双击,取消断点.一般想调试哪一句代码,就在哪一句和下一句打上断点. 2在要执行的class文件上(有main方法的),右键--Debug As 然后程序正常走,当走到断点时,会 ...

  5. 表单和HTML5

    1.form表单 <form action="" method=""> </form> action: 规定当提交表单时,向何处发送表单 ...

  6. AngularJS(八):http服务

    本文也同步发表在我的公众号“我的天空” http服务 之前我们的示例都是在本地获取模拟数据,在实际应用中,所有的项目都将不可避免的从后台获取数据,我们都是通过Ajax来实现与服务器的通信.在Angul ...

  7. android :fragmentation使用中遇到的 NullPointerException

    背景:fragmentation(单ACTIVITY+多个fragments)+brvah(  recyclerView多级自定义菜单功能) 目的:实现  菜单栏的点击,fragment 显示相应的内 ...

  8. C# 报表和打印等

    说到报表打印.那就不得不说需要查数据库了,然后填写报表信息.设计报表用的 grid++. 查数据库时候,我也是醉了,直接一个表自身与自身级联了4次...一共取了7个表的信息数据. 关于级联--(表字段 ...

  9. IOS制作纯色背景

    // 生成纯色背景图- (UIImage *)createPureColorImageWithColor:(UIColor *)color alpha:(CGFloat)alpha size:(CGS ...

  10. PostgreSQL缓存

    目录[-] pg_buffercache pgfincore pg_prewarm dstat Linux ftools 使用pg_prewarm预加载关系/索引: pgfincore 输出: 怎样刷 ...