Cleaning Shifts

题目连接:

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

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

题意

给你n个区间,要求你选出最少的区间,使得能够覆盖[1,t]

题解:

dp很简单,dp[i] = min(dp[j])+1,其中需满足a[j].r+1>=a[i].l

贪心也很简单,每次我们扫的时候,直接扫到能够转移到的最右边就好了(其实也是DP思想..

期末考试前写道水题攒攒RP T T

代码

#include<algorithm>
#include<iostream>
#include<stdio.h>
using namespace std; struct node
{
int x,y;
};
bool cmp(node a,node b)
{
if(a.x==b.x)return a.y>b.y;
return a.x<b.x;
}
node p[25005]; int main()
{
int n,t;
scanf("%d%d",&n,&t);
for(int i=1;i<=n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
sort(p+1,p+1+n,cmp);
int l = 0;
int ans = 0;
int i = 1;
while(i<=n)
{
if(p[i].x>l+1)return puts("-1");
int tmp = l;
while(i<=n&&p[i].x<=l+1)
{
tmp = max(tmp,p[i].y);
i++;
}
l = tmp;ans++;
if(l>=t)return printf("%d\n",ans);
}
return puts("-1");
}

POJ 2376 Cleaning Shifts 贪心的更多相关文章

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

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

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

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

  3. POJ 2376 Cleaning Shifts (贪心,区间覆盖)

    题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...

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

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

  5. poj 2376 Cleaning Shifts

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

  6. POJ 2376 Cleaning Shifts【贪心】

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

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

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

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

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

  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. nmon基础

    nmon是分析 AIX 和 Linux 性能的免费工具 最简单的安装方式(Ubuntu apt源) sudo apt-get install nmon 在terminal下打开nmon 敲CMD,出现 ...

  2. Embedded之memory type

    1 Types of memory 2 Characteristics

  3. 《学习OpenCV》练习题第四章第二题

    #include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...

  4. HDOJ-ACM1009(JAVA) (传说中的贪心算法)分为数组实现 和 封装类实现

    转载声明:原文转自:http://www.cnblogs.com/xiezie/p/5564311.html 这个道题有几点要注意的: 数组存放的类型:float或double 打印的格式:(如果只是 ...

  5. 在mac上搭建python环境

    原文出处:http://blog.justbilt.com/2014/07/02/setup_python_on_mac/ 这两天重新搞了下python的环境,发现好多地方还是容易忘记,因此有了这篇文 ...

  6. navigationController 之间的切换

    项目要实现从一个Navigation 下push出的第N层controller后 立即切换到另一个 Navigation下 例如:在微信的通讯录Nav中选择一个好友,进入好友的详细资料,点击发消息按钮 ...

  7. Ruiy自我识人做事领悟录ing

    是坑总需要人去踩,谁踩谁收获! 做人做事分层分次,后方能至始及终不乱; 做人做事切记诚记信,宁他人负我,我定不负他人! 做人做事做力求清心寡欲; 安静做工,沉静学道;

  8. 应用引擎BAE3.0(转)

    add by zhj: 其实我主要是想看看基于docker的PaaS的特性. 原文:http://developer.baidu.com/wiki/index.php?title=docs/cplat ...

  9. codeforces 644A Parliament of Berland

    A. Parliament of Berland time limit per test 1 second memory limit per test 256 megabytes input stan ...

  10. HDU4513吉哥系列故事――完美队形II(manacher算法)

    这个比最长回文子串就多了一个条件,就是回文字串(这里相当于人的高度)由两端向中间递增. 才刚刚看了看manacher,在用模板A了一道题后,还没有完全理解manacher,然后就准备把这道题也直接带模 ...