POJ 3171 区间覆盖最小值&&线段树优化dp

Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4715   Accepted: 1590

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 ]有一个权值w ,  问覆盖区间[ m , e ]的最小权值是多少。
解析: 首先按照右端点从小到大排序,然后dp[i][j] 表示 考虑前i个区间,覆盖区间[m,j]的最小值。
考虑当前区间[ li , ri ]容易想到 dp[i][ri] 的最小值就是 dp[ri]=min(dp[ri],min(dp[ li -1, ri ])+wi) 我们可以线段树查找区间最小值进行优化,
当我们确定了dp[ri] 其实更新ri这一个点值就可以了 因为下一个区间[ Li+1,Ri+1 ] 询问最小值的时候,一定会询问到dp[ri],询问不到,说明 Li+1是大于 ri 的,覆盖的区间已经断开了。
AC代码
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n")
#define debug(a,b) cout<<a<<" "<<b<<" "<<endl
#define ffread(a) fastIO::read(a)
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
const ll inf = 1e12;
const ll mod = 1000000007;
const double epx = 1e-6;
const double pi = acos(-1.0);
//head-----------------------------------------------------------------
ll minn[maxn*4];
void PushUp(int rt)
{
    minn[rt]=min(minn[rt<<1],minn[rt<<1|1]);
}
void update(int x,ll val,int l,int r,int rt)
{
    if(l==x&&r==x)
    {
       minn[rt]=val;
       return;
    }
    int mid=(l+r)>>1;
    if(x<=mid)
        update(x,val,l,mid,rt<<1);
    else
        update(x,val,mid+1,r,rt<<1|1);
    PushUp(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return minn[rt];
    }
    int mid=(l+r)>>1;
    ll ans=1e12;
    if(L<=mid) ans=min(ans,query(L,R,l,mid,rt<<1));
    if(R>mid) ans=min(ans,query(L,R,mid+1,r,rt<<1|1));
    return ans;
}
struct node
{
    ll l,r,val;
}qujian[maxn];
bool cmp(node a,node b)
{
    return a.r<b.r;
}
int main()
{
    ll n,m,e;
    scanf("%lld%lld%lld",&n,&m,&e);
    m+=2,e+=2;
    for(int i=0;i<n;i++)
    {
        scanf("%lld%lld%lld",&qujian[i].l,&qujian[i].r,&qujian[i].val);
        qujian[i].l+=2,qujian[i].r+=2;
    }
    sort(qujian,qujian+n,cmp);
    for(int i=1;i<=1e5;i++)
        update(i,inf,1,1e5,1);
    update(m-1,0,1,1e5,1);
    for(int i=0;i<n;i++)
    {
        if(qujian[i].r>=m)
        {
            ll tempmin=query(qujian[i].l-1,qujian[i].r,1,1e5,1);
            if(tempmin==inf)
                continue;
            ll temp=tempmin+qujian[i].val;
            ll before=query(qujian[i].r,qujian[i].r,1,1e5,1);
            if(before>temp)
                update(qujian[i].r,temp,1,1e5,1);
        }
    }
    ll ans=query(e,1e5,1,1e5,1);
    if(ans==inf)
        printf("-1\n");
    else
        printf("%lld\n",ans);
}

 

posted @ 2019-04-02 21:29  灬从此以后灬  阅读(287)  评论(0编辑  收藏  举报