传送门

题意

给出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. [MDX] Build a Custom Provider Component for MDX Deck

    MDX Deck is a great library for building slides using Markdown and JSX. Creating a custom Providerco ...

  2. 在 Edison 上自动启动 Arduino Sketch

    前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验和正常的链接.图片显示,请访问我的博客原文: http://www.cnblog ...

  3. spring 事件模式 源代码导读

    一,jdk 事件对象基类 package java.util; import java.io.Serializable; public class EventObject implements Ser ...

  4. 使用zTree进行数据动态显示

    由于公司项目的须要.现学了一下zTree的使用. 以下是我项目的结构图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmVuamFtaW5fd2h4/f ...

  5. C#Unicode和Utf-8

    Unicode(统一码.万国码.单一码)是一种在计算机上使用的字符编码.Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言 ...

  6. Redis 事务及其应用

    参考: http://www.runoob.com/redis/redis-transactions.html https://www.cnblogs.com/qlshine/p/5958504.ht ...

  7. C#高阶与初心:(二)P/Invoke平台调用

    最近某个项目要采集交易终端的信息用于监管,主要厂商给出了API,C++版的...开启hard模式!!! C#调用C++的DLL基本就两种方法:加一个VC++项目包一层,或者使用P/Invoke(平台调 ...

  8. Apache Flink 1.5.1 Released

    Apache Flink: Apache Flink 1.5.1 Released http://flink.apache.org/news/2018/07/12/release-1.5.1.html ...

  9. 如何使用Visual Studio构建libiconv

    参考博文:How to Build libiconv with Microsoft Visual Studio - CodeProject libiconv源码下载地址:libiconv - GNU ...

  10. Javascript正则中的exec和match

    分几种情况说明 1.假设re中不是全局的也就是不带g var str = "cat3 hat4"; var re = /\w+\d/; var ex = re.exec(str); ...