cf822c Hacker,Pack your bags
来源:互联网 发布:python装饰器 编辑:程序博客网 时间:2024/06/01 12:34
给定n个区间,l,r,cost,求其中2个区间是否能凑成总长为x,并且费用最小。
没看懂题解,先记录下。
从左往右遍历,遇到左端点就更新答案,否则更新bestcost[j],长度为j天时的最小费用。
ps:n = 2e5 cost = 1e9 max = 2e14需要long long
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespacestd;
const int maxn =2e5 + 5;
typedef longlong ll;
typedef pair<int,ll> pr;
const ll INF =2e15;
ll bestcost[maxn];
vector<pr> L[maxn],R[maxn];
int main()
{
int n,x;
scanf("%d%d",&n,&x);
int l,r;
ll cost;
for (int i =0; i < n; i ++) {
scanf("%d%d",&l,&r);
cin >> cost;
L[l].push_back({r - l +1,cost});//以第l天为左端点的区间
R[r].push_back({r - l +1,cost});
}
for (int i =0; i < maxn; i ++) {
bestcost[i] =INF;
}
ll m =INF;
for (int i =0; i < maxn; i ++) {
for(int j =0;j < L[i].size();j ++)
{
if(L[i][j].first >= x) continue;
m = min(m ,bestcost[x - L[i][j].first] +L[i][j].second );
}
for (int j =0; j < R[i].size(); j ++) {
if(R[i][j].first >= x) continue;
bestcost[R[i][j].first] = min(bestcost[R[i][j].first] , R[i][j].second );
}
}
if(m ==INF) m = -1;
cout << m <<endl;
return0;
}
- cf822c Hacker,Pack your bags
- CF822C:Hacker, pack your bags!(思维)
- CF822C Hacker, pack your bags! 【思维好题】
- Hacker, pack your bags!
- codeforces Hacker, pack your bags!
- Hacker, pack your bags! CodeForces
- `Hacker, pack your bags!
- cf C. Hacker, pack your bags!
- CF422 div2 C. Hacker, pack your bags!
- codeforces 822 C Hacker, pack your bags!
- Codeforces822 C. Hacker, pack your bags!
- 822C Hacker, pack your bags!
- Round 422 C. Hacker, pack your bags!
- codeforces 882C Hacker, pack your bags!
- CodeForces 822C Hacker, pack your bags!
- 822C Hacker, pack your bags!
- Hacker, pack your bags!(线段+dp)
- Codeforces Round #422 C. Hacker, pack your bags! (二分)
- Anroid动画总结一:属性动画(Property)简单应用
- iOS UIView上利用UIBezierPath 画线 并且计算线的角度
- java synchronized详解
- 获取登录用户Session
- php二维数组增加字段
- cf822c Hacker,Pack your bags
- tp 里面 select2 筛选实例
- 常用加密与解密算法示例代码
- 软件开发工具
- 用static关键字修饰类(只适用于内部类)
- Cassandra简介
- GetOBSRef 解析
- socket C/C++编程(3)Windows&Linux之socket兼容性编程
- OSI七层模型与TCP/IP五层模型