codeforces 822 C Hacker, pack your bags!
来源:互联网 发布:linux arping 编辑:程序博客网 时间:2024/06/11 09:33
题目:http://codeforces.com/contest/822/problem/C
大致题意:
给出n个区间和cost,以及一个数x,从n个区间取出两个不重合的区间使得他们的区间大小恰好为x, 并使得两个cost和最小
解法:
对于每个区间,一个区间起点之前的区间都与这个区间不重合,用一个数组动态记录这个区间之前的所有区间大小对应的cost最小值,那么就可以求出这个区间和之前区间和为x的最小值。
代码:
#include <iostream>#include <vector>#include <algorithm>using namespace std;const int N = 2e5+7;const long long INF = 2e9+7;vector<int> li[N],ri[N];int l[N],r[N];long long c[N];int n,x;long long dp[N];int main(){ cin >> n >> x; for(int i = 0 ; i < n ; i++){ cin >> l[i] >> r[i] >> c[i]; li[l[i]].push_back(i); ri[r[i]].push_back(i); } fill(dp,dp+N,INF); long long ans = INF; for(int i = 1 ; i < N ; i++){ for(auto j : li[i]){ int d = r[j] - l[j] + 1; int m = x - d; if(m<1) continue; ans = min(ans,dp[m]+c[j]); } for(auto j : ri[i]){ int d = r[j] - l[j] + 1; dp[d] = min(dp[d],c[j]); } } if(ans == INF) cout << "-1" << endl; else cout << ans << endl;}
阅读全文
0 0
- codeforces 822 C Hacker, pack your bags!
- CodeForces 822C Hacker, pack your bags!
- codeforces 882C Hacker, pack your bags!
- Codeforces 822C Hacker, pack your bags!(思维+技巧)
- Codeforces 822C Hacker, pack your bags!【排序+二分】
- codeforces 822 C. Hacker, pack your bags!(思维+dp)
- Codeforces 822 C. Hacker, pack your bags! 思维
- 【Codeforces 822 C. Hacker, pack your bags!】+ pair
- Codeforces 822C Hacker pack your bags! 区间
- codeforces Hacker, pack your bags!
- Hacker, pack your bags! CodeForces
- 822C Hacker, pack your bags!
- 822C 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!
- codeforces #422 C. Hacker, pack your bags!(贪心dp)
- Codeforces 822C: Hacker, pack your bags!【二分查找】【后缀最小值】
- 走向大神之路的必备git命令操作
- 对 jquery.carousel.js 进行修改
- AWS-G2.2xlarge的Redhat7.2上安装Nvidia驱动、CUDA7.5和cudnn4.0
- python import用法
- HBuilder自定义dialog
- codeforces 822 C Hacker, pack your bags!
- standardUserDefaults 的使用
- GCD的理解与使用
- hdu_round1-1007 吃巧克力(计算几何)
- Firefox关闭GET请求到detectportal.firefox.com网站的方法
- 页面中多组切换按钮— 事件不同
- angularjs1.*返回跳转到上一页代码配置
- 朋友圈那个随便辞职的年轻人,后来活成了什么样?
- Android的插件化和组件化