传送门

题意

给出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;
}

Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!(更新数组)的更多相关文章

  1. Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序,贪心

    C. Hacker, pack your bags!     It's well known that the best way to distract from something is to do ...

  2. Codeforces Round #422 (Div. 2)

    Codeforces Round #422 (Div. 2) Table of Contents Codeforces Round #422 (Div. 2)Problem A. I'm bored ...

  3. 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(二分写法)

    [题目链接]:http://codeforces.com/contest/822/problem/C [题意] 有n个旅行计划, 每个旅行计划以开始日期li,结束日期ri,以及花费金钱costi描述; ...

  4. 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(hash写法)

    接上一篇文章; 这里直接把左端点和右端点映射到vector数组上; 映射一个open和close数组; 枚举1..2e5 如果open[i]内有安排; 则用那个安排和dp数组来更新答案; 更新答案完之 ...

  5. Codeforces Round #422 (Div. 2) E. Liar 后缀数组+RMQ+DP

    E. Liar     The first semester ended. You know, after the end of the first semester the holidays beg ...

  6. Codeforces Round #422 (Div. 2) B. Crossword solving 枚举

    B. Crossword solving     Erelong Leha was bored by calculating of the greatest common divisor of two ...

  7. Codeforces Round #422 (Div. 2) A. I'm bored with life 暴力

    A. I'm bored with life     Holidays have finished. Thanks to the help of the hacker Leha, Noora mana ...

  8. 【Codeforces Round #422 (Div. 2) D】My pretty girl Noora

    [题目链接]:http://codeforces.com/contest/822/problem/D [题意] 有n个人参加选美比赛; 要求把这n个人分成若干个相同大小的组; 每个组内的人数是相同的; ...

  9. 【Codeforces Round #422 (Div. 2) B】Crossword solving

    [题目链接]:http://codeforces.com/contest/822/problem/B [题意] 让你用s去匹配t,问你最少需要修改s中的多少个字符; 才能在t中匹配到s; [题解] O ...

随机推荐

  1. Camtasia Studio如何添加画中画

    将录像文件和其他视频文件拖放到剪辑箱,右击录像文件(camrec文件)添加到时间轴,一般这个就是主要的视频文件,我们会在这个基础上添加字幕,配音,画中画等,拖进去之后可以发现多出来了一个视频1和音频1 ...

  2. centos 重新获取IP hdcp 模式

    centos 重新获取IP hdcp 模式 dhclient -r →release dhclient →renew

  3. setState 是异步的

    1.解决 setState 异步问题 // 查询 handleSearch(e){ // 禁止默认行为 e.preventDefault(); // 获取 form 表单的值 this.setStat ...

  4. CSP:使用CryptoAPI解码X509证书内容

    微软的CryptoAPI提供了一套解码X509证书的函数,一个X509证书解码之后,得到一个PCCERT_CONTEXT类型的结构体指针. 通过该结构体,我们就能够获取想要的证书项和属性等. X509 ...

  5. C++再论单例模式

    #include <iostream> #include <windows.h> #include <mutex> std::mutex gmutex; using ...

  6. 死去活来的OC NSArray 中文排序 及输出

    目的 1.NSArray 能够支持中文排序 2.NSLog 能够直接输出 NSArray 内的中文(事实上 java 直接打印数组也不能显示内容哈) 又是死去活来的搞了1个小时,分类实现.废话少说,上 ...

  7. (转载)js(jquery)的on绑定点击事件执行两次的解决办法

    js(jquery)的on绑定点击事件执行两次的解决办法—不是事件绑定而是事件冒泡 遇到的问题:jquery中用.on()给页面中新加的元素添加点击事件时,点击事件源,绑定的事件执行两次,这里的ale ...

  8. Android开发之开机自动启动应用

    package com.raycloud.wolf.autostart; import android.content.BroadcastReceiver; import android.conten ...

  9. BusyBox下ftpget的使用方法

    在终端输入ftpget命令,可以得到以下帮助信息: BusyBox v1.17.4 (2010-12-22 10:59:18 CST) multi-call binary. Usage: ftpget ...

  10. NSString类的方法实现

    创建一个新字符串并将其设置为 path 指定的文件的内容,使用字符编码enc,在error上返回错误 + (id)stringWithContentsOfURL:(NSURL *)url encodi ...