POJ 3171 DP

Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3563   Accepted: 1205

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

题意:
要处理m~e时间段的东西,有n个人,每个人能处理l~r连续时间段的东西并且费用为w,问将这m~e时间段的东西都处理完的最小花费。
输入n,m,e;
输入n行l,r,w;
输出最小花费
代码:
//容易想到dp但是没想到可以用线段树处理区间最小值,dp[i]表示到达时间i
//时的最小花费,将区间按照右值从小到大排序,然后枚举区间右值,
//dp[r]=min(dp[r],min(dp[l-1~r-1])+w),其中后一项用线段树处理区间最小值。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=10009;
const int maxm=90000;
int n,m,e,minv[maxm*4],f[maxm];
struct Lu{
    int l,r,w;
    Lu(){}
    Lu(int a,int b,int c):l(a),r(b),w(c){}
    bool operator < (const Lu &p)const{
        return r<p.r;
    }
}L[maxn];
void pushup(int rt){
    minv[rt]=min(minv[rt<<1],minv[rt<<1|1]);
}
void build(int l,int r,int rt){
    minv[rt]=inf;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushup(rt);
}
void update(int id,int v,int l,int r,int rt){
    if(l==r){
        minv[rt]=v;
        return;
    }
    int mid=(l+r)>>1;
    if(id<=mid) update(id,v,l,mid,rt<<1);
    else update(id,v,mid+1,r,rt<<1|1);
    pushup(rt);
}
int query(int ql,int qr,int l,int r,int rt){
    if(ql<=l&&qr>=r)
        return minv[rt];
    int mid=(l+r)>>1,ans=inf;
    if(ql<=mid) ans=min(ans,query(ql,qr,l,mid,rt<<1));
    if(qr>mid) ans=min(ans,query(ql,qr,mid+1,r,rt<<1|1));
    return ans;
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&e)==3){
        e-=m;                           //将区间左移到从0开始
        int cnt=0;
        for(int i=0;i<n;i++){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            if(y<m||x>e) continue;      //去掉不可行的区间
            x-=m;y-=m;
            if(x<0) x=0;
            if(y>e) y=e;
            L[cnt++]=Lu(x,y,z);
        }
        sort(L,L+cnt);
        memset(f,inf,sizeof(f));
        build(0,e,1);
        for(int i=0;i<n;i++){
            int tmp=inf;
            if(L[i].l==0) tmp=L[i].w;
            else tmp=query(L[i].l-1,L[i].r-1,0,e,1)+L[i].w;
            f[L[i].r]=min(f[L[i].r],tmp);
            if(f[L[i].r]<inf)
                update(L[i].r,f[L[i].r],0,e,1);
        }
        if(f[e]>=inf) f[e]=-1;
        printf("%d\n",f[e]);
    }
    return 0;
}

 

 
posted @ 2017-08-01 09:39  luckilzy  阅读(365)  评论(0编辑  收藏  举报