【题目链接】: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!(二分写法)的更多相关文章

  1. 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 ...

  2. 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排序,再设 ...

  3. Codeforces Round #422 (Div. 2)

    Codeforces Round #422 (Div. 2) Table of Contents Codeforces Round #422 (Div. 2)Problem A. I'm bored ...

  4. 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(hash写法)

    接上一篇文章; 这里直接把左端点和右端点映射到vector数组上; 映射一个open和close数组; 枚举1..2e5 如果open[i]内有安排; 则用那个安排和dp数组来更新答案; 更新答案完之 ...

  5. 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 ...

  6. Codeforces Round #422 (Div. 2) B. Crossword solving 枚举

    B. Crossword solving     Erelong Leha was bored by calculating of the greatest common divisor of two ...

  7. 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 ...

  8. 【Codeforces Round #422 (Div. 2) D】My pretty girl Noora

    [题目链接]:http://codeforces.com/contest/822/problem/D [题意] 有n个人参加选美比赛; 要求把这n个人分成若干个相同大小的组; 每个组内的人数是相同的; ...

  9. 【Codeforces Round #422 (Div. 2) B】Crossword solving

    [题目链接]:http://codeforces.com/contest/822/problem/B [题意] 让你用s去匹配t,问你最少需要修改s中的多少个字符; 才能在t中匹配到s; [题解] O ...

随机推荐

  1. hiho1116 - 数据结构 线段树(区间合并)

    题目链接 现在有一个有n个元素的数组a1, a2, ..., an. 记f(i, j) = ai * ai+1 * ... * aj. 初始时,a1 = a2 = ... = an = 0,每次我会修 ...

  2. STM8S103内存详析

    STM8S103的RAM有1k,0x00-0x3FF(RAM和ROM统一编址),其中0x200-0x3ff共512个字节默认为堆栈,剩余的低端512个字节又分为了Zero Page和剩余的RAM(简称 ...

  3. Swift 中的协议

    Swift 中的协议协议是为方法.属性等定义一套规范,没有具体的实现,类似于Java中的抽象接口,它只是描述了方法或属性的骨架,而不是实现.方法和属性实现还需要通过定义类,函数和枚举完成. 协议定义 ...

  4. 《Unix环境高级编程》读书笔记 第8章-进程控制

    1. 进程标识 进程ID标识符是唯一.可复用的.大多数Unix系统实现延迟复用算法,使得赋予新建进程的ID不同于最近终止所使用的ID ID为0的进程通常是调度进程,也常被称为交换进程.它是内核的一部分 ...

  5. dedecmsV5.7自定义图片字段调用方法

    正常情况下,在列表页(也就是 {dede:list}标签)调用附加的图片类型字段则会出现Fatal error: Call to a member function GetInnerText() on ...

  6. [arc086e]snuke line

    题意: 有n个区间,询问对于$1\leq i\leq m$的每个i,有多少个区间至少包含一个i的倍数? $1\leq N\leq 3\times 10^5$ $1\leq M\leq 10^5$ 题解 ...

  7. centos7 命令

    firewall-cmd --zone=public --add-port=80/tcp --permanent 开启端口 systemctl stop firewalld.service  关闭服务 ...

  8. Docker学习总结(12)——非常详细的 Docker 学习笔记

    一.Docker 简介 Docker 两个主要部件: Docker: 开源的容器虚拟化平台 Docker Hub: 用于分享.管理 Docker 容器的 Docker SaaS 平台 -- Docke ...

  9. 洛谷 P1990 覆盖墙壁

    P1990 覆盖墙壁 题目描述 你有一个长为N宽为2的墙壁,给你两种砖头:一个长2宽1,另一个是L型覆盖3个单元的砖头.如下图: 0 0 0 00 砖头可以旋转,两种砖头可以无限制提供.你的任务是计算 ...

  10. hdu 2037 贪心

    今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...