【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 ...
随机推荐
- 脱离node自己使用普通的requirejs管理js资源
首先,工程目录: 现在主页面(web框架写法.html): <!DOCTYPE html> <html lang="en"> <head> &l ...
- ftp 一个账号多个家目录的解决方案
通常,配置ftp时,一个ftp账号只对应一个家目录,不能有多个家目录的情况. 但是,根据公司开发项目的需求,需要做到一个ftp对应多个开发目录.有想过创建软链接的,可是发现通过ftp是访问不了的. 举 ...
- 利用js自带函数 数组去重
<script> ,,]; //原数组 var a=[]; //定义空数组 arr.map(function(x){ //用 map 遍历数组 ){ //如果当前值没有存在空数组中 a.p ...
- luogu P4137 Rmq Problem / mex(可持久化线段树)
一开始想的是莫队,然后维护几个bitset,然后瞎搞.脑子里想了想实现,发现并不好写. 还是主席树好写.我们维护一个权值的线段树,记录每一个权值的最后一次出现的位置下标.我们查询的时候要在前\(r\) ...
- FastDFS图片服务器搭建
*FastDFS图片服务器搭建准备:1.需要libfastcommon安装包 选择最新稳定版(libfastcommon-1.0.36.tar.gz)2.需要FastDFS安装包 选择最新稳定版(fa ...
- caioj 1154 同余方程(模版)
求x的最小正整数解,使得ax=b(mod m) 那么显然ax - b = m * y ax - my = b 那么就套入Ax+By = K的不定方程中,然后用exgcd求解即可 但这道题求最大正整数解 ...
- Linux进程管理之状态(二)
二.进程的生命周期 进程是一个动态的实体,所以他是有生命的.从创建到消亡,是一个进程的整个生命周期.在这个周期中,进程可能会经历各种不同的状态.一般来说,所有进程都要经历以下的3个状态: 就绪态.指进 ...
- Hibernate持久化步骤
1. 读取并解析配置文件 Configuration config= new Configuration().configure(); 相当于使用DataSource获取连接前读取DataSour ...
- GET和POST请求的核心区别
GET请求具有幂等性,而POST请求没有.
- 国庆 day 7 下午
思路:见博客. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...