【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 ...
随机推荐
- (转载)Android中的Service:Binder,Messenger,AIDL(2)
前言 前面一篇博文介绍了关于Service的一些基本知识,包括service是什么,怎么创建一个service,创建了一个service之后如何启动它等等.在这一篇博文里有一些需要前一篇铺垫的东西,建 ...
- 51nod 1785 数据流中的算法 (方差计算公式)
1785 数据流中的算法 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 51nod近日上线了用户满意度检测工具,使用高级人工智能算法,通过用户访问时间.鼠 ...
- 3ds Max修改桌面快捷方式为中文语言
通过上篇教程的学习,我们已经会切换3ds Max 2018的界面语言了,相关内容可参阅3ds Max如何设置中文界面.但是如果每次都要从开始菜单进行操作,岂不麻烦?简单一点,可以在桌面添加3ds Ma ...
- 每个IT安全专业人员应该知道的12种根本漏洞
每个IT安全专业人员应该知道的12种根本漏洞 每年,IT安全专业人员都面临着数千个新的软件漏洞和数百万个不同的恶意软件程序,但只有12种根本漏洞会让这些软件漏洞和恶意软件程序攻击你的设备.了解这些根本 ...
- layui Layui-Select多选的使用和注意事项
1.最近买了layadmin的后台框架,使用Layui-Select总结如下 A.配置:我采用的全局引入配置的方式 赋值(选中状态)
- pip 出错
pip 升级到10以上出错 ImportError: cannot import name 'main' 解决方法一: 降低pip的版本号 python -m pip install pip==9.0 ...
- php把数据表导出为Excel表的最简单、最快的方法(不用插件)
亲测可用,把下面的数据换成自己的即可 <?php header("Content-type:application/vnd.ms-excel");header("C ...
- HDU 4945 2048 DP 组合
思路: 这个题写了一个背包的解法,超时了.搜了下题解才发现我根本不会做. 思路参见这个: 其实我们可以这样来考虑,求补集,用全集减掉不能组成2048的集合就是答案了. 因为只要达到2048就可以了,所 ...
- jQuery实现点击图标div循环放大缩小功能
很基础的一个功能,点击左下角的图标按钮,地图的整个div会变大,变大预览之后,再次点击图标按钮,地图的整个div会变小,恢复原样,两个图标在地图界面的放大和缩小时间不断的切换图标状态(箭头向里面,或者 ...
- 【BZOJ 1059】[ZJOI2007]矩阵游戏
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 最后要求对于所有的i,a[i][i]=1 那么,如果第i行的第j列为1. 就说明我们可以把这个第i行换到第j行. 因为这样的话,a[ ...