题目地址:

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. ajax文本空输入显示用户信息

    一般文件代码 public void ProcessRequest (HttpContext context) { //获取主见值 string s = context.Request["u ...

  2. 调整Tomcat的并发线程到5000+

    调整Tomcat的并发线程数到5000+ 1. 调整server.xml的配置 先调整maxThreads的数值,在未调整任何参数之前,默认的并发线程可以达到40. 调整此项后可以达到1800左右. ...

  3. 解决xShell4某些情况下按删除键会输出^H的问题

    当我们用Xshell登录进入linux后,在普通模式下,对输入进行删除等操作没有问题. 而在执行中,按delete,backspace键时会产生^H等乱码问题. 这是由于编码不匹配的问题. 解决方法: ...

  4. java matlab混合编程之返回值Struct类型

    java matlab混合编程的时候当返回值是Struct类型(matlab中的返回类型)如何来取得(java中)其值? 上网找,看到这个网页:http://www.mathworks.cn/cn/h ...

  5. Android中使用SurfaceView和Canvas来绘制动画

    事实上每一个View中都有Canvas能够用来绘制动画.仅仅须要在这个View中重载onDraw()方法就能够,可是SurfaceView类是一个专门用来制动动画的类. Canvas(中文叫做&quo ...

  6. NetBeans工具学习之道:NetBeans的(默认)快捷键

    没什么好介绍的,是netbeans的快捷键,比較全面.看到好多坛子里还在问eclipse下的这个快捷键怎么netbeans下没有呢.曾经收集的,如今列在以下: 事实上,在当前安装的netbeans的 ...

  7. HTML5 Canvas动画效果实现原理

    在线演示 使用HTML5画布可以帮助我们高速实现简单的动画效果.基本原理例如以下: 每隔一定时间绘制图形而且清除图形,用来模拟出一个动画过程,能够使用context.clearRect(0, 0, x ...

  8. 利用linux BT5来破解无线 破解无线

    下面是自己整理的详细步骤,版权小冯全部. 一.提前准备好bt5的ISO镜像文件.和虚拟机,提前把虚拟机安装好.然后进行安装bt5. 二.进入页面,点击statx.进入可视化界面. 三.进入主界面后.下 ...

  9. SQLServer 复制中移除和加入公布而不初始化全部项目

    -- 若提前"禁止架构更改".新增的列不会自己主动加入大公布.此时应使用 sp_articlecolumn 加入列 EXEC sp_changepublication @publi ...

  10. java 线程 ProducerAndConsumer

    package j2se.thread.demo; /** * <p>Project:J2SE 的基础知识</p> * <p>Tile:多线程模拟 生产者 和 消费 ...