【题解】poj3171 Cleaning Shifts 线段树优化DP

题目链接

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.


F [ x ] F[x] F[x] 表示覆盖 [ m , x ] [m,x] [m,x] 所需要的最小代价。
F [ t 2 i ] = min ⁡ t 1 i ≤ x &lt; t 2 i { F [ x ] } + s i F[t_{2_i}]=\min\limits_{t_{1_i}\leq x&lt;t_{2_i}}\{F[x]\}+s_i F[t2i]=t1ix<t2imin{F[x]}+si
初值: F [ m − 1 ] = 0 F[m-1]=0 F[m1]=0,其余为正无穷。目标: min ⁡ t 2 i ≥ e { F [ t 2 i ] } \min\limits_{t_{2_i}\geq e}\{F[t_{2_i}]\} t2iemin{F[t2i]}

#include<cstdio>
#include<algorithm>
using namespace std;
#define lc (o<<1)
#define rc (o<<1|1)
typedef long long ll;
const int N=1e4+10,M=1e5+10,INF=0x7fffffff;
int n,m,e;
ll mn[M<<2],f[M],ans=INF;
struct node{
	int t1,t2;
	ll s;
	bool operator <(const node&rhs)const{
	return t2<rhs.t2||(t2==rhs.t2&&t1<rhs.t1);}
}cow[N];
void build(int o,int l,int r)
{
	mn[o]=INF;
	if(l==r)return;
	int mid=(l+r)>>1;
	build(lc,l,mid);build(rc,mid+1,r);
	mn[o]=min(mn[lc],mn[rc]);
}
void update(int o,int l,int r,int x,ll v)
{
	if(l==r)
	{
		mn[o]=min(mn[o],v);return;
	}
	int mid=(l+r)>>1;
	if(x<=mid)update(lc,l,mid,x,v);
	else update(rc,mid+1,r,x,v);
	mn[o]=min(mn[lc],mn[rc]);
}
ll query(int o,int l,int r,int nl,int nr)
{
	if(nl<=l&&r<=nr)return mn[o];
	int mid=(l+r)>>1;ll ans=INF;
	if(nl<=mid)ans=min(ans,query(lc,l,mid,nl,nr));
	if(nr>mid)ans=min(ans,query(rc,mid+1,r,nl,nr));
	return ans;
}
int main()
{
	//freopen("in.txt","r",stdin);
    scanf("%d%d%d",&n,&m,&e);
    build(1,1,e+1);
    for(int i=1;i<=n;i++)
        scanf("%d%d%lld",&cow[i].t1,&cow[i].t2,&cow[i].s);
    fill(f,f+M,INF);
    sort(cow+1,cow+n+1);
    for(int i=1;i<=n;i++)
    {
    	if(cow[i].t2<m)continue;
    	if(cow[i].t1<=m)f[cow[i].t2]=min(f[cow[i].t2],cow[i].s);
    	else f[cow[i].t2]=min(f[cow[i].t2],query(1,1,e+1,cow[i].t1,cow[i].t2)+cow[i].s);
	    update(1,1,e+1,cow[i].t2+1,f[cow[i].t2]);
	}
	for(int i=e;i<=cow[n].t2;i++)
	    ans=min(ans,f[i]);
	if(ans>=INF)puts("-1");
	else printf("%lld\n",ans);
	return 0;
}

总结

记得集训时考了一道一模一样的。(当时肯定跪了鸭)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值