Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! Sort + Greedy

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.