Codeforces 822 C. Hacker, pack your bags! 思维
来源:互联网 发布:freemind软件 编辑:程序博客网 时间:2024/06/02 07:17
传送门:Codeforces 822C
题意:给出n组l r c,定义duration为r-l+1,找出两组使其duration之和为x,并且(l,r)不相交,求c之和最小是多少。
思路:一开始我按duration排序,然后二分瞎搞了半天,不是wa就是tle。。
正解:将输入的端点按大小排序,然后遍历,如果是右端点就更新num数组,如果是左端点就更新答案。
num[i]保存的是已经遍历过的完整区间里,duration为i的最小的c。
代码:
#include<bits/stdc++.h>using namespace std;#define ll long long#define inf 0x3f3f3f3fstruct node{int l,r,c,k,id;}a[200005];pair<int,int>p[400005];int main(){memset(num,inf,sizeof(num)); int n,x,cnt=0; cin>>n>>x; for(int i=0;i<n;i++) { scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].c); a[i].k=a[i].r-a[i].l+1; p[cnt++]=make_pair(a[i].l,-(i+1)); p[cnt++]=make_pair(a[i].r,i+1); } sort(p,p+cnt); int ans=2000000001; for(int i=0;i<cnt;i++) { if(p[i].second>0) { int id=p[i].second-1; num[a[id].k]=min(num[a[id].k],a[id].c); } else { int id=(-p[i].second)-1; int t=x-a[id].k; if(num[t]!=inf&&t>=0) ans=min(ans,num[t]+a[id].c); } } if(ans==2000000001) ans=-1; cout<<ans<<endl;}
阅读全文
0 0
- 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!
- CodeForces 822C Hacker, pack your bags!
- codeforces 882C Hacker, pack your bags!
- Codeforces 822C 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 (Div. 2) C. 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)
- 一题多解 —— $?(命令返回状态)的检验
- React 基本事件用法
- POJ 3098 Frugal Search 笔记
- 88. Merge Sorted Array
- C#---ADO.net
- Codeforces 822 C. Hacker, pack your bags! 思维
- (OK) MIMP
- Ubuntu16.04 +cuda8.0+cudnn+opencv+caffe+theano+tensorflow配置明细
- HttpUrlconnectionPsot方法请求listview展示
- 关于Jquery版本的Ajax和SpringMVC拦截器(Interceptor)
- 数据库 第一、第二、第三范式说明
- SQL的having和where的区别
- 【GDOI2018模拟7.9】期末考试
- Python编码问题