题目大意是给若干线段及其费用,每个线段权值即为其长度。要求找出两个不重合线段,令其权值和等于x且费用最少。

解法:

先分析一下题目,要处理不重合的问题,有重合的线段不能组合,其次这是一个选二问题,当枚举其中一条线段时,另一条合法线段的必要条件“权值”可以直接得出。

对于第一个问题,想到先对线段根据l进行排序,这样每次枚举一个线段的时候,如果在它的l之后有一个合法线段,我们只要标记一下x-LenNow,待枚举到那个合法线段的时候自然就判断出来了。如果在它之前有一个合法线段符合条件,根据刚刚的做法我们自然可以处理。现在问题就是,万一它不合法,它重合呢?解决方法就是延迟标记它。对于线段们,我们已经对l排序,那么一旦枚举到一条线段的l大于等于之前待标记的某线段的r,那它及之后的线段都不会与它重合,而之前的线段(标记线段之后的线段)一定与它重合。所以我们维护一个优先队列,以待标记的r进行排序,每次枚举前,根据枚举到线段的l将部分待标记物执行标记即可。

撤了那么多,看看代码吧。

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#define LL long long int
using namespace std;
struct node
{
LL r,len,c;
friend bool operator < (node a,node b)
{
return a.r>b.r; }
};//用于延迟标记
struct cd
{
LL l,r,c;
};
LL mi[];
const LL inf=;
vector<cd> v;
bool cmp(cd a,cd b)
{
if(a.l==b.l)
return a.r<b.r;
return a.l<b.l;
}
int main()
{
LL n,x;
while(cin>>n>>x)
{
LL a,b,c;
LL ans=inf;
v.clear();
fill(mi,mi+,inf);
priority_queue<node> upt;
for(LL i=;i<n;i++)
{
cin>>a>>b>>c;
v.push_back((cd){a,b,c});
}
sort(v.begin(),v.end(),cmp);
for(int i=;i<v.size();i++)
{
while(!upt.empty())
{
node check=upt.top();
if(v[i].l<=check.r) break;
upt.pop();
if(mi[check.len]>check.c)
mi[check.len]=check.c;
}
cd now=v[i];
LL len=now.r-now.l+;
LL f=x-len;
upt.push((node){now.r,len,now.c});
if(f<=) continue;
if(now.c+mi[f]<ans) ans=now.c+mi[f];
}
if(ans!=inf)
cout<<ans<<endl;
else
cout<<-<<endl;
}
return ;
}

CF-822C Hacker, pack your bags! 思维题的更多相关文章

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

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

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

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

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

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

  6. CodeForces 822C Hacker, pack your bags!

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

  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! 排序,贪心

    C. Hacker, pack your bags!     It's well known that the best way to distract from something is to do ...

  9. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

随机推荐

  1. Python3基础 list del 从内存中删除整个列表

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. 网络存储结构简明分析—DAS、NAS和SAN 三者区别

    存储的总体分类     主流存储结构   网络存储结构大致分为三种:直连式存储(DAS:Direct Attached Storage).存储区域网络(SAN:Storage Area Network ...

  3. centos6.8下如何升级git版本?

    1. 安装开发环境 yum install -y curl curl-devel zlib-devel openssl-devel perl cpio expat-devel gettext-deve ...

  4. centos 编译lantrn

    github上的安装指导: Custom fork of Go is currently required. We'll eventually switch to Go 1.7 which suppo ...

  5. 解决/var/log下没有messages文件的问题?

    fedora23和centos7+ 都是使用的 systemd 来代替sysv 管理系统启动和服务了. 在systemd 中主要包含两个方面的内容, 当打开/etc/inittab 文件时, 会看到: ...

  6. <OFFER15> 15_NumberOf1InBinary

    // 面试题15:二进制中1的个数 // 题目:请实现一个函数,输入一个整数,输出该数二进制表示中1的个数.例如 // 把9表示成二进制是1001,有2位是1.因此如果输入9,该函数输出2. #inc ...

  7. .NET Standard vs. .NET Core

    What is the difference between .NET Core and .NET Standard Class Library project types? Answer1 When ...

  8. C++笔记(2017/2/9)

    this指针 this指针作用就是指向成员函数所作用的对象. 非静态成员函数中可以直接使用this来代表指向该函数作用的对象的指针. 静态成员函数中不能使用this指针. 静态成员 static 定义 ...

  9. Java创建对象的几种方式。

    Java创建对象的几种方式(重要): (1) 用new语句创建对象,这是最常见的创建对象的方法. (2) 运用反射手段,调用java.lang.Class或者java.lang.reflect.Con ...

  10. Educational Codeforces Round 23 E. Choosing The Commander trie数

    E. Choosing The Commander time limit per test 2 seconds memory limit per test 256 megabytes input st ...