传送门

题意

给出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. 转: Code Review 程序员的寄望与哀伤

    转自: http://www.cnblogs.com/mindwind/p/5639008.html 一个程序员,他写完了代码,在测试环境通过了测试,然后他把它发布到了线上生产环境,但很快就发现在生产 ...

  2. [WASM Rust] Create and Publish a NPM Package Containing Rust Generated WebAssembly using wasm-pack

    wasm-pack is a tool that seeks to be a one-stop shop for building and working with Rust generated We ...

  3. hadoop优质链接

    http://wiki.apache.org/hadoop/FAQ

  4. 【转载】C#之C#、.NET Framework、CLR的关系

    C#..NET Framework.CLR的关系 很多人没有将C#..NET Framework(.NET框架).CLR(Common Language Runtime,公共语言运行库)这三者之间的关 ...

  5. Java单例的实现

    1.声明实例变量(静态) 2.私有化构造函数 3.创建获取实例的方法 public class Singleton{ //创建实例变量 private static Singleton singlet ...

  6. TinyXML:TiXmlNode

    TiXmlNode: 对应于DOM结构中的节点,是TinyXML中很重要的一个类,它继承自TiXmlBase TiXmlNode的友元类: friend class TiXmlDocument; fr ...

  7. 基于bootstrap_网站汇总页面

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 记录魅族m1note编译TWRP recovery 3.1.0-0,包括mtk机型的处理方法

    1.安装64位linux系统,我用的是deepin os 15.3 2.将系统升级到最新版本 sudo apt-get update && sudo apt-get upgrade 3 ...

  9. peewee模块

    Peewee Python中数据库与ORM主要做这几件事: 数据库方面由程序员设计表关系,主要是1v1,1vN,NvN: ORM做数据类型映射,将数据库表示的char/int等类型映射成Python对 ...

  10. yum 安装 mysql

    安装 yum -y install mysql-server 开机自启动 chkconfig mysqld on 设置账户密码 启动 service mysqld start -- 进入mysql库 ...