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. ms sqlserver 清除数据库日志脚本

    USE [master] GO ALTER DATABASE F360DW SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE F360DW SET ...

  2. 去除inline-block间隙的几种方法

    为什么会产生间隙? 由于编写代码时的美观和可读性,在代码中添加回车或空格而产生的间隙. html代码: <ul class="container"> <li> ...

  3. mui对话框、表单

    1.mui.alert() 普通提醒参数 1.message Type: String 提示对话框上显示的内容 2.title Type: String 提示对话框上显示的标题 3.btnValue ...

  4. 织梦后台上传mp4视频不显示

    include/dialog/select_media.php ,约185行, 把(rm|rmvb) 改为(rm|mp4|rmvb)

  5. BaseAdapter获取View之三重境界

    在BaseAdapter获取View之前,BaseAdapter需要与数据源相关联. 可以使用构造方法: private List<ItemBean> baseListItems; pri ...

  6. 升级CentOS内核 - 2.6升级到3.10/最新内核

    ##记得切换到root用户执行升级操作. [root@localhost ~]# uname -a ##旧版 Linux localhost.localdomain 2.6.32-279.el6.i6 ...

  7. Java 反射机制(一)

    阅读<Core Java Volume I --- Fundamentals>反射部分,总觉得许多概念艰涩难懂.模棱两可.我想造成这个结果的主要原因可能是Cay S. Horstmann和 ...

  8. tsung基准测试方法、理解tsung.xml配置文件、tsung统计报告简介

    网上搜集的资料,资料来源于:http://blog.sina.com.cn/ishouke 1.tsung基准测试方法 https://pan.baidu.com/s/1Ne3FYo8XyelnJy8 ...

  9. 【LeetCode】9 Palindrome Number 回文数判定

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  10. C、C++、C#中struct的简单比较

    比较这三者是因为C.C++.C#这三者存在着一定的血缘关系,而他们三者都有的struct却有很大的不同. 功能 C中的struct是最简单的.只能有成员变量而不能有成员函数. C++和C#中都支持,而 ...