POJ 3171 Cleaning Shifts

Description
Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now require their barn to be immaculate. Farmer John, the most obliging of farmers, has no choice but hire some of the cows to clean the barn.

Farmer John has N (1 <= N <= 10,000) cows who are willing to do some cleaning. Because dust falls continuously, the cows require that the farm be continuously cleaned during the workday, which runs from second number M to second number E during the day (0 <= M <= E <= 86,399). Note that the total number of seconds during which cleaning is to take place is E-M+1. During any given second M..E, at least one cow must be cleaning.

Each cow has submitted a job application indicating her willingness to work during a certain interval T1..T2 (where M <= T1 <= T2 <= E) for a certain salary of S (where 0 <= S <= 500,000). Note that a cow who indicated the interval 10..20 would work for 11 seconds, not 10. Farmer John must either accept or reject each individual application; he may NOT ask a cow to work only a fraction of the time it indicated and receive a corresponding fraction of the salary.

Find a schedule in which every second of the workday is covered by at least one cow and which minimizes the total salary that goes to the cows.

解题报告:这一题数据范围很小,可以用\(O(N^2)\)卡,我们把不再M-E范围内的去掉,然后我们按R端点排序\(f[i]\)为前i头奶牛,并且[M,r[i]]都已经被覆盖的最小费用,最后 \(f[i]=Max(f[j]+s[i])\) 如果满足\(r[j]>=l[i]-1\) 就转移即可,这个显然是可以线段树优化的,所以数据范围可以出到\(O(nlogn)\)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=1e4+5,M=1e5+5,inf=2e8;
struct node{
	int l,r,val;
	bool operator <(const node &pp){
		return r<pp.r;
	}
}a[N];
int n,S,E,f[N];
void work()
{
	n=0;int num;
	scanf("%d%d%d",&num,&S,&E);
	for(int i=1;i<=num;i++){
		n++;scanf("%d%d%d",&a[n].l,&a[n].r,&a[n].val);
		if(a[n].r<S || a[n].l>E)n--;
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
	  f[i]=inf;
		if(a[i].l<=S)f[i]=a[i].val;
		for(int j=i-1;j>=1;j--){
			if(a[j].r<a[i].l-1)break;
			f[i]=Min(f[i],f[j]+a[i].val);
		}
	}
	int ans=inf;
	for(int i=n;i>=1;i--)
		if(a[i].r>=E && f[i]<ans)ans=f[i];
	if(ans==inf)puts("-1");
	else printf("%d\n",ans);
}

int main()
{
	work();
	return 0;
}
posted @ 2017-09-10 14:34  PIPIBoss  阅读(159)  评论(0编辑  收藏  举报