POJ 3171 Cleaning Shifts 线段树优化DP

版权声明:https://blog.csdn.net/huashuimu2003 https://blog.csdn.net/huashuimu2003/article/details/89789539

title

POJ 3171
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.

Input

Line 1: Three space-separated integers: N, M, and E.
Lines 2…N+1: Line i+1 describes cow i’s schedule with three space-separated integers: T1, T2, and S.

Output

Line 1: a single integer that is either the minimum total salary to get the barn cleaned or else -1 if it is impossible to clean the barn.

Sample Input

3 0 4
0 2 3
3 4 2
0 0 1

Sample Output

5

Hint

Explanation of the sample:
FJ has three cows, and the barn needs to be cleaned from second 0 to second 4. The first cow is willing to work during seconds 0, 1, and 2 for a total salary of 3, etc.
Farmer John can hire the first two cows.

Source

USACO 2005 December Silver

analysis

f [ x ] f[x] 表示覆盖 [ L , x ] [L,x] 需要花费的最小代价。

把所有贴纸按照右端点 b i b_i 递增排序,按顺序扫描这些贴纸。设当前贴纸为 [ a i , b i ] [a_i,b_i] ,价格为 c i c_i 。状态转移方程为:
f [ b i ] = min a i 1 x &lt; b i { f [ x ] } + c i f[b_i]=\min_{a_i-1 \le x &lt; b_i}\left\{f[x]\right\}+c_i
初值: f [ L 1 ] = 0 f[L-1]=0 ,其余为负无穷。目标: min b i R { f [ b i } \min_{b_i \geq R}\left\{f[b_i\right\}

在这个状态转移方程中,需要查询 f f 数组在 [ a i 1 , b i ] [a_i-1,b_i] 上的最小值,同时 f f 数组会不断发生更新。这是一个带有修改的区间最值问题,使用线段树维护 f f 数组即可在 O ( log N ) O(\log N) 的时间内执行查询、更新操作。

本题中网格位置的坐标都很小,可以直接在 [ L 1 , R ] [L-1,R] 上建立线段树。当坐标较大时也可以先离散化,再用线段树求解。另外,要注意一下贴纸左、右端点超出 [ L , R ] [L,R] 的边界情况。如果你写线段树习惯 1   b a s e 1\text{ }base ,那么别忘了对读入数据做一些预处理(比如坐标统一 + 2 +2 )。

code

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+10,maxe=86399+10,inf=0x3f3f3f3f;

template<typename T>inline void read(T &x)
{
	x=0;
	T f=1, ch=getchar();
	while (!isdigit(ch) && ch^'-') ch=getchar();
	if (ch=='-') f=-1, ch=getchar();
	while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
	x*=f;
}

namespace SGT
{
	int tree[maxe<<2];

	inline void change(int now,int l,int r,int pos,int val)
	{
		if (l==r) { tree[now]=val; return ; }
		int mid=(l+r)>>1;
		if (pos<=mid) change(now<<1,l,mid,pos,val);
		else change(now<<1|1,mid+1,r,pos,val);
		tree[now]=min(tree[now<<1],tree[now<<1|1]);
	}
	
	inline int query(int now,int l,int r,int tl,int tr)
	{
		if (l>tr || r<tl) return inf;
		if (tl<=l && r<=tr) return tree[now];
		int mid=(l+r)>>1;
		return min(query(now<<1,l,mid,tl,tr),query(now<<1|1,mid+1,r,tl,tr));
	}
}using namespace SGT;

struct Orz
{
	int x,y,z;
	inline bool operator < (const Orz &a) const
	{
    	return y<a.y;
    }
}s[maxn];
int f[maxe];
int main()
{
	int n,m,e;
	read(n);read(m);read(e);
	m+=2;e+=2;
	for (int i=1; i<=n; ++i)
	{
		read(s[i].x),read(s[i].y),read(s[i].z);
		s[i].x+=2,s[i].y+=2;
	}

	memset(tree,inf,sizeof(tree));
	memset(f,0x3f,sizeof(f));
	f[m-1]=0;
	change(1,1,maxe,m-1,0);
	sort(s+1,s+n+1);

	for (int i=1; i<=n; ++i)
	{
		if (s[i].y<m) continue;
		int cur=query(1,1,maxe,s[i].x-1,s[i].y)+s[i].z;
		if (f[s[i].y]>cur)
		{
			f[s[i].y]=cur;
			change(1,1,maxe,s[i].y,f[s[i].y]);
		}
	}

	int ans=inf;
	for (int i=e; i<maxe; ++i) ans=min(ans,f[i]);
	printf("%d\n",ans==inf?-1:ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/huashuimu2003/article/details/89789539