题意

POJ2376 Cleaning Shifts 0x50「动态规划」例题

http://bailian.openjudge.cn/practice/2376

总时间限制:
1000ms
内存限制:
65536kB
描述
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.
			<dt>输入</dt>
<dd>* Line 1: Two space-separated integers: N and T<br><br>* 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.</dd>
<dt>输出</dt>
<dd>* 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.</dd>
<dt>样例输入</dt>
<dd><pre>3 10

1 7

3 6

6 10

样例输出
2
提示
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.
来源
USACO 2004 December Silver

分析

针对这题的贪心做法:

由于代价都是1,贪心策略是从左往右,尽量选择长度最大的区间。

首先对所有奶牛排序,按照开始时间排序。

然后更新起点=终点+1,搜索剩下的奶牛中能够覆盖这个起点同时终点最远的那一头,更新终点。

也可以考虑dp,设f[x]表示覆盖[1,x]的最小代价,那么把区间按照r排序,每次用线段树维护更新即可。

时间复杂度\(O(n \log n)\),这种做法可以轻松的处理 POJ3171 Cleaning Shifts 这道同名题。

代码

#include<iostream>
#include<cstring>
#include<algorithm>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std; co int N=1e5+1,INF=0x3f3f3f3f;
int n,m,f[N],b[N],tot;
struct T{
int l,r,x;
bool operator<(co T&w)co {return r<w.r;}
}a[N],t[N*4];
#define lc (p<<1)
#define rc (p<<1|1)
void build(int p,int l,int r){
t[p].l=l,t[p].r=r,t[p].x=l?INF:0;
if(l==r) return;
int mid=l+r>>1;
build(lc,l,mid),build(rc,mid+1,r);
}
void change(int p,int x,int y){
if(t[p].l==t[p].r) return t[p].x=y,void();
int mid=t[p].l+t[p].r>>1;
if(x<=mid) change(lc,x,y);
else change(rc,x,y);
t[p].x=min(t[lc].x,t[rc].x);
}
int ask(int p,int l,int r){
if(l<=t[p].l&&t[p].r<=r) return t[p].x;
int mid=t[p].l+t[p].r>>1;
if(r<=mid) return ask(lc,l,r);
if(l>mid) return ask(rc,l,r);
return min(ask(lc,l,r),ask(rc,l,r));
}
int main(){
read(n),read(m);
b[++tot]=1;
for(int i=1;i<=n;++i){
read(a[i].l),read(a[i].r);
b[++tot]=a[i].l,b[++tot]=a[i].l+1;
b[++tot]=a[i].r,b[++tot]=a[i].r+1;
}
b[++tot]=m;
sort(b+1,b+tot+1),tot=unique(b+1,b+tot+1)-b-1;
while(b[tot]>m) --tot;
sort(a+1,a+n+1);
build(1,0,tot);
memset(f,0x3f,sizeof f);
f[0]=0;
for(int i=1;i<=n;++i){
a[i].l=lower_bound(b+1,b+tot+1,a[i].l)-b;
a[i].r=lower_bound(b+1,b+tot+1,a[i].r)-b;
int num=ask(1,a[i].l-1,a[i].r-1)+1;
if(f[a[i].r]>num)
f[a[i].r]=num,change(1,a[i].r,f[a[i].r]);
}
if(f[tot]==INF) puts("-1");
else printf("%d\n",f[tot]);
return 0;
}

POJ2376 Cleaning Shifts的更多相关文章

  1. poj2376 Cleaning Shifts【线段树】【DP】

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32561   Accepted: 7972 ...

  2. POJ2376 Cleaning Shifts 【贪心】

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11542   Accepted: 3004 ...

  3. poj2376 Cleaning Shifts(区间贪心,理解题意)

    https://vjudge.net/problem/POJ-2376 题意理解错了!!真是要仔细看题啊!! 看了poj的discuss才发现,如果前一头牛截止到3,那么下一头牛可以从4开始!!! # ...

  4. poj-2376 Cleaning Shifts (排序+贪心)

    http://poj.org/problem?id=2376 john有n头牛做打扫工作,他想在t时间内每个时间都至少有一头牛在做打扫工作,第一头牛在1,最后一头牛在t时间,每一头牛工作都有一个开始时 ...

  5. poj2376 Cleaning Shifts 区间贪心

    题目大意: (不说牛了) 给出n个区间,选出个数最少的区间来覆盖区间[1,t].n,t都是给出的. 题目中默认情况是[1,x],[x+1,t]也是可以的.也就是两个相邻的区间之间可以是小区间的右端与大 ...

  6. 【POJ - 2376】Cleaning Shifts(贪心)

    Cleaning Shifts Descriptions: 原文是English,我这就直接上Chinese了,想看原文的点一下链接哦 大表哥分配 N (1 <= N <= 25,000) ...

  7. 【BZOJ1672】[Usaco2005 Dec]Cleaning Shifts 清理牛棚 动态规划

    [BZOJ1672][Usaco2005 Dec]Cleaning Shifts Description Farmer John's cows, pampered since birth, have ...

  8. poj 2376 Cleaning Shifts

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

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

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

随机推荐

  1. 【HNOI 2018】游戏

    Problem Description 一次小 \(G\) 和小 \(H\) 在玩寻宝游戏,有 \(n\) 个房间排成一列,编号为 \(1,2,-,n\),相邻房间之间都有 \(1\) 道门.其中一部 ...

  2. 力扣(LeetCode) 821. 字符的最短距离

    给定一个字符串 S 和一个字符 C.返回一个代表字符串 S 中每个字符到字符串 S 中的字符 C 的最短距离的数组. 示例 1: 输入: S = "loveleetcode", C ...

  3. 大数据新手之路一:安装JDK

    Ubuntu16.04 1.下载jdk-8u192-linux-x64.tar.gz https://www.oracle.com/technetwork/java/javase/downloads/ ...

  4. RDP服务开启

    一.RDP协议概述 远程桌面协议(RDP, Remote Desktop Protocol)是一个多通道(multi-channel)的协议,让用户(客户端或称“本地电脑”)连上提供微软终端机服务的电 ...

  5. js基本类型存放和对象存放的区别(对象遍历)

    js的基本类型,对象类型的应用在初学的时候,需要自己加以明确,明确了数据类型,在使用过程中才能正确使用变量.如下两个例子是摘自初学时的笔记,为大家提供参考. 1.对象可以存放属性和方法,js基本类型不 ...

  6. js中用户名的正则(字符,数字,下划线,减号)

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...

  7. 1)selenium+ java集成,待深度项目流程应用

    selenium 1,selenium ide mac 安装 打开firefox浏览器,进入下面网址https://addons.mozilla.org/en-US/firefox/addon/sel ...

  8. Python3+PyCharm+Django+Django REST framework开发教程

    一.说明 自己一是想跟上潮流二是习惯于直接干三是没有人可以请教,由于这三点经常搞得要死要活.之前只简单看过没写过Diango,没看过Django REST framework,今天一步到位直接上又撞上 ...

  9. 关于vs调用数据库存储过程 返回输出参数的一些总结

    1.直接上练习的存储过程,方便回想 create proc proc_output @totlecount int output, @pageIndex int, @pageSize intas de ...

  10. Python3版本中的filter函数,map函数和reduce函数

    一.filter函数: filter()为已知的序列的每个元素调用给定的布尔函数,调用中,返回值为非零的元素将被添加至一个列表中 def f1(x): if x>20: return True ...