poj 3171 Cleaning Shifts(区间的最小覆盖价值)

Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2743   Accepted: 955

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.


给出n个小区间[l,r],更新这段区间的代价为c,求覆盖一段区间[m,e]的最小值。

线段树+DP

首先应该将小区间排个序,对于每一个区间[l,r],它能够在[l-1,r]区间上覆盖,找到这段区

间的最小代价。最小代价加上这段区间的代价与r点的代价比較。更新包括r点的区间

段就能够了。

区间查询用线段树。


代码:

//94ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=100000+100;
const int inf=199999999;
struct node
{
    int minc;
} tree[maxn<<2];
struct Cow
{
    int l;
    int r;
    int c;
} cow[maxn];
bool cmp(Cow a,Cow b)
{
    if(a.l==b.l)
        return a.r<b.r;
    return a.l<b.l;
}
void build(int rt,int l,int r)//建树
{
    tree[rt].minc=inf;
    if(l==r)
        return;
    int m=(l+r)>>1;
    build(rt<<1,l,m);
    build(rt<<1|1,m+1,r);
}
void update(int rt,int k,int v,int l,int r)//更新所有包括k的区间
{
    if(l==r)
    {
        tree[rt].minc=min(tree[rt].minc,v);
        return;
    }
    int m=(l+r)>>1;
    if(k<=m)
        update(rt<<1,k,v,l,m);
    else
        update(rt<<1|1,k,v,m+1,r);
    tree[rt].minc=min(tree[rt<<1].minc,tree[rt<<1|1].minc);
}
int query(int rt,int l,int r,int L,int R)//查找L,R区间内的最小值
{
    if(l>=L&&r<=R)
    return tree[rt].minc;
    int mid=(l+r)>>1;
    int temp = inf;
    if(L<=mid)
        temp=query(rt<<1,l,mid,L,R);
    if(R>mid)
        temp=min(temp,query(rt<<1|1,mid+1,r,L,R));
    return temp;
}
int main()
{
    int n,m,e;
    while(~scanf("%d%d%d",&n,&m,&e))
    {
        for(int i=0; i<n; i++)
            scanf("%d%d%d",&cow[i].l,&cow[i].r,&cow[i].c);
        int sign=1;
        sort(cow,cow+n,cmp);
        build(1,m-1,e);
        int cur=m-1;
        update(1,cur,0,m-1,e);//将m-1点赋为0.
        for(int i=0; i<n; i++)
        {
            if(cow[i].l>cur+1)//不能所有覆盖
            {
                sign=0;
                break;
            }
            int temp=query(1,m-1,e,cow[i].l-1,cow[i].r);
            update(1,cow[i].r,temp+cow[i].c,m-1,e);
            cur=max(cur,cow[i].r);

        }
        if(sign)
            printf("%d\n",query(1,m-1,e,e,e));
        else
            printf("-1\n");
    }
    return 0;
}


posted @ 2016-03-30 09:22  phlsheji  阅读(279)  评论(0编辑  收藏  举报