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. NOIP2008 ISBN号码(一桶水)【A005】

    [A005]NOIP2008 ISBN号码(一大桶水)[难度A]———————————————————————————————————————————————————————————————————— ...

  2. BACKLOG

    团队: 郭志豪:http://www.cnblogs.com/gzh13692021053/ 杨子健:http://www.cnblogs.com/yzj666/ 刘森松:http://www.cnb ...

  3. poj2689Prime Distance 素数测试

    朴素素数测试是O(x1/2)的,每一个数都测试下来就炸了 然而如果全部预处理的话才是做大死,时间空间各种炸(大约有1亿个数) 所以怎么平衡一下呢? 其实在预处理的时候可以只处理一半:把21474836 ...

  4. ZeroMQ接口函数之 :zmq_msg_recv - 从一个socket中接受一个消息帧

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

  5. UI测试测试分析

    解析:jQuery easyUI是基于jQuery框架在使用之前应该先引入jquery框架否则jQuery easyUI 将失效故D错误 解析: JQuery UI下的menu插件的使用,menu提供 ...

  6. IBatis按条件分页查询

    XML中代码  <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMap PUBLIC & ...

  7. Ubuntu 14.04 安装pdf阅读器

    1. 个人推荐 okular. 关于安装okular的原因,可以很好的做到护眼功能. Ubuntu 14.04 自带的阅读器,因为白色太刺眼,长时间使用对眼睛不好. 对于,长时间编程的朋友们习惯夜间模 ...

  8. Android 四大护法之一 Service

    1.Service的概念 Service是Android 四大组件之一,是默认没有界面的运行于后台的服务程序.Service的开启方式分为启动式服务(startService)和绑定式服务(bindS ...

  9. 如何获取google地图、baidu百度地图的坐标

    google:打开google地图-->查找目的地-->右键:此位置居中-->地址栏键入javascript:void(prompt('',gApplication.getMap() ...

  10. http详解

    mac地址是网卡出厂的时候给定的固定地址.ip是当前节点被分配的地址,mac和ip相互配对: ip间通信- http本身是无状态无连接的,因为有这样的特性才能让他适用于当今这么复杂的网络环境. 但是有 ...