CF822C Hacker, pack your bags!(思维)
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!(思维)的更多相关文章
- CF-822C Hacker, pack your bags! 思维题
题目大意是给若干线段及其费用,每个线段权值即为其长度.要求找出两个不重合线段,令其权值和等于x且费用最少. 解法: 先分析一下题目,要处理不重合的问题,有重合的线段不能组合,其次这是一个选二问题,当枚 ...
- Codeforces 822C Hacker, pack your bags!(思维)
题目大意:给你n个旅券,上面有开始时间l,结束时间r,和花费cost,要求选择两张时间不相交的旅券时间长度相加为x,且要求花费最少. 解题思路:看了大佬的才会写!其实和之前Codeforces 776 ...
- CF822C Hacker, pack your bags!
思路: 对于一个区间[l, r],只需枚举所有满足r' < l并且二者duration之和为x的区间[l', r'],寻找其中二者cost之和最小的即可.于是可以开一个数组a[],a[i]表示所 ...
- 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 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 ...
- 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 ...
- 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 ...
- codeforces 822 C. Hacker, pack your bags!(思维+dp)
题目链接:http://codeforces.com/contest/822/submission/28248100 题解:多维的可以先降一下维度sort一下可以而且这种区间类型的可以拆一下区间只要加 ...
- 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 ...
随机推荐
- SQL反模式学习笔记10 取整错误
目标:使用小数取代整数 反模式:使用Float类型 根据IEEE754标识,float类型使用二进制格式编码实数数据. 缺点:(1)舍入的必要性: 并不是所有的十进制中描述的信息都能使用二进制存储,处 ...
- windows下创建MySQL定时备份与删除脚本
今天在windows服务器上面写了一个MySQL定时任务,备份呢与删除 rem *****************************Code start********************* ...
- Git 配置用户名、密码
在终端输入: git config --global credential.helper store 然后git pull一次,输入一次用户名密码就会自动保存该用户名密码: 查看配置的用户信息: gi ...
- Android 常用知识点
1.Kotlin 将字节大小转换为KB,MB,GB 并保留两位小数 fun getFileSize(size: Long): String { var GB = 1024 * 1024 * 1024 ...
- Scrapy基础(十四)————知乎模拟登陆
#-*-coding:utf-8 -*-__author__ = "ruoniao"__date__ = "2017/5/31 20:59" 之前我们通过爬取伯 ...
- mysql查询用,或#隔开的字段
假如,user表有一字段 pids,pids字段是用#(实际用逗号合适)隔开的师傅id.现在查询师傅id:168的徒弟有哪些(徒弟.徒孙.徒孙的徒弟.徒孙的徒孙....) sql: select * ...
- java集合的复习
1:自定义的linkedList链表 https://blog.csdn.net/qq_33471403/article/details/80109620 2:用linked https://b ...
- UIdynamic系列认知
1.刚上来就遇到的大坑 按照前辈们的代码码上了一段重力效果,结果不管怎么折腾都没有任何效果,不由心塞. 开始怀疑是不是模拟器不支持呀?是不是必须拖动到视图上的空间才可以呀? 结果试了各种方法还是没效果 ...
- HTML入门7
这篇来些可能用的比较少的,调试HTML 程序员调试代码常见,遇到问题一切正常,找出问题解决,满足 来了解下HTML调试, 在浏览器解析和显示之前HTML不会被编译成其他形式,只是解析而不是编译因此运行 ...
- 问题10:获取当前页面宽度JS
var mleft=document.getElementById("mleft"); var h = document.documentElement.clientHeight; ...