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

Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12604   Accepted: 3263

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.

 
 
分析:
题意就是给你N段小区间 [n , m]和一个T,T代表大区间[1 ,T] , 要求你找出最少使用小区间完全覆盖大区间。
一开始一直WA,因为理解错了“完全覆盖”的概念。即是下面的数据:
      10      //3代表小区间个数,10代表大区间长度。
    
    
    

上面的数据我的程序输出的是:-1

因为有两个区间没有“完全覆盖”,[5 ,6]和[8 ,9]。

但是正确答案是 3 。

WA代码:

 #include<cstdio>
#include<algorithm>
using namespace std;
struct P
{
int x,y;
bool operator < (const P & p) const
{
return x < p.x || (x == p.x && y > p.y);
}
}a[]; int main()
{
int n,t;
while(~scanf("%d %d",&n,&t))
{
for(int i = ;i < n;i++)
scanf("%d %d",&a[i].x,&a[i].y);
sort(a ,a + n);
int res = ,s;
if(a[].x > )
printf("-1\n");
else
{
s = a[].y;
for(int i = ;i < n && s < t;)
{
int tmp = ;
while(i < n && a[i].x <= s)
{
tmp = max(tmp , a[i].y);
i++;
}
if(tmp > s)
{
s = tmp;
res++;
}
else
break;
}
}
if(s >= t)
printf("%d\n",res);
else
printf("-1\n");
}
return ;
}

AC代码:

 #include<cstdio>
#include<algorithm>
using namespace std; struct P{
int x,y;
bool operator < (const P & p) const {
return x < p.x || (x == p.x && y > p.y);
}
}a[]; int main() {
int n,t;
while(~scanf("%d %d",&n,&t)) {
for(int i = ;i < n;i++)
scanf("%d %d",&a[i].x,&a[i].y); sort(a ,a + n); int res = ,s;
if(a[].x > ) {
printf("-1\n");
continue;
} else {
s = a[].y;
for(int i = ;i < n && s < t;) {
int tmp = ;
while(i < n && a[i].x <= s + ) {
tmp = max(tmp , a[i].y);
i++;
}
if(tmp > s) {
s = tmp;
res++;
} else break;
}
} if(s >= t) {
printf("%d\n",res);
} else {
printf("-1\n");
}
}
return ;
}

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 贪心

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

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

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

  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. iOS ---不一样的NSLog打印(精准打印)

    在iOS开发过程中,调试是很重要的过程,而除了各种断点调试(普通断点.条件断点.全局断点)之外,似乎NSLog是我们调试最常用的方法,当然,也是最简单朴素的寻debug方法. 在项目中,我们常使用的N ...

  2. NOI2015 题解

    [NOI2015]程序自动分析 离散化+并查集. [NOI2015]软件包管理器 [Noi2015]寿司晚宴 [Noi2015]荷马史诗 [NOI2015]品酒大会 [Noi2015]小园丁与老司机

  3. A star 寻路

    大白话说一下几个点: 通俗的来说,其实就是以一个规则来 从A点走到B点. 怎么来判断我们走的格子是一个合适的格子? 就是靠一个规则来计算,这个规则就是估价函数. 估价函数: 常用:曼哈顿算法 F = ...

  4. ZeroMQ接口函数之 :zmq_msg_init - 初始化一个空的ZMQ消息结构

    ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq_msg_init zmq_msg_init(3) ØMQ Manual - ØMQ/3.2.5 Name zmq_ ...

  5. ZeroMQ接口函数之 :zmq_msg_move - 将一个消息里面的内容移动到另一个消息里面

    ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq_msg_move zmq_msg_move(3)   ØMQ Manual - ØMQ/3.2.5 Name zm ...

  6. table常用功能总结

    1,设置表格边框为单线框 table, th, td { border: 1px solid blue; }加上:table { border-collapse:collapse; } 由于 tabl ...

  7. python endswith和startwith

    转载:http://blog.sina.com.cn/s/blog_5dd2af0901012rmn.html 做文本处理的时候经常要判断一个文本有没有以一个子串开始,或者结束.Python为此提供了 ...

  8. python学习 第一天

    正式学习python第一天,网上找到了python教程,带练习题的,又装了ubuntu. 这是学习笔记: list[]: 可变,append/1,insert/2,pop/index? tuple() ...

  9. 如何完全卸载oracle11g?

    步骤一: 停止Oracle的所有服务. 步骤二: 运行%oracle_home%\app\Administrator\product\11.2.0\dbhome_1\deinstall \deinst ...

  10. JDBC编程

    简单地说,JDBC 可做三件事:与数据库建立连接.发送 SQL 语句并处理结果.下列代码段给出了以上三步的基本示例: Connection con = DriverManager.getConnect ...