codeforces #422 C. Hacker, pack your bags!(贪心dp)
来源:互联网 发布:amarra for mac 编辑:程序博客网 时间:2024/05/22 20:16
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 li,ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-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 orrj < li.
Help Leha to choose the necessary vouchers!
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 li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
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.
4 51 3 41 2 55 6 11 2 4
5
3 24 6 32 4 13 5 4
-1
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 to2.
【题意】:
给出n个区间段,每个区间有一个价值,现在要找到两个不相交的区间段,使得长度和为x,且价值最小,输出最小价值。
注意:定义区间长度为r-l+1,且端点重合也属于相交
【分析】:
方法1:
有点像贪心+dp维护。
结构体变量存点的信息,每个点包括 坐标p,所属区间长度len,所属区间价值val,标志位(左端点or右端点)
按坐标从小到大扫描。
遇到右端点时,更新数组dp【len】表示单个区间长度为len时的最小价值。
遇到左端点时,取dp【x-len】加上当前区间的价值,更新答案ans的最小值。
方法2:
枚举区间+二分查找
按区间长度排序,枚举区间,用二分查找互补的区间,价值相加,用ans记录最小价值
【代码-方法1】:
#include<bits/stdc++.h>using namespace std;typedef long long ll;const ll INF=0x3f3f3f3f3f3f;struct node{ int p,len;//坐标和长度 ll val; //价值 bool flag;//标记左右 node(int P=0,int L=0,ll V=0,bool F=0){ p=P;len=L;val=V;flag=F; }}e[505050];ll dp[502020];bool cmp(node a,node b){ if(a.p==b.p)return a.flag<b.flag; return a.p<b.p;}int main(){ ios::sync_with_stdio(0); int n,x,u,v,w; while(cin>>n>>x) { for(int i=0;i<n;i++) { cin>>u>>v>>w; e[i]=node(u,v-u+1,w,0); e[i+n]=node(v,v-u+1,w,1); } sort(e,e+n*2,cmp); for(int i=0;i<200000;i++)dp[i]=INF; ll ans=INF; for(int i=0;i<2*n;i++) { if(e[i].flag==0&&x-e[i].len>=0)//left ans=min(ans,e[i].val+dp[x-e[i].len]); else if(e[i].flag) //right dp[e[i].len]=min(dp[e[i].len],e[i].val); } if(ans==INF)ans=-1; cout<<ans<<endl; }}
【代码-方法2】:不想写了..
- codeforces #422 C. Hacker, pack your bags!(贪心dp)
- codeforces 822 C. Hacker, pack your bags!(思维+dp)
- Codeforces Round #422 C. Hacker, pack your bags! (二分)
- codeforces 822 C Hacker, pack your bags!
- codeforces 882C Hacker, pack your bags!
- CodeForces 822C Hacker, pack your bags!
- cf #422 c Hacker, pack your bags! 【贪心】
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!
- Hacker, pack your bags! Codeforces Round #422 (Div. 2) (贪心)
- Hacker, pack your bags!(线段+dp)
- codeforces Hacker, pack your bags!
- Hacker, pack your bags! CodeForces
- Round 422 C. Hacker, pack your bags!
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!(扫描线)
- Codeforces Round #422 (Div. 2) C Hacker, pack your bags! (二分orDP)
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序, 贪心, 暴力(居然可以n方暴力)
- Codeforces 822C Hacker, pack your bags!(思维+技巧)
- Codejam之Ratatouille
- ESP32 Linux开发环境搭建
- 数据结构之树的操作
- Atitit 人工智能目前的进展与未来 包含的技术 v3
- python 统计序列中元素的出现频度\统计英文文章的词频
- codeforces #422 C. Hacker, pack your bags!(贪心dp)
- 利用ScriptEngineManager实现字符串公式灵活计算
- GreenDao+ButterKnife+Retrofit+Fresco展示网络数据
- yii2 Unable to find debug data tagged with
- 运算符重载
- AIDL的基本使用
- Java之等边三角形
- SVN中如何给项目打基线
- Redis 分布式锁的正确实现方式( Java 版 )