您的位置:首页 > 其它

[usaco2004 dec]打扫机房II-Pku2376 Cleaning Shifts(区间问题)

2013-10-30 21:20 441 查看
[b]如有错误,请留言提醒,不要坑到小朋友
[/b]
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
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. 
首先这是一道关于区间的问题,求的是最小区间覆盖问题

算法:

将所有区间按左端点坐标从小到大排序,顺序处理每个区间。每次选择覆盖点的区间中右端点坐标最大的一个,并将更新为该区间的右端点坐标,直到选择的区间已包含。

证明:

显然,该算法最后选出来的区间完全覆盖,下面证明所选出区间的数量是最少的。设为该算法所接受的第个区间的右端点坐标,为某最优解中第个区间的右端点坐标。

命题4.1当时:该算法所接受的第个区间的右端点坐标≥某最优解中的第个区间的右端点坐标。

该命题可以运用数学归纳法来证明。对于,命题显然为真,因为算法第一个选择的区间是能覆盖点的区间中右端点坐标最大的那个。令,假定论断对为真,即,最优解中选的第个区间的右端点坐标为,设它的左端点坐标为。由于该区间覆盖,即。当时,由于,有;当时,有,此时最优解所选择的区间覆盖点,又算法选择的第个区间是右端点坐标最大的那个,故。证毕。

设该算法选出了个区间,而最优解选出了个区间。

命题4.2最优解选出的区间数量=该算法选出的区间数量。

假设,根据命题4.1,有。根据该算法,,因此该最优解不可能覆盖,产生矛盾。所以,又因为是最优解中选出区间个数,所以。

综上所述,算法选出的区间是最优解。

实现:
选择区间可以通过线性扫描来实现。时间复杂度:排序+扫描(nlogn)。
#include<cstdio>
#include<iostream>
#define maxn 30000
using namespace std;
typedef pair<int,int> PII;
PII cow[maxn];
int n,m;
inline int read(){
int tmp=0;char ch;
while(ch=getchar())if('0'<=ch&&ch<='9')break;
for(;'0'<=ch&&ch<='9';ch=getchar())tmp=tmp*10+ch-'0';
return tmp;
}
int main(){
n=read();m=read();
for(int i=1;i<=n;i++){
int a=read(),b=read();
cow[i]=make_pair(a,b);
}
sort(cow+1,cow+n+1);
int s=1,now=1,ans=0;
while(s<=m){
int tmp=0;
if(now>n){puts("-1");return 0;}
while(cow[now].first<=s&&now<=n)tmp=max(cow[now].second,tmp),now++;
if(tmp<s){puts("-1");return 0;}
s=tmp+1;
ans++;
}
printf("%d\n",ans);

}

还能用spfa+slf优化
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
const int maxn=1000010,maxm=2000010;
using namespace std;
int st,ed,tot=0;
int f[maxn],now[maxn];
bool b[maxn];
int h[maxm];
int pre[maxm],son[maxm],v[maxm];

inline void cc(int a,int b,int c)
{
pre[++tot]=now[a];
now[a]=tot;
son[tot]=b;
v[tot]=c;
}

inline int read()
{
char ch; int tmp=0;
while(ch=getchar()) if('0'<=ch && ch<='9') break;
for(;'0'<=ch && ch<='9';ch=getchar()) tmp=tmp*10+ch-'0';
return tmp;
}
int a[50010];
inline void init()
{
int n=read();
st=1;
ed=read()+1;
for (int i=1;i<=n;i++)
{
int l=read();
int r=read()+1;
cc(l,r,1);
a[n+i]=r;
a[i]=l;
}
sort(a+1,a+2*n+1);
for (int i=2;i<=2*n;i++)
cc(a[i],a[i-1],0);
}

inline void spfa()
{
memset(f,63,sizeof(f));
int t=0,w=1;
f[st]=0;
h[1]=st;
b[st]=1;
while (t!=w)
{
if (++t==maxn) t=1;
int p=now[h[t]];
while (p)
{
if (f[h[t]]+v[p]<f[son[p]] && f[h[t]]+v[p]<f[ed])
{
f[son[p]]=f[h[t]]+v[p];
if (!b[son[p]])
{
b[son[p]]=1;
if (++w==maxn) w=1;
h[w]=son[p];
int tt=t+1==maxn?1:t+1;
if (f[h[tt]]>f[h[w]])
swap(h[tt],h[w]);
}
}
p=pre[p];
}
b[h[t]]=0;
}
if (f[ed]>1000000) f[ed]=-1;
printf("%d",f[ed]);
}

int main()
{
init();
spfa();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  优化 贪心