题意

给出一些闭区间(始末+代价),选取两段不重合区间使长度之和恰为x且代价最低

思路

相同持续时间的放在一个vector中,内部再对起始时间排序,从后向前扫获取对应起始时间的最优代价,存在minn中,对时间 i 从前向后扫,在对应的k-i中二分找第一个不重合的区间,其对应的minn加上 i 的cost即为出发时间为 i 时的最优解

代码

#include<bits/stdc++.h>
using namespace std;
int n, k;
struct EVE{
int st,ed,val;
EVE(){
}
EVE(int a,int b, int c){
st = a, ed = b, val = c;
}
};
int f,t,c;
vector<EVE> v[];
vector<int> minn[];
int tmp[];
bool cmp(EVE a, EVE b){
return a.st<b.st;
}
int main(){
scanf("%d%d",&n,&k);
for(int i = ;i<n;i++){
scanf("%d%d%d",&f,&t,&c);
if(t-f+ >= k) continue;
v[t-f+].push_back({f,t,c});
}
for(int i = ;i<=k;i++) sort(v[i].begin(),v[i].end(),cmp);
for(int i = ;i<=k;i++){
for(int j = v[i].size()-;j>=;j--){
if(j==v[i].size()-) tmp[j]=v[i][j].val;
else tmp[j]=min(v[i][j].val,tmp[j+]);
}
for(int j = ;j<v[i].size();j++){
minn[i].push_back(tmp[j]);
}
}
long long ans = 1e12;
for(int i = ;i<=k;i++){
if(v[k-i].empty()) continue;
for(int j = ;j<v[i].size();j++){
int ed = v[i][j].ed;
long long cost = v[i][j].val;
int le = , ri = v[k-i].size()-;
if(v[k-i][ri].st<=ed) continue;
int mid = le+ri>>;
while(le<ri){
mid = le+ri>>;
if(v[k-i][mid].st<=ed) le = mid+;
else ri = mid;
}
ans = min(ans, cost+minn[k-i][le]);
}
}
if(ans == 1e12) printf("-1");
else printf("%I64d",ans);
return ;
}

CodeForces 822C Hacker, pack your bags!的更多相关文章

  1. CodeForces 754D Fedor and coupons&&CodeForces 822C Hacker, pack your bags!

    D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  2. Codeforces 822C Hacker, pack your bags! - 贪心

    It's well known that the best way to distract from something is to do one's favourite thing. Job is ...

  3. Codeforces 822C Hacker, pack your bags!(思维)

    题目大意:给你n个旅券,上面有开始时间l,结束时间r,和花费cost,要求选择两张时间不相交的旅券时间长度相加为x,且要求花费最少. 解题思路:看了大佬的才会写!其实和之前Codeforces 776 ...

  4. Codefroces 822C Hacker, pack your bags!

    C. Hacker, pack your bags! time limit per test 2 seconds memory limit per test 256 megabytes input s ...

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

  6. CF822C Hacker, pack your bags!(思维)

    Hacker, pack your bags [题目链接]Hacker, pack your bags &题意: 有n条线段(n<=2e5) 每条线段有左端点li,右端点ri,价值cos ...

  7. Codeforces822 C. Hacker, pack your bags!

    C. Hacker, pack your bags! time limit per test 2 seconds memory limit per test 256 megabytes input s ...

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

    [题目链接]:http://codeforces.com/contest/822/problem/C [题意] 有n个旅行计划, 每个旅行计划以开始日期li,结束日期ri,以及花费金钱costi描述; ...

  9. codeforces 822 C. Hacker, pack your bags!(思维+dp)

    题目链接:http://codeforces.com/contest/822/submission/28248100 题解:多维的可以先降一下维度sort一下可以而且这种区间类型的可以拆一下区间只要加 ...

随机推荐

  1. Spring的AOP原理

    转自 https://www.tianmaying.com/tutorial/spring-aop AOP是什么? 软件工程有一个基本原则叫做“关注点分离”(Concern Separation),通 ...

  2. 分享一个关于Opencv的小总结

    import cv2   #opencv读取的格式是BGR import numpy as np 一.#读入文件 img=cv2.imread('cat.jpg') #’’引号内是图片所在盘的地址+名 ...

  3. Leetcode之148. Sort List Medium

    https://leetcode.com/problems/sort-list/ Sort a linked list in O(n log n) time using constant space ...

  4. VS2017.常量中有换行符

    1.VS中加入 “/utf-8” Qt中 也是加入“/utf-8”,加的地方注意下:在 pro文件中 ,这个位置加入: win32-msvc*:QMAKE_CXXFLAGS += /wd"4 ...

  5. JavaScript form提交汉字乱码

    <script type="text/javascript"> var test1="http://www.w3school.com.cn/My first/ ...

  6. MySQL(六) 索引

    索引与优化 1.选择索引的数据类型 MySQL支持很多数据类型,选择合适的数据类型存储数据对性能有很大的影响.通常来说,可以遵循以下一些指导原则: (1)越小的数据类型通常更好:越小的数据类型通常在磁 ...

  7. Acquire and Release Fences

    转载自:   http://preshing.com/20130922/acquire-and-release-fences/ Acquire and release fences, in my op ...

  8. PPT 中用 LaTeX 插入公式、PowerPoint 中用 LaTeX 插入公式(4)

    步骤: 1. 安装 CTex 2. 安装 IguanaTex >> 下载链接1:官网 >> 下载链接2:复制链接到迅雷或IDM下载很快 3. 将「IguanaTex_v1_56 ...

  9. VMware的下载与安装

    VMware的下载与安装 一.虚拟机的下载 1.进入VMware官网,点击左侧导航栏中的下载,再点击图中标记的Workstation Pro,如下图所示. 2.根据操作系统选择合适的产品,在这里以Wi ...

  10. 关联安装 mysql ,zabbix , nginx, php

    /usr/local/zabbix-3.2.6 /usr/local/php-5.6.3 /usr/local/mysql-5.7.26 安装mysql mv /etc/yum.repos.d/Cen ...