Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!(更新数组)

传送门

题意

给出n个区间[l,r]及花费\(cost_i\),找两个区间满足
1.区间和为指定值x
2.花费最小

分析

先用vector记录(l,r,cost)和(r,l,cost),按l排序,再设置一个数组bestcost[i]代表长度为i的最小花费。
O(n)扫一遍,如果碰到区间左端点,更新答案;碰到右端点,更新bestcost[len],具体见代码

trick

1.更新答案会爆int

代码

#include <bits/stdc++.h>
using namespace std;

#define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define pb(x) push_back(x)
#define LL long long
//#pragma comment(linker, "/STACK:102400000,102400000")
//inline void read(int &x){x=0; char ch=getchar();while(ch<'0') ch=getchar();while(ch>='0'){x=x*10+ch-48; ch=getchar();}}
const int maxn=200200;
const int inf = 2e9+20;
int n,x,l,r,cost;
std::vector<pair<pair<int,int>,pair<int,int> > > v;
int bestcost[maxn+10];
int main()
{
    scanf("%d %d",&n,&x);
    R(i,0,n)
    {
        scanf("%d %d %d",&l,&r,&cost);
        v.pb(mp(mp(l,-1),mp(r,cost)));
        v.pb(mp(mp(r,1),mp(l,cost)));
    }
    F(i,0,maxn) bestcost[i]=inf; 
    sort(v.begin(),v.end());
    LL ans=inf;
    int type,sz=v.size();
    R(i,0,sz)
    {
        type=v[i].first.second;
        if(type==-1)
        {
            int len=v[i].second.first-v[i].first.first+1;
            //printf("%d\n",bestcost[x-len]);
            if(x>len) ans=min(ans,(LL)(v[i].second.second)+(LL)bestcost[x-len]);
        }
        else
        {
            int len=v[i].first.first-v[i].second.first+1;
            bestcost[len]=min(bestcost[len],v[i].second.second);
        }
    }
    printf("%I64d\n",(ans>=inf)?-1:ans);
    return 0;
}
posted @ 2017-07-04 10:30  遗风忘语  阅读(173)  评论(0编辑  收藏  举报