Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!
来源:互联网 发布:淘宝网女裤 编辑:程序博客网 时间:2024/06/15 02:59
题意:给你n个区间,区间的价值为r-l+1,花费cost,求任意两个不相交的区间价值和为x的最小花费是多少。
思路:这题很有意思,看了别人的思路发现用到了没用过的技巧,把区间拆成两个点起点和终点,然后按点从小到大排序,每次扫到的点如果是右端点则更新该区间价值的最小花费,即如果该区间的价值为value,则dp[value]=min(dp[value],cost),如果是左端点则用该区间的花费和前面已知的最小花费求出当前花费来更新答案。具体操作看代码:
#include<iostream> #include<cmath> #include<queue> #include<cstdio> #include<queue> #include<algorithm> #include<cstring> #include<string> #include<utility>#include<map>#include<stack>#include<vector>#define maxn 200005#define inf 0x3f3f3f3fusing namespace std;typedef long long LL;const double eps = 1e-8;const int mod = 1e9 + 7;struct node{int index, mark, value, cost;}p[maxn << 1];bool cmp(node a, node b){return a.index < b.index || (a.index == b.index&&a.mark < b.mark);}int dp[maxn];int main(){int n, x;scanf("%d%d", &n, &x);int number = 0;while (n--){int l, r, cost;scanf("%d%d%d", &l, &r, &cost);p[number].index = l;p[number + 1].index = r;p[number].mark = 0;p[number + 1].mark = 1;p[number].value = p[number + 1].value = r - l + 1;p[number].cost = p[number + 1].cost = cost;number += 2;}sort(p, p + number, cmp);memset(dp, inf, sizeof(dp));int ans = inf << 1;for (int i = 0; i < number; i++){if (p[i].mark)dp[p[i].value] = min(dp[p[i].value], p[i].cost);else{int value = x - p[i].value;if (value >= 0 && dp[value] != inf)ans = min(ans, dp[value] + p[i].cost);}}ans = ans == inf << 1 ? -1 : ans;printf("%d\n", ans);}
阅读全文
0 0
- Codeforces Round #422 (Div. 2) 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 Round #422 (Div. 2) C. Hacker, pack your bags!(扫描线)
- Codeforces Round #422 (Div. 2) C Hacker, pack your bags! (二分orDP)
- Codeforces Round #422 C. Hacker, pack your bags! (二分)
- Round 422 C. Hacker, pack your bags!
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序, 贪心, 暴力(居然可以n方暴力)
- Hacker, pack your bags! Codeforces Round #422 (Div. 2) (贪心)
- codeforces 822 C Hacker, pack your bags!
- codeforces 882C Hacker, pack your bags!
- CodeForces 822C Hacker, pack your bags!
- codeforces #422 C. Hacker, pack your bags!(贪心dp)
- codeforces Hacker, pack your bags!
- Hacker, pack your bags! CodeForces
- Codeforces 822C Hacker, pack your bags!(思维+技巧)
- Codeforces 822C Hacker, pack your bags!【排序+二分】
- codeforces 822 C. Hacker, pack your bags!(思维+dp)
- Leetcode 213. House Robber II
- 51Nod 旋转字符串
- WebService到底是什么?
- js中Uncaught TypeError: Cannot read property 'substr' of undefined问题分析
- Java或.NET该怎么选择?
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!
- eval
- centos6.5下安全架设FTP服务器
- JAVA多线程实现的三种方式
- 堆优化dijkstra bzoj2143飞飞侠
- 1012. The Best Rank (25)
- 前端构建工具-fis3使用入门
- Android RxJava初步认识
- Mycat配置文件schema.xml的详解