题意
POJ2376 Cleaning Shifts 0x50「动态规划」例题
- 总时间限制:
- 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. - 样例输出
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
<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
分析
针对这题的贪心做法:
由于代价都是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
",f[tot]);
return 0;
}