codeforces 822 C Hacker, pack your bags!
2017-07-03 15:25
525 查看
题目:http://codeforces.com/contest/822/problem/C
大致题意:
给出n个区间和cost,以及一个数x,从n个区间取出两个不重合的区间使得他们的区间大小恰好为x, 并使得两个cost和最小
解法:
对于每个区间,一个区间起点之前的区间都与这个区间不重合,用一个数组动态记录这个区间之前的所有区间大小对应的cost最小值,那么就可以求出这个区间和之前区间和为x的最小值。
代码:
大致题意:
给出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 ,ri ; int l ,r ; long long c ; int n,x; long long dp ; 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; }
相关文章推荐
- 【Codeforces 822 C. Hacker, pack your bags!】+ pair
- Codeforces 822 C. Hacker, pack your bags! 思维
- codeforces 822 C. Hacker, pack your bags!(思维+dp)
- CodeForces 822C Hacker, pack your bags!
- Codeforces 822C: Hacker, pack your bags!【二分查找】【后缀最小值】
- CodeForces - 822C Hacker, pack your bags!(区间端点处理技巧+思维)
- Codeforces 822C Hacker, pack your bags!(思维+技巧)
- Codeforces 822C Hacker, pack your bags!【排序+二分】
- Codeforces 822C - Hacker, pack your bags!(二分)
- C - Hacker, pack your bags! Codeforces(贪心)
- Codeforces 822C Hacker, pack your bags! - 贪心
- Hacker, pack your bags! CodeForces - 822C
- 【线段问题创新思路】Hacker, pack your bags! CodeForces - 822C
- A - Hacker, pack your bags! CodeForces - 822C(思路,排序)
- 822C Hacker, pack your bags!
- cf #422 c Hacker, pack your bags! 【贪心】
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序, 贪心, 暴力(居然可以n方暴力)
- Codefroces 822C Hacker, pack your bags!
- Hacker, pack your bags!
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!