Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!(更新数组)
传送门
题意
给出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!(更新数组)的更多相关文章
- 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 ...
- Codeforces Round #422 (Div. 2)
Codeforces Round #422 (Div. 2) Table of Contents Codeforces Round #422 (Div. 2)Problem A. I'm bored ...
- 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(二分写法)
[题目链接]:http://codeforces.com/contest/822/problem/C [题意] 有n个旅行计划, 每个旅行计划以开始日期li,结束日期ri,以及花费金钱costi描述; ...
- 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(hash写法)
接上一篇文章; 这里直接把左端点和右端点映射到vector数组上; 映射一个open和close数组; 枚举1..2e5 如果open[i]内有安排; 则用那个安排和dp数组来更新答案; 更新答案完之 ...
- 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 ...
- Codeforces Round #422 (Div. 2) B. Crossword solving 枚举
B. Crossword solving Erelong Leha was bored by calculating of the greatest common divisor of two ...
- 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 ...
- 【Codeforces Round #422 (Div. 2) D】My pretty girl Noora
[题目链接]:http://codeforces.com/contest/822/problem/D [题意] 有n个人参加选美比赛; 要求把这n个人分成若干个相同大小的组; 每个组内的人数是相同的; ...
- 【Codeforces Round #422 (Div. 2) B】Crossword solving
[题目链接]:http://codeforces.com/contest/822/problem/B [题意] 让你用s去匹配t,问你最少需要修改s中的多少个字符; 才能在t中匹配到s; [题解] O ...
随机推荐
- 【Android小项目】找不同,改编自"寻找房祖名"的一款开源小应用。
近期在微信朋友圈"寻找房祖名"和"万里寻刀"这类小游戏比較火.我试着写了一个android版本号的,里面全是一系列的形近字,实现原理非常easy:用一个Grid ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- WMS8_仓库配置
仓库划分为不同的区域,比如收货区,出货区,库存区,这些区域在odoo中称之为库位[location],库位可以上下嵌套,形成一个树状的层级关系. 库位分为不同的类型 物理库存位置[Physical s ...
- 关于从 coding 拉项目的操作
介绍:coding是托管代码的仓库 sourceTree 是把代码提交到coding的界面化工具 1.通过百度 登录coding账号
- Python爬取韩寒所有新浪博客
接上一篇,我们依据第一页的链接爬取了第一页的博客,我们不难发现,每一页的链接就仅仅有一处不同(页码序号),我们仅仅要在上一篇的代码外面加一个循环,这样就能够爬取全部博客分页的博文.也就是全部博文了. ...
- [转载]saltstack master配置文件中文翻译
原文出处:http://blog.coocla.org/301.html Salt系统的配置是令人惊讶的简单,对于salt的两个系统都有各自的配置文件,salt-master是通过一个名为master ...
- ok6410[000] 搭建裸机开发环境
1.安装交叉工具链arm-linux-gcc-4.3.2 先把这个工具复制到rhat系统中[rhel-server-6.3-i386-dvd.iso] 解压arm-linux-gcc-4.3.2到一个 ...
- Vim经常使用技巧总结2
我的主力博客:半亩方塘 1. 在光标所在行查找字符在普通模式下用 f{char} 命令,光标会移动到该字符所在的位置.向下反复查找在普通模式下用 ;,向上回退查找用 , 2. 在光标所在行查找与替换在 ...
- 程序编写安全代码——sendto和recvfrom的大坑
近日帮一个兄弟查代码问题,再处理完一系列问题以后,发现程序某些时候工作还是不正常,甚至会崩溃.因为环境所限,不能使用gdb,所以我只能review他的代码.最终发现原来是sendto和recvfrom ...
- LINQ体验(18)——LINQ to SQL语句之视图和继承支持
视图 我们使用视图和使用数据表类似,仅仅需将视图从"server资源管理器/数据库资源管理器"拖动到O/R 设计器上,自己主动能够创建基于这些视图的实体类.我们能够同操作数据表一样 ...