题目大意:给你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!(思维)的更多相关文章

  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!

    题意 给出一些闭区间(始末+代价),选取两段不重合区间使长度之和恰为x且代价最低 思路 相同持续时间的放在一个vector中,内部再对起始时间排序,从后向前扫获取对应起始时间的最优代价,存在minn中 ...

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

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

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

  6. CF-822C Hacker, pack your bags! 思维题

    题目大意是给若干线段及其费用,每个线段权值即为其长度.要求找出两个不重合线段,令其权值和等于x且费用最少. 解法: 先分析一下题目,要处理不重合的问题,有重合的线段不能组合,其次这是一个选二问题,当枚 ...

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

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

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

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

随机推荐

  1. Spring中使用要点集合

    1.InitializingBean和init-method方法 Spring的InitializingBean为bean提供了定义初始化方法的方式.InitializingBean是一个接口,它仅仅 ...

  2. Square Country

    原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1073 分析:dp,dp[i]表示钱为i且恰好用完时能买的最少土地数,易知dp[i]=mi ...

  3. python学习(十一)测试和调试

    最近学习了python的错误处理和几种测试方法 1 try except 可以通过try except方式捕捉异常 try: print('try...') r = 10/0 print('resul ...

  4. vue 父子组件相互传递数据

    例子一 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

  5. 应用maven自动部署的脚本

    @(编程) 最近写了一个自动部署的脚本,可以一键部署到测试服务器或者生产服务器上,包括一个函数脚本和一个调用脚本,比较简单,记录如下. 特点如下: 部署前自动备份 可以部署tomcat项目和java项 ...

  6. spoj 1825 Free tour II

    http://www.spoj.com/problems/FTOUR2/ After the success of 2nd anniversary (take a look at problem FT ...

  7. 您是哪个等级的CSS开发人员?

    我们在不断的学习,追求进步与提高,到底学到什么程度了,到底是 不是真的了解CSS,是哪个层次了呢.我们来对照一下. 第0级:CSS?那不是一个多人射击游戏吗?  CSS? Isn't that a m ...

  8. 【CodeForces】827 D. Best Edge Weight 最小生成树+倍增LCA+并查集

    [题目]D. Best Edge Weight [题意]给定n个点m条边的带边权无向连通图,对每条边求最大边权,满足其他边权不变的前提下图的任意最小生成树都经过它.n,m<=2*10^5,1&l ...

  9. Calf Flac

    1.3.3 Calf Flac Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 223  Solved: 42[Submit][Status][Forum] ...

  10. Metasploit 使用基础

    本文将简单介绍一下Kali2 上Metasploit的一些基本使用,包括启动.更新及一个小问题"Module database cache not built yet, using slow ...