Link:
Http://codeforces.com/contest/822/problem/C
Test instructions
There is an X-day holiday, there are n travel tickets, each ticket has a starting time L, the end time R, cost, want to split the holiday into two parts to travel, two parts of time can not coincide (Ri < LJ | | RJ < LI), ask the minimum cost is how much, if not two parts, output-1
Exercises
CF official solution, efficiencyo(nlOgn2)
Set up a struct body,struct P{int p, len, cost, type};
Each ticket will be(l, r, cost)Represented as two structures,P(l, r-l+1, cost, -1), P(r, r-l+1, cost, 1);
Set an array of Best[i], which represents the cheapest ticket for the length of I, which is all INF at first, and then updated with the edge
Sort the struct numbers, first by P, and p by type, so that it is in chronological order and the same time type is-1 before type 1
Traversing the entire structure array
Type is 1, update Best[p[i].len with P[i].cost]
Type is-1, the ANS is updated with P[i].cost+best[x-p[i].len]
Because the array is sorted by P and then type, it is guaranteed that the update ans is used by the best[], which is stored according to the time period in the previous ticket, to ensure that the time does not overlap
Code:
1#include <bits/stdc++.h>2 3 using namespacestd;4typedefLong Longll;5 Const intMAXN = 2E5 + -, INF = 2e9+Ten;6 intN, X, L, R, C;7 intBEST[MAXN];8 structP9 {Ten intp, Len, cost, type; One BOOL operator< (ConstP &x) A { - if(p = = X.P)returnType <X.type; - returnP <X.P; the } -}p[maxn*2]; - - + intMain () - { +scanf"%d%d", &n, &x); A intCNT =0; at for(intI=0; i<n; ++i) - { -scanf"%d%d%d", &l, &r, &c); -p[cnt++] = p{l, r-l+1, C,-1}; -p[cnt++] = p{r, r-l+1C1}; - } inll ans =INF; -Sort (p, p+CNT); toFill (Best, best+x, INF); + - for(intI=0; i<cnt; ++i) the { * if(P[i].type = =-1) $ {Panax Notoginseng if(P[i].len <x) - { theans = min (ans, (LL) p[i].cost+ (LL) best[x-P[i].len]); + } A } the ElseBest[p[i].len] =min (Best[p[i].len], p[i].cost); + } - $ if(ans >= INF) ans =-1; $cout << ans <<Endl; - - return 0; the}
Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! sort + greedy