传送门:http://poj.org/problem?id=2376

Cleaning Shifts

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 27272 Accepted: 6724

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.


解题心得:

  1. 题意就是让你选择的最少的线段将整个区间完全覆盖,如果不能完全覆盖输出-1。
  2. 可以使用贪心的思想,首先按照起始时间排序,如果起始时间相同,让结束时间越长的排在越前面,每次找一个线段,然后找起点在线段内,但是终点要更远的线段作为新的终点,维护远的终点,如果起点超出了当前选择的线段答案就+1。
  3. 然后就是判断是否出现无线段覆盖的地方,这个就需要判断答案+1的地方和开始选择的第一个线段是否是从1开始的,主要是注意一下边界问题。

#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 25000;
int n,t; struct The_cow{
int s,e;
bool operator < (const The_cow &a) const {
if(a.s == s)
return a.e < e;
return a.s > s;
}
}cow[maxn]; int main() {
while(scanf("%d%d",&n,&t) != EOF) {
memset(cow,0,sizeof(cow));
for(int i=0;i<n;i++){
scanf("%d%d",&cow[i].s,&cow[i].e);
if(cow[i].s > cow[i].e)
swap(cow[i].s,cow[i].e);
} sort(cow,cow+n);
if(cow[0].s != 1) {
printf("-1\n");
continue;
}
int ans = 1,pos_end = 0,temp_end = 0;//temp_end为当前记录的终点,pos_end是延伸出去的最长终点
temp_end = cow[0].e;
for(int i=1;i<n;i++){
if(cow[i].s > temp_end + 1){//为啥不加等号,因为在temp_end+1的位置的起点也可以用来更新pos_end
temp_end = pos_end;
ans++;
}
if(cow[i].s <= temp_end + 1) {
if(cow[i].e > pos_end)
pos_end = cow[i].e;
if(cow[i].e == t) {
ans ++;
temp_end = t;
break;
}
}
}
if(temp_end == t)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}

POJ:2367-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 解题报告

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

  5. poj 3171 Cleaning Shifts(区间的最小覆盖价值)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2743   Accepted: 955 De ...

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

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

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

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

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

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

  9. poj 2376 Cleaning Shifts(贪心)

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

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

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

随机推荐

  1. Vue.js - Day3

    定义Vue组件 什么是组件: 组件的出现,就是为了拆分Vue实例的代码量的,能够让我们以不同的组件,来划分不同的功能模块,将来我们需要什么样的功能,就可以去调用对应的组件即可: 组件化和模块化的不同: ...

  2. SpringCloud的学习记录(5)

    这一章节讲如何使用ribbon和hystrix. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Arti ...

  3. Azure进阶攻略 | 该如何唤醒你?因内核超时而沉睡的Linux虚拟机!

    周五下午,当你收拾好东西准备下班,奔赴 Happy Hour 时,突然接到开发团队的电话: 对方:伙计救命啊,我在搭建开发环境,但 Azure 上的 Linux 虚拟机无法启动! 你(心里想着:你要加 ...

  4. simotion读写CF卡,保存/读取变量

    simotion读写CF卡功能 1 使用西门子的Simotion运动控制器时,有时需要用到 读/写 CF卡的功能.主要来自以下几个方面的需求. 1)用户数据量较大,可保持(retain)存储区的容量不 ...

  5. 第三章 八位数字开关板&模拟输入板&火焰传感器

    这节我将带大家了解亮宁机器人基础外接硬件. 八位数字板开关 接线方法:W1~W8接23~37号数字端口,Enter接39号数字端口,vcc和gnd分别接正负. #include <LNDZ.h& ...

  6. RPC电源监控总结

    首先说一下监控机箱的监控原理. 设备的信息传输是通过tcp或者udp传输十六进制的数然后进行解析,传输数据. 如图: 设备反馈信息也是返回来的十六机制,然后按照对应的位置进将数据解析成二进制,用二进制 ...

  7. IOS 封装View的fram(X Y W H )

    @interface UIView (Extension) @property (nonatomic, assign) CGFloat x; @property (nonatomic, assign) ...

  8. vue.js--基础 v-bind绑定属性使用

    背景:因为10月要休产假了,8月的时间我工作很少,因为最开始做平台我一直做的是后端,前端很少接触,所以现在有时间,就学习前端基础,前端使用的vue.js+element,因为没有基础,所以下了一个视频 ...

  9. BZOJ 4247 挂饰 01背包

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4247 JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将其中的一些装在手机 ...

  10. 20145238-荆玉茗 《Java程序设计》第6周学习总结

    20145238 <Java程序设计>第6周学习总结 教材学习内容总结 第十章输入和输出 10.1.1 ·如果要将数据从来源中取出,可以使用输入串流,若将数据写入目的地,可以使用输出串流. ...