题目地址:

http://poj.org/problem?id=2376

题目内容:

Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12226   Accepted: 3187

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.

Source

 
 
解题思路:
 
典型的区间贪心。
 
首先,我们需要根据线段的起点坐标来排序,小的在前面。
具体实现上,我们需要使用一个变量记住当前需要匹配的shift。所谓当前需要匹配的shift,举一个例子:
比如需要匹配100,那么在第一轮中,当前需要匹配的shift就是1。而选用1 20来匹配后,当前需要匹配的shift就变为21。
 
遍历排序后的数组,如果能够匹配当前点,记录下终点;如果不能够匹配当前点,就取出最长的终点,作为下一个当前需要匹配的shift。
 
具体代码:
#include <algorithm>
#include <cstdio>
using namespace std; int n,t;
struct cow
{
int start;
int fin;
};
cow whole[]; bool cmp(const cow& a, const cow& b)
{
return a.start < b.start;
} int main(void)
{
scanf("%d%d", &n, &t);
for (int i = ; i < n; i ++) {
int t1,t2;
scanf("%d%d", &t1, &t2);
whole[i].start = t1;
whole[i].fin = t2;
}
sort(whole, whole + n, cmp);
int top = ;
int max_length = ;
int res = ;
for (int i = ; i < n; i ++) { if (whole[i].start > top) {
if (max_length == ) {
res = -;
break;
} else {
res ++;
top = max_length + ;
max_length = ;
if (top > t)
break;
}
}
if (whole[i].start <= top && whole[i].fin >= top && max_length < whole[i].fin) {
max_length = whole[i].fin;
}
//printf("now is %d, top is %d, max is %d\n", i, top, max_length);
}
if (max_length != ) {
res ++;
top = max_length + ;
}
if (top <= t)
res = -;
printf("%d\n", res);
}

【原创】poj ----- 2376 Cleaning Shifts 解题报告的更多相关文章

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

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

  2. poj 2376 Cleaning Shifts

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

  3. POJ 2376 Cleaning Shifts 贪心

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

  4. POJ 2376 Cleaning Shifts【贪心】

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

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

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

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

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

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

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

  8. poj 2376 Cleaning Shifts(贪心)

    Description Farmer John <= N <= ,) cows to <= T <= ,,), the first being shift and the la ...

  9. ACM学习历程——POJ 2376 Cleaning Shifts(贪心)

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

随机推荐

  1. CorePlot学习

    阅读这篇文章,指出它在国外    原文地址:https://github.com/core-plot/core-plot/wiki/High-Level-Design-Overview 强烈推荐阅读该 ...

  2. ADO异步查询显示进度条

    一般,ADO都是以同步的方式来处理数据.这就是说,当ADO开始处理数据后,应用程序必须等到ADO处理完毕之后才可以继续执行.但是除了同步执行方式之外,ADO也提供了异步执行的方式,允许当ADO处理时, ...

  3. Delphi动态申请数组内存的方法(不使用SetLength,采用和C相似的方式)

    procedure TForm1.Button1Click(Sender: TObject);type  TArr = array [0..0] of Integer;  PArr = ^TArr;v ...

  4. vc2008构建和使用libcurl静态库

    1>下载CURL源代码curl-7.26.0.zip 2>用VC2008/2005打开工程curl-7.26.0\lib\libcurl.vcproj,转换下工程并构建,可以直接编译成功! ...

  5. 普联的路由器TL-WR842N和TL-WR845N还有 TL-WR847N哪一个更好,我是家用

    http://zhidao.baidu.com/question/525244706.html?fr=ala&device=mobile&ssid=0&from=844b&am ...

  6. 利用未公开API获取终端会话闲置时间(Idle Time)和登入时间(Logon Time)

    利用未公开API获取终端会话闲置时间(Idle Time)和登入时间(Logon Time)作者:Tuuzed(土仔)   发表于:2008年3月3日23:12:38 版权声明:可以任意转载,转载时请 ...

  7. 李兴华JavaWeb开发笔记

    李兴华JavaWeb开发笔记 1.Java语法-基础 环境变量-JAVA_HOME, PATH, ClassPath 变量名 作用 举例 JAVA_HOME 指向JDK目录 C:\Program Fi ...

  8. 深入应用看本质之-ICMP(1)

    在网络层的学习时我们easy忽略IP的一个字段--存活时间 以下是百度上的解释 (8)生存时间 占8位,生存时间字段经常使用的的英文缩写是TTL(Time To Live),表明是数据报在网络中的寿命 ...

  9. Linux命令之rz - 批量上传文件,简单易用(转载)

    用途说明 rz命令能够批量上传文件,当然也可上传单个文件啦.使用的协议是古老的ZMODEM协议,尽管协议古老,但毫不影响的简单易用的特性.一般情 况我们要上传文件到Linux系统,要么使用ftp(还得 ...

  10. Spring中继承配置的注入方法

    (1)两个java类.一个父类一个字类 package com.lc.inherit; /* * 这里是父类 */ public class Student { protected String na ...