Hacker, pack your bags

【题目链接】Hacker, pack your bags

&题意:

有n条线段(n<=2e5) 每条线段有左端点li,右端点ri,价值cost(1 <= li <= ri <= 2e5, cost <= 1e9)

对于一个给定的x(x <= 2e5),寻找两个不相交的线段,使它们的长度和恰好为x,并且价值和最小

&题解:

只有2个线段,并且他们的和是定值x.但是还有另外一个条件,他们的区间不相交,这个我们可以通过排序左端点l来实现,当我们排序完l,那么任意一个r在这个l之前的都可以与这条线段相组合,那么可以设一个mi[i]:表示线段长度为i时的最小花费,mi[i]可以通过r是否在这个l之前来更新.

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define ll long long
#define fo(i,a,b) for(int i=(a);i<=(b);i++)
#define fd(i,a,b) for(int i=(a);i>=(b);i--)
#define cle(a,v) memset(a,(v),sizeof(a))
const int maxn = 2e5 + 7, inf = 2e9 + 9;
int n, x, cnt, mi[maxn];
struct Gro {
int a, b, co, dur, sta;
} a[maxn << 1];
bool cmp (Gro a, Gro b) {
return a.a < b.a || a.a == b.a && a.sta > b.sta;
}
int main() {
freopen("E:1.in", "r", stdin);
cle(mi, -1);
scanf("%d%d", &n, &x);
fo(i, 0, n - 1) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
a[cnt++] = Gro{x, y, z, y - x + 1, 1};
a[cnt++] = Gro{y, x, z, y - x + 1, -1};
}
sort(a, a + cnt, cmp);
int ans = inf;
fo(i, 0, cnt - 1) {
// printf("%d %d %d \n", a[i].a, a[i].b, a[i].dur);
if(a[i].sta == 1) {
if(a[i].dur < x && mi[x - a[i].dur] != -1) {
ans = min(ans, mi[x - a[i].dur] + a[i].co);
}
}
else {
if(a[i].dur < x && (mi[a[i].dur] == -1 || mi[a[i].dur] > a[i].co)) {
mi[a[i].dur] = a[i].co;
}
}
}
printf("%d\n", ans == inf ? -1 : ans);
return 0;
}

CF822C Hacker, pack your bags!(思维)的更多相关文章

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

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

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

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

  3. CF822C Hacker, pack your bags!

    思路: 对于一个区间[l, r],只需枚举所有满足r' < l并且二者duration之和为x的区间[l', r'],寻找其中二者cost之和最小的即可.于是可以开一个数组a[],a[i]表示所 ...

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

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

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

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

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

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

随机推荐

  1. SQL反模式学习笔记10 取整错误

    目标:使用小数取代整数 反模式:使用Float类型 根据IEEE754标识,float类型使用二进制格式编码实数数据. 缺点:(1)舍入的必要性: 并不是所有的十进制中描述的信息都能使用二进制存储,处 ...

  2. net core体系-web应用程序-4asp.net core2.0 项目实战(1)-12基于cookie登录授权认证并实现前台会员、后台管理员同时登录

    1.登录的实现 登录功能实现起来有哪些常用的方式,大家首先想到的肯定是cookie或session或cookie+session,当然还有其他模式,今天主要探讨一下在Asp.net core 2.0下 ...

  3. php页面传值的方法(转)

    原文链接:https://www.cnblogs.com/suvllian/p/5582540.html   PHP页面间传值的几种方法 方法一:require_once //Page a: < ...

  4. build配置项中maven常用插件

    <build> <!-- 在浏览器地址栏的项目名称 --> <finalName>${project.artifactId}</finalName> & ...

  5. aspnet mvc 中 跨域请求的处理方法

    ASP.NET 处理跨域的两种方式    方式1,后端程序处理.原理:给响应头加上允许的域即可,*表示允许所有的域                 定义一个cors的过滤器 加在在action或者co ...

  6. python爬虫遇到https站点InsecureRequestWarning警告解决方案

    python爬虫遇到https站点InsecureRequestWarning警告解决方案 加三行代码即可 from requests.packages.urllib3.exceptions impo ...

  7. XVII Open Cup named after E.V. Pankratiev. GP of Tatarstan

    A. Arithmetic Derivative 形如$p^p(p是质数)$的数的比值为$1$,用$k$个这种数相乘得到的数的比值为$k$,爆搜即可. #include<cstdio> # ...

  8. 【BZOJ1103】大都市 解题报告

    题目传送门 打算5分钟写完题解 题目大意 有一棵n个点的有根树, 初始时每条边均为红色,有两种操作: 把某条边染为蓝色 统计根到某一点路径上的红边数量 思路 用\(a_i\)表示根到点i路径上的红边数 ...

  9. Windows系统Git安装配置

    Git的安装 Git是一个开源的分布式的版本控制软件,是Linus Torvalds 为了方便开源贡献者协同开发和管理 Linux 内核开发替代BitKe而开发的. 打开git官网的下载地址:http ...

  10. uri&url

    统一资源标志符URI就是在某一规则下能把一个资源独一无二地标识出来. 拿人做例子,假设这个世界上所有人的名字都不能重复,那么名字就是URI的一个实例,通过名字这个字符串就可以标识出唯一的一个人.现实当 ...