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. ajax多图上传

    百度云代码 参考:https://segmentfault.com/q/1010000004218827

  2. 机器学习——KNN

    导入类库 import numpy as np from sklearn.neighbors import KNeighborsClassifier from sklearn.model_select ...

  3. CSS3_过渡_2D 变换_瓶体旋转_动态时钟

    1. 过渡 transition 允许 CSS 的属性值在一定时间内平滑的过渡, 在鼠标点击,鼠标滑过或对属性改变中触发,并圆滑的改变 CSS 的属性值 简写属性: #box { width: 300 ...

  4. Node.js_express_搭建一个服务器

    原生 node 服务器 1. 导入 node.js 核心模块  / 自带模块 :   http const http = require('http'); // HTTP 库所具有的功能已经赋给了 h ...

  5. 面试题: 多个 await 处理,有一个失败,就算作失败

    面试题: 多个 await 处理,有一个失败,就算作失败 ? Promise.all([p1, p2, p3....])    // 返回的也是一个 Promise 对象 -------- asait ...

  6. Promise 用法

    Promise是一个构造函数,自己身上有all.reject.resolve这几个眼熟的方法,原型上有then.catch等同样很眼熟的方法. 那就new一个 Promise的构造函数接收一个参数,是 ...

  7. hadoop本地开发环境搭建

    1:下载hadoop2.7.3并解压 2:配置hadoop2.7.3环境变量 HADOOP_HOME %HADOOP_HOME%\bin 3:下载hadoop-eclipse-plugin插件 网址: ...

  8. 【C++ 流类库与输入输出 】实验七

    1. 基础练习 (1)教材习题 11-7 (2)教材习题 11-3 (3)教材习题 11-4 2. 应用练习 (1)已知有班级名单文件 list.txt(见实验 7 附件包).编写一个应用程序实现随机 ...

  9. VS2015中使用报表控件(ReportViewer)的方法

    没有报表,一般默认安装之后会出现这种情况,在安装的时候选择自定义安装,把Microsoft Office 开发人员工具.Microsoft SQL Server Data Tools勾选上,安装之后就 ...

  10. verilog 之流水灯

    1.黑金板 简易操作: 通过判断数值累加    个人观点:黑金代码质量有待提高,讲解不够详细 2.正点原子的 位置调换 led[:] <= {led[:],led[]}; 3.传统位移 led& ...