【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(二分写法)
【题目链接】:http://codeforces.com/contest/822/problem/C
【题意】
有n个旅行计划,
每个旅行计划以开始日期li,结束日期ri,以及花费金钱costi描述;
让你在这n个旅行计划中选出两个计划;
要求这两个计划的日期没有相交的部分;
且这两个日期的总时间长度恰好为x;
让你求最小花费
【题解】
先把每个计划按照左端点第一优先级,右端点第二优先级升序排序;
然后定义一个dp[x]数组,表示在前i个计划中,时长为x,且右端点的位置< a[i].l的一个旅行计划的最小花费;
更新的时候:
len = a[i].r-a[i].l+1;
ans = min(ans,a[i].cost+dp[x-len]);
对于到达的第i个计划;
先看看有没有哪个之前哪个计划从这个位置开始才满足r< a[i].l的;用那些来更新dp数组;
然后在更新完毕之后,在找到这个第i个计划从哪个位置开始,满足a[i].r< l暂时先存着.
【Number Of WA】
1
【反思】
在写的时候,把二分答案的初始值给赋错了;
很伤心。
还是缺少经验啊。
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int INF = 2e5+100;
const int N = 2e5+100;
struct abc{
int l,r,cost;
};
int n,x,dic[INF],ans = 21e8;
abc a[N];
vector <abc> need[N];
bool cmp(abc a,abc b){
if (a.l != b.l)
return a.l < b.l;
else
return a.r < b.r;
}
int main(){
//Open();
Close();
cin >> n >> x;
rep1(i,1,n){
cin >> a[i].l >> a[i].r >> a[i].cost;
}
sort(a+1,a+1+n,cmp);
abc temp;int len;
rep1(i,1,n){
while ( (int) need[i].size()){
temp = need[i].back();
need[i].pop_back();
len = temp.r-temp.l+1;
if (!dic[len]){
dic[len] = temp.cost;
}else{
dic[len] = min(dic[len],temp.cost);
}
}
temp = a[i];
len = temp.r - temp.l+1;
if (x >= len && dic[x-len]){
ans = min(ans,temp.cost+dic[x-len]);
}
int l = i +1 ,r = n;
int idx = n+1;
while (l <= r){
int mid = (l+r)>>1;
if (a[mid].l > a[i].r){
idx = mid;
r = mid - 1;
}else
l = mid + 1;
}
need[idx].push_back(temp);
}
if (ans > (int) 20e8)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(二分写法)的更多相关文章
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序,贪心
C. Hacker, pack your bags! It's well known that the best way to distract from something is to do ...
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!(更新数组)
传送门 题意 给出n个区间[l,r]及花费\(cost_i\),找两个区间满足 1.区间和为指定值x 2.花费最小 分析 先用vector记录(l,r,cost)和(r,l,cost),按l排序,再设 ...
- Codeforces Round #422 (Div. 2)
Codeforces Round #422 (Div. 2) Table of Contents Codeforces Round #422 (Div. 2)Problem A. I'm bored ...
- 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(hash写法)
接上一篇文章; 这里直接把左端点和右端点映射到vector数组上; 映射一个open和close数组; 枚举1..2e5 如果open[i]内有安排; 则用那个安排和dp数组来更新答案; 更新答案完之 ...
- Codeforces Round #422 (Div. 2) E. Liar 后缀数组+RMQ+DP
E. Liar The first semester ended. You know, after the end of the first semester the holidays beg ...
- Codeforces Round #422 (Div. 2) B. Crossword solving 枚举
B. Crossword solving Erelong Leha was bored by calculating of the greatest common divisor of two ...
- Codeforces Round #422 (Div. 2) A. I'm bored with life 暴力
A. I'm bored with life Holidays have finished. Thanks to the help of the hacker Leha, Noora mana ...
- 【Codeforces Round #422 (Div. 2) D】My pretty girl Noora
[题目链接]:http://codeforces.com/contest/822/problem/D [题意] 有n个人参加选美比赛; 要求把这n个人分成若干个相同大小的组; 每个组内的人数是相同的; ...
- 【Codeforces Round #422 (Div. 2) B】Crossword solving
[题目链接]:http://codeforces.com/contest/822/problem/B [题意] 让你用s去匹配t,问你最少需要修改s中的多少个字符; 才能在t中匹配到s; [题解] O ...
随机推荐
- 【原创】rman备份出现ORA-19625
[oracle@sunny stage]$ rman target / Recovery Manager: Release 10.2.0.1.0 - Production on Sun Mar 18 ...
- opengl问题
1.glCreateShader(GL_VERTEX_SHADER)失败原因: 在调用glewInit()初始化之后才能create shader;
- shell-3.bash的基本功能:通配符和其他特殊字符
1. 2.
- 运维派 企业面试题6 防dos攻击
Linux运维必会的实战编程笔试题(19题) 企业实战题6:请用至少两种方法实现! 写一个脚本解决DOS攻击生产案例 提示:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到 ...
- sass的用法小结(一)
1. 使用变量; sass让人们受益的一个重要特性就是它为css引入了变量.你可以把反复使用的css属性值 定义成变量,然后通过变量名来引用它们,而无需重复书写这一属性值.或者,对于仅使用过一 次的属 ...
- 路飞学城Python-Day13
[2.常用模块-模块的种类和导入方法] 1.什么是模块? 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长.越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分 ...
- NodeJS学习笔记 (1)资源压缩-zlib(ok)
原文: https://github.com/chyingp/nodejs-learning-guide/blob/master/README.md 自己的跟进学习: 概览 做过web性能优化的同学, ...
- [HDU5686]2016"百度之星" - 资格赛 Problem B
题目大意:给你n,规定一个串中相邻的两个1可以合并为一个2(别的不行),让你求长度为n的全1串最多能变成多少种不同的串. 解题思路:我们先来找一波规律,发现n=1,2,3,4,5时答案分别为1,2,3 ...
- docker私有仓库yum安装 docker-registry
[root@riyimei-node1:/root]> yum install docker-registryLoaded plugins: fastestmirror, langpacksLo ...
- cogs 181. [USACO Jan07] 最高的牛
181. [USACO Jan07] 最高的牛 ★★ 输入文件:tallest.in 输出文件:tallest.out 简单对比时间限制:1 s 内存限制:32 MB FJ's N ( ...