• Codeforces 822C Hacker, pack your bags!


    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 liri,costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of thei-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 liri 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) = 5and 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.


      题目大意 给定n个区间,每个区间有个费用,选择两个不相交(端点也不能重叠)的区间使得它们的长度总和恰好为x,并且使得它们的费用和最小。如果无解输出-1。

      根据常用套路,按照区间的长度进行分组。然后按照端点的大小(反正按左端点还是右端点都是一样的)进行排序。再拿两个数组跑一道前缀最小值和后缀最小值。

      现在枚举每一条旅行方案,在和它的长度之和恰好为x的另一个数组中,可以用lower_bound和upper_bound快速找出两侧合法的的最后一个和第一个,然后更新一下就好了(详细可以看代码)。

    Code

      1 /**
      2  * Codeforces
      3  * Problem#822C
      4  * Accepted
      5  * Time:124ms
      6  * Memory:16416k
      7  */
      8 #include <iostream>
      9 #include <cstdio>
     10 #include <ctime>
     11 #include <cmath>
     12 #include <cctype>
     13 #include <cstring>
     14 #include <cstdlib>
     15 #include <fstream>
     16 #include <sstream>
     17 #include <algorithm>
     18 #include <map>
     19 #include <set>
     20 #include <stack>
     21 #include <queue>
     22 #include <vector>
     23 #include <stack>
     24 #ifndef WIN32
     25 #define Auto "%lld"
     26 #else
     27 #define Auto "%I64d"
     28 #endif
     29 using namespace std;
     30 typedef bool boolean;
     31 const signed int inf = (signed)((1u << 31) - 1);
     32 const double eps = 1e-6;
     33 const int binary_limit = 128;
     34 #define smin(a, b) a = min(a, b)
     35 #define smax(a, b) a = max(a, b)
     36 #define max3(a, b, c) max(a, max(b, c))
     37 #define min3(a, b, c) min(a, min(b, c))
     38 template<typename T>
     39 inline boolean readInteger(T& u){
     40     char x;
     41     int aFlag = 1;
     42     while(!isdigit((x = getchar())) && x != '-' && x != -1);
     43     if(x == -1) {
     44         ungetc(x, stdin);    
     45         return false;
     46     }
     47     if(x == '-'){
     48         x = getchar();
     49         aFlag = -1;
     50     }
     51     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
     52     ungetc(x, stdin);
     53     u *= aFlag;
     54     return true;
     55 }
     56 
     57 typedef class Trip {
     58     public:
     59         int l;
     60         int r;
     61         int fee;
     62         
     63         Trip(int l = 0, int r = 0, int fee = 0):l(l), r(r), fee(fee) {        }
     64         
     65         boolean operator < (Trip b) const {
     66             return l < b.l;
     67         }
     68 }Trip;
     69 
     70 boolean operator < (const int& b, const Trip& a) {
     71     return b < a.l;
     72 }
     73 
     74 int n, x;
     75 vector<Trip>* a;
     76 vector<int>* minu;        // in order
     77 vector<int>* mind;        // in opposite order
     78 
     79 inline void init() {
     80     readInteger(n);
     81     readInteger(x);
     82     a = new vector<Trip>[x];
     83     minu = new vector<int>[x];
     84     mind = new vector<int>[x];
     85     for(int i = 1, l, r, c; i <= n; i++) {
     86         readInteger(l);
     87         readInteger(r);
     88         readInteger(c);
     89         int len = r - l + 1;
     90         if(len >= x)    continue;
     91         a[len].push_back(Trip(l, r, c));
     92     }
     93 }
     94 
     95 int res = inf;
     96 inline void solve() {
     97     for(int i = 1; i < x; i++)
     98         if(!a[i].empty()) {
     99             sort(a[i].begin(), a[i].end());
    100             minu[i].resize(a[i].size());
    101             mind[i].resize(a[i].size());
    102             for(int j = 0; j < (signed)a[i].size(); j++) {
    103                 if(j == 0)    minu[i][j] = a[i][j].fee;
    104                 else minu[i][j] = min(a[i][j].fee, minu[i][j - 1]);
    105             }
    106             for(int j = (signed)a[i].size() - 1; j >= 0; j--) {
    107                 if(j == a[i].size() - 1)    mind[i][j] = a[i][j].fee;
    108                 else mind[i][j] = min(a[i][j].fee, mind[i][j + 1]);
    109             }
    110         }
    111         
    112     for(int i = 1; i < x; i++) {
    113 //        cout << i << ":" << endl;
    114         if(!a[i].empty()) {
    115             for(int j = 0; j < a[i].size(); j++) {
    116 //                cout << a[i][j].l << " " << a[i][j].r << endl;
    117                 Trip &t = a[i][j];
    118                 int len = t.r - t.l + 1;
    119                 int ulen = x - len;
    120                 int pos = upper_bound(a[ulen].begin(), a[ulen].end(), t.r) - a[ulen].begin();
    121                 if(pos != a[ulen].size())    smin(res, t.fee + mind[ulen][pos]);
    122                 pos = lower_bound(a[ulen].begin(), a[ulen].end(), t.l - ulen + 1) - a[ulen].begin() - 1;
    123                 if(pos != -1)    smin(res, t.fee + minu[ulen][pos]);
    124             }
    125         }
    126     }
    127     
    128     if(res == inf)
    129         puts("-1");
    130     else
    131         printf("%d
    ", res);
    132 }
    133 
    134 int main() {
    135     init();
    136     solve();
    137     return 0;
    138 }
  • 相关阅读:
    MySQL 卸载
    Mysql(压缩包)下载与安装
    sqlserver一次性修改多条
    mysql一些操作
    SQL函数集合
    js时间转换相关
    easyui grid中翻页多选方法
    easyui combobox筛选(拼音)
    wpf图片切换,幻灯效果
    openfire搭建IM
  • 原文地址:https://www.cnblogs.com/yyf0309/p/7145080.html
Copyright © 2020-2023  润新知