codeforces 822c

http://www.elijahqi.win/2017/07/09/codeforces-822c/
C. Hacker, pack your bags!
time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

It’s well known that the best way to distract from something is to do one’s favourite thing. Job is such a thing for Leha.

So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.

At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don’t intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don’t intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.

Help Leha to choose the necessary vouchers!

Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha’s vacation correspondingly.

Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.

Output
Print a single integer — a minimal amount of money that Leha will spend, or print  - 1 if it’s impossible to choose two disjoint vouchers with the total duration exactly x.

Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.

In the second sample the duration of each voucher is 3 therefore it’s impossible to choose two vouchers with the total duration equal to 2.

题解:(官方)

Let’s sort all the vouchers by their left border li. Let’s consider the vouchers in this sorted order. Now we want to consider i-th one. The duration of the i-th voucher is ri - li + 1. Consequentally only the vouchers with the duration x - ri + li - 1 can make a couple with the i-th one. At the same time you shouldn’t forget that the value of this expression may be negative. You should check it. Besides let’s find a couple for the i-th voucher among all the vouchers k for which rk < li.

To implement this solution let’s keep an array bestCost. bestCost[j] denotes the minimal cost of the voucher with the duration equal to exactly j on the considering prefix (i.e. we should consider only such vouchers k with rk < li in bestCost). Thus, it’s enough to consider vouchers in order of increasing of their left borders and update the array bestCost.

Total complexity is .

const int maxn = 200500;
const int inf = ( 2e9 ) + 2;

vector < pair < pair < int, int >, pair < int, int > > > queries;
int bestCost[maxn];

int l[maxn];
int r[maxn];
int cost[maxn];

int solution( int n, int needLen ) {
    queries.clear();
    for ( int j = 0; j < n; j++ ) {
        queries.pb( mp( mp( l[j], -1 ), mp( r[j], cost[j] ) ) );
        queries.pb( mp( mp( r[j], 1 ), mp( l[j], cost[j] ) ) );
    }
    for ( int j = 0; j < maxn; j++ )
        bestCost[j] = inf;
    ll ans = 2LL * inf;
    sort( queries.begin(), queries.end() );
    int sz = queries.size();
    for ( int j = 0; j < sz; j++ ) {
        int type = queries[j].f.s;
        if ( type == -1 ) {
            int curLen = queries[j].s.f - queries[j].f.f + 1;
            if ( curLen <= needLen )
                ans = min( ans, 1LL * queries[j].s.s + 1LL * bestCost[needLen - curLen] );
        }
        if ( type == 1 ) {
            int curLen = queries[j].f.f - queries[j].s.f + 1;
            bestCost[curLen] = min( bestCost[curLen], queries[j].s.s );
        }
    }
    return ans >= inf ? -1 : ans;
}

官方代码我倒是没有看懂,解释大概看了看

题意: 有n条线段(n<=2e5) 每条线段有左端点li,右端点ri,价值cost(1 <= li <= ri <= 2e5, cost <= 1e9);
对于一个给定的x(x <= 2e5),寻找两个不相交的线段,使它们的长度和恰好为x,并且价值和最小;

这题目肯定是枚举一条线段再找剩下一条线段, 假设枚举右线段, 且其长为len1, 那么我们需要找这条线段左边的长度为 x - len1 的线段的 cost 最小值;直接暴力的话是n^2的复杂度, 肯定 tle. 可以给这些线段按照左右端点排下序, 那么我们可以从按左端点排序的数组中枚举右线段, 然后在按右端点排序的数组中找左线段;但是如果暴力找的话时间复杂度还是 n^2 . 我们可以维护一个 least数组, least[i] 为当前右线段的左边线段中长度为 i 的 cost 最小值, 那么 least[x - len1] 即为当前左线段的 cost 最小值;
那么 cost1 + least[x - len1] 的最小值即为答案啦;

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 220000
const int inf=0x7f7f7f7f; 
using namespace std;
inline int read(){
    int x=0;char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
inline long long read1(){
    long long x=0;char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
struct node{
    int l,r;
    long long w;
}a[N],b[N];
bool cmp(node x,node y){
    return x.l<y.l;
}
bool cmp1(node x,node y){
    return x.r<y.r;
}
long long least[N];
int n,x;
int main(){
    freopen("cf.in","r",stdin);
    freopen("cf.out","w",stdout);
    scanf("%d%d",&n,&x);
    //n=read();x=read();
    for (int i=0;i<n;++i) {
        //a[i].l=read();b[i].l=a[i].l;
        //a[i].r=read();b[i].r=a[i].r;
        //a[i].w=read1();b[i].w=a[i].w;
        scanf("%d%d%lld",&a[i].l,&a[i].r,&a[i].w);
        b[i]=a[i];
    }
    sort(a,a+n,cmp);
    sort(b,b+n,cmp1);
    memset(least,0x7f,sizeof(least));
    long long ans=inf;
    //printf("%d",ans);
    int idx=0;
    for (int i=0;i<n;++i){
        long long less1=a[i].w;
        while (idx<=n&&b[idx].r<a[i].l){
            int tmp=b[idx].r-b[idx].l+1;
            if (least[tmp]>b[idx].w) least[tmp]=b[idx].w;
            idx++;
        }
        int tmp2=x-(a[i].r-a[i].l+1);
        if (tmp2<=0) continue;
        less1+=least[tmp2];
        if (ans>less1) ans=less1;
    }
    if (ans==inf) printf("-1\n");else{
        printf("%d\n",ans);
    }
    //printf("%lld\n",ans==inf?-1:ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值