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.
 
 
这个题目就是贪心,在开始时间能满足条件的情况下,让结束时间越晚就可以了。这样就需要对结构体进行排序,这里使用sort函数进行了排序。
 
 
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#define inf 0x3fffffff
#define eps 1e-10 using namespace std; struct node
{
int Begin, End;
}a[25005]; bool cmp(node x, node y)
{
if (x.Begin != y.Begin)
return x.Begin < y.Begin;
else
return x.End > y.End;
} int t, n; int qt()
{
int ans = 0;
int now, p, k = 0, Max;
now = 1;
while (now <= t)
{
if (k == n)
return -1;
if (a[k].Begin > now)
return -1;
Max = a[k].End;
while (a[k].Begin <= now && k < n)
{
Max = max(Max, a[k].End);
k++;
}
now = Max + 1;
ans++;
}
return ans;
} int main()
{
//freopen("test.txt", "r", stdin);
while (scanf("%d%d", &n, &t) != EOF)
{
for (int i = 0; i < n; ++i)
scanf("%d%d", &a[i].Begin, &a[i].End);
sort(a, a+n, cmp);
printf("%d\n", qt());
}
return 0;
}

ACM学习历程——POJ 2376 Cleaning Shifts(贪心)的更多相关文章

  1. POJ 2376 Cleaning Shifts 贪心

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

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

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

  3. poj 2376 Cleaning Shifts 贪心 区间问题

    <pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS   Memory ...

  4. POJ 2376 Cleaning Shifts (贪心,区间覆盖)

    题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...

  5. POJ 2376 Cleaning Shifts(轮班打扫)

    POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Farmer ...

  6. poj 2376 Cleaning Shifts

    http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  7. POJ 2376 Cleaning Shifts【贪心】

    POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...

  8. 【原创】poj ----- 2376 Cleaning Shifts 解题报告

    题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K ...

  9. poj 2376 Cleaning Shifts 最小区间覆盖

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40751   Accepted: 9871 ...

随机推荐

  1. jsp获取web.xml 里的配置项

    ServletContext servletContext = request.getSession().getServletContext();                String titl ...

  2. 自己编译GCC(compile gcc from source)

    有的时候,我不是第一次遇到这种时候,编译内核时报出编译器BUG.如果是ubuntu还好一点,默认软件仓库中就有好几个GCC,换一换总能找到一个好使的,实在不行还有个Tooltrain的ppa,但Deb ...

  3. java String概述

    class StringDemo { public static void main(String[] args)  {  String s1 = "abc";//s1 是一个类类 ...

  4. Win7获取管理权限修改Host文件以其他权限问题

    ——win7管理员权限修改Host文件——百度经验 win7管理员权限获取办法: win7管理员权限 获取办法——经验——百度 win7最高权限获取: win7最高权限 获取——百度经验

  5. Using ADO.NET Data Service

    ADO.NET Data Service是随同Visual Studio 2008 SP1提供的用于构建在数据对象模型 (如EF-DEMX, LINQ-DBML) 之时来快速提供企业网内外的轻量级数据 ...

  6. 公网yum 源地址

    1. centos5.*  公网yum 源地址 [root@web ~]# cd /etc/yum.repos.d/[root@web yum.repos.d]# wget -O /etc/yum.r ...

  7. npm install --save 、--save-dev 、-D、-S 的区别与NODE_ENV的配置

    https://blog.csdn.net/jwl_willon/article/details/81054978 1.npm install <=> npm i --save   < ...

  8. 【BZOJ3997】[TJOI2015]组合数学 最长反链

    [BZOJ3997][TJOI2015]组合数学 Description 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完.此对此问题变形,假设每个格 ...

  9. 九度OJ 1146:Flipping Pancake(翻饼子) (递归、游戏)

    时间限制:1 秒 内存限制:32 兆 特殊判题:是 提交:265 解决:116 题目描述: We start with a stack n of pancakes of distinct sizes. ...

  10. sed相关

    1 global flag sed 's/xxx/xxx/' inputfile,如果没有带global flag g的话,匹配替换的只是inputfile中的每一行的第一个匹配项.如果带了g的话,才 ...