Codeforces 822C Hacker, pack your bags!(思维)
题目大意:给你n个旅券,上面有开始时间l,结束时间r,和花费cost,要求选择两张时间不相交的旅券时间长度相加为x,且要求花费最少。
解题思路:看了大佬的才会写!其实和之前Codeforces 776C的写法有点像,遍历l,设以l为起始时间时长为time,看是否存在时长为x-time且与当前时段不相交的时间段,取最小值。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int N=2e5+;
typedef pair<int,int>pii; vector<pii>v[N];//存放拿来拼凑的区段
vector<pii>rc[N];//用来存放接下来要遍历的区段
int mp[N]; int main(){
ios::sync_with_stdio(false);
int n,x;
cin>>n>>x;
for(int i=;i<=n;i++){
int l,r,cost;
cin>>l>>r>>cost;
//以l为索引存放区段
rc[l].push_back(pii(r,cost));
}
int ans=2e9+;
//遍历l的值
for(int l=;l<=2e5;l++){
for(int i=;i<rc[l].size();i++){
int r=rc[l][i].first;
int cost=rc[l][i].second;
int time=r-l+;
if(time>x)
continue;
//看是否存在与这段时间相加和为x,且不想交的时段
if(mp[x-time]){
ans=min(ans,mp[x-time]+cost);
}
//遍历过的放到左边区域
v[r].push_back(pii(time,cost));
}
//找v[l],也就是左边区域的v[r],因为下一次l2=l+1,肯定大于r,所以区间不会相交,可以直接拿来用
for(int i=;i<v[l].size();i++){
int time=v[l][i].first;
int cost=v[l][i].second;
if(!mp[time]||mp[time]>cost)
mp[time]=cost;
}
}
if(ans==2e9+)
cout<<-<<endl;
else
cout<<ans<<endl;
}
Codeforces 822C Hacker, pack your bags!(思维)的更多相关文章
- 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 ...
- 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 ...
- CodeForces 822C Hacker, pack your bags!
题意 给出一些闭区间(始末+代价),选取两段不重合区间使长度之和恰为x且代价最低 思路 相同持续时间的放在一个vector中,内部再对起始时间排序,从后向前扫获取对应起始时间的最优代价,存在minn中 ...
- CF822C Hacker, pack your bags!(思维)
Hacker, pack your bags [题目链接]Hacker, pack your bags &题意: 有n条线段(n<=2e5) 每条线段有左端点li,右端点ri,价值cos ...
- 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 ...
- CF-822C Hacker, pack your bags! 思维题
题目大意是给若干线段及其费用,每个线段权值即为其长度.要求找出两个不重合线段,令其权值和等于x且费用最少. 解法: 先分析一下题目,要处理不重合的问题,有重合的线段不能组合,其次这是一个选二问题,当枚 ...
- 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 ...
- 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 ...
- codeforces 822 C. Hacker, pack your bags!(思维+dp)
题目链接:http://codeforces.com/contest/822/submission/28248100 题解:多维的可以先降一下维度sort一下可以而且这种区间类型的可以拆一下区间只要加 ...
随机推荐
- 【BZOJ3563/BZOJ3569】DZY Loves Chinese I/II(随机化,线性基)
[BZOJ3563/BZOJ3569]DZY Loves Chinese I/II(随机化,线性基) 题面 搞笑版本 正经版本 题面请自行观赏 注意细节. 题解 搞笑版本真的是用来搞笑的 所以我们来讲 ...
- 光荣之路测试开发面试linux考题之四:性能命令
Hi,大家好我是tom,I am back.今天要给大家讲讲linux系统一些性能相关命令. 1.fdisk 磁盘管理 是一个强大的危险命令,所有涉及磁盘的操作都由该命令完成,包括:新增磁盘.增删改磁 ...
- Hadoop1重新格式化HDFS
注意:原来的数据全部被清空了.产生了一个新的hdfs 1. 删除已有目录 1.1 查看hdfs-site.xml 将 dfs.name.dir和dfs.data.dir所指定的目录删除 vim hdf ...
- 安装黑苹果的config.plist
前提条件:有mac真机.目前在测试虚拟机可行性 第一步:制作U盘启动盘 1.在 app store 下载 mac OS sierra 镜像 2.格式化 U 盘,gpt 格式 3.执行以下命令(具体名称 ...
- Socket初识2
一.Socket一些概念 sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) 1.1 参数1:Socket Families(地址簇) / ...
- 类的起源与metaclass
一.概述 我们知道类可以实例化出对象,那么类本身又是怎么产生的呢?我们就来追溯一下类的起源. 二.类的起源 2.1 创建一个类 class Foo(object): def __init__(self ...
- Spring 源码学习(3) —— 增加属性注册编辑器
创建一个实体类UserManager: /** * @filename: UserManager.java * @desc 增加属性编辑器功能测试实体类 * @author: Wang Chinda ...
- Jquery中find与each方法使用详解
本文实例讲述了jQuery中find与each方法用法.分享给大家供大家参考.具体如下: 一.find()方法 jquery选择器非常强大,利用css的命名规约,可以更快更方便的找出想要的元素. 图解 ...
- Python学习笔记(四十九)爬虫的自我修养(一)
论一只爬虫的自我修养 URL的一般格式(带括号[]的为可选项): protocol://hostname[:port]/path/[;parameters][?query]#fragment URL由 ...
- 51nod1056 最长等差数列 V2
基准时间限制:8 秒 空间限制:131072 KB 分值: 1280 N个不同的正整数,从中选出一些数组成等差数列. 例如:1 3 5 6 8 9 10 12 13 14 等差子数列包括(仅包括 ...