POJ - 3171 Cleaning Shifts【线段树优化DP】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niiick/article/details/83153205

Time limit 1000 ms
Memory limit 65536 kB

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.


题目分析

将奶牛按工作结束时间升序排序
d p [ i ] dp[i] 表示 M M ~ i 1 i-1 时间段都有奶牛打扫所需最小费用
初始化 d p [ M ] = 0 dp[M]=0 ,其余为INF
O ( n 2 ) O(n^2) 的dp方程很显然 d p [ T i 2 + 1 ] = m i n ( d p [ j ] ) + S i ( j [ T i 1 , T i 2 ] ) dp[T_{i2}+1]=min(dp[j])+S_i(j\in[T_{i1},T_{i2}])
但是时间复杂度肯定无法承受
于是考虑线段树保存dp数组的区间最小值优化DP
最后判断 d p [ E + 1 ] dp[E+1] 是否为INF,若是则输出-1,否则输出其值


#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
typedef long long lt;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const lt inf=1e12;
const int maxn=100010;
int n,M,E;
struct node{int st,ed,cost;}rem[maxn];
lt mi[maxn<<2],dp[maxn];
bool cmp(node a,node b){return (a.ed==b.ed)?a.st<b.st:a.ed<b.ed;}

void build(int s,int t,int p)
{
    if(s==t){ mi[p]=dp[s]; return;}
    int mid=s+t>>1;
    build(s,mid,p<<1); build(mid+1,t,p<<1|1);
    mi[p]=min(mi[p<<1],mi[p<<1|1]);
}

void update(int u,int s,int t,int p,lt val)
{
    if(s==t){ mi[p]=val; return;}
    int mid=s+t>>1;
    if(u<=mid) update(u,s,mid,p<<1,val);
    else update(u,mid+1,t,p<<1|1,val);
    mi[p]=min(mi[p<<1],mi[p<<1|1]);
}

lt qmin(int ll,int rr,int s,int t,int p)
{
    if(ll<=s&&t<=rr) return mi[p];
    int mid=s+t>>1;lt ans=inf;
    if(ll<=mid) ans=min(ans,qmin(ll,rr,s,mid,p<<1));
    if(rr>mid) ans=min(ans,qmin(ll,rr,mid+1,t,p<<1|1));
    return ans;
}

int main()
{
    n=read();M=read()+1;E=read()+1;
    for(int i=1;i<=n;++i)
    rem[i].st=read()+1,rem[i].ed=read()+1,rem[i].cost=read();
    
    sort(rem+1,rem+1+n,cmp);
    for(int i=1;i<=E+1;++i)dp[i]=inf; dp[M]=0;
    build(1,E+1,1);
    for(int i=1;i<=n;++i)
    {
    	dp[rem[i].ed+1]=min(dp[rem[i].ed+1],qmin(rem[i].st,rem[i].ed,1,E+1,1)+rem[i].cost);
    	update(rem[i].ed+1,1,E+1,1,dp[rem[i].ed+1]);
    }
    if(dp[E+1]==inf) printf("-1");
    else printf("%lld",dp[E+1]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/83153205