【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 ...
随机推荐
- (转载)Mac系统下利用ADB命令连接android手机并进行文件操作
Mac系统下利用ADB命令连接android手机并进行文件操作 标签: Mac adb android 2016-03-14 10:09 5470人阅读 评论(1) 收藏 举报 分类: Androi ...
- 推箱子小游戏《格鲁的实验室》13关 - bfs最短路径
下载了一款推箱子小游戏,第13关的时候怎么也破不了最佳纪录(最少步数是9而我们最好的方案是10步),因为数据比较小(6*8的方阵),所以写了个BFS来找最短路. 游戏的目标是把小黄人推到黄色球,小绿人 ...
- maven/ssm框架搭建
好久没有写java了,昨天学了下maven,不用手动的下载和添加jar包,实在是太方便. ------------------------------------------------------- ...
- swift语言点评三 - Basic Operators
1.Tuples are compared from left to right, one value at a time, until the comparison finds two values ...
- freemarker加载模板文件的
java代码: public String getContent(String name, HashMap<String, Object> paramMap) { //home 文件路径 ...
- java的selenium环境搭建
1.下载jdk1.8 环境变量我的博客有我就不说 selenium下载地址:http://npm.taobao.org/mirrors/selenium 2.下 ...
- CodeForces-920E Connected Components? 广度搜索 双向链表 判断联通 大量重复节点的删除
题目链接:https://cn.vjudge.net/problem/CodeForces-920E 题意 给一个补图,问各个联通块有几个元素,升序排列 注意maxn=2e5, maxm=2e10 思 ...
- [CTSC2007][APIO2007]数据备份Backup
题目:BZOJ1150.codevs1615.洛谷P3620 题目大意:有n个点,k条链,每个点离原点有一定的距离.要你用k条链连接2k个点,使得k条链的长度最短. 解题思路:毕竟是CTSC级别的题目 ...
- 使用InstelliJ IDEA创建Web应用程序
环境版本 Windows 8.1IDE:InstelliJ IDEA 13 Spring:Spring 4.1.1 & Spring MVC 4.1.1 WebLogic 10.3 ...
- 网页里如何使用js屏蔽鼠标右击事件
图片.png 在后台管理系统里面,遇到了这样的一个问题,右击ztree菜单,弹出修改界面,但是,现在确实这样的,右击默认弹出功能提示的框框,看上去似乎很影响自己想要的功能,只能禁用了,那么,网页里如何 ...