题目链接: http://codeforces.com/problemset/problem/822/C

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

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

思路: xjb贪心

这题目肯定是枚举一条线段再找剩下一条线段, 假设枚举右线段, 且其长为len1, 那么我们需要找这条线段左边的长度为 x - len1 的线段的 cost 最小值;直接暴力的话是n^2的复杂度, 肯定 tle. 可以给这些线段按照左右端点排下序, 那么我们可以从按左端点排序的数组中枚举右线段, 然后在按右端点排序的数组中找左线段;但是如果暴力找的话时间复杂度还是 n^2 . 我们可以维护一个 vis数组, vis[i] 为当前右线段的左边线段中长度为 i 的 cost 最小值, 那么 vis[x - len1] 即为当前左线段的 cost 最小值;

那么 cost1 + vis[x - len1] 的最小值即为答案啦;

代码:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define ll long long
using namespace std; const int MAXN = 2e5 + ;
const int inf = 0x7f7f7f7f;
ll vis[MAXN];
struct node{
int l, r;
ll w;
}gel1[MAXN], gel2[MAXN]; bool cmp1(node a, node b){
return a.l < b.l;
} bool cmp2(node a, node b){
return a.r < b.r;
} int main(void){
int n, x;
ll ans = inf;
scanf("%d%d", &n, &x);
for(int i = ; i < n; i++){
scanf("%d%d%lld", &gel1[i].l, &gel1[i].r, &gel1[i].w);
gel2[i] = gel1[i];
}
sort(gel1, gel1 + n, cmp1);
sort(gel2, gel2 + n, cmp2);
memset(vis, 0x7f, sizeof(vis));
int indx = ;
for(int i = ; i < n; i++){
ll gg = gel1[i].w;
while(indx < n && gel2[indx].r < gel1[i].l){
int cnt = gel2[indx].r - gel2[indx].l + ;
if(vis[cnt] > gel2[indx].w) vis[cnt] = gel2[indx].w;
indx++;
}
int cc = x - (gel1[i].r - gel1[i].l + );
if(cc <= ) continue;
gg += vis[cc];
if(ans > gg) ans = gg;
}
if(ans == inf) cout << - << endl;
else cout << ans << endl;
return ;
}

cf822C(贪心)的更多相关文章

  1. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  2. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

  7. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

  8. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

  9. 【贪心】HDU 1257

    HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...

随机推荐

  1. Gemini.Workflow 双子工作流入门教程二:定义流程:流程节点介绍

    简介: Gemini.Workflow 双子工作流,是一套功能强大,使用简单的工作流,简称双子流,目前配套集成在Aries框架中. 下面介绍本篇教程:流程定义:流程节点属性. 流程节点: 左侧是节点工 ...

  2. 近200篇机器学习&深度学习资料分享【转载】

    编者按:本文收集了百来篇关于机器学习和深度学习的资料,含各种文档,视频,源码等.而且原文也会不定期的更新,望看到文章的朋友能够学到更多. <Brief History of Machine Le ...

  3. POJ 1330 Nearest Common Ancestors 【最近公共祖先LCA算法+Tarjan离线算法】

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20715   Accept ...

  4. matlab之scatter3()函数

    Display point cloud in scatter plot(在散点图中显示点云): scatter3(X,Y,Z) 在向量 X.Y 和 Z 指定的位置显示圆圈. scatter3(X,Y, ...

  5. L93

    Three Days to See(Excerpts) All of us have read thrilling stories in which the hero had only a limit ...

  6. hdu-5802 Windows 10(贪心)

    题目链接: Windows 10 Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others ...

  7. 解决Linux Kettle出现闪退问题

    linux环境, 运行sh spoon.sh打开图形化界面时经常出现闪退情况. 报错信息如下: cfgbuilder - Warning: The configuration parameter [o ...

  8. [C++11新特性]第二篇

    0.可变数量参数,可变函数模版,变长模版类 c++98可变数量参数 #include<cstdio> #include<cstdarg> double SumOfFloat(i ...

  9. 1067 Bash 游戏v2

    传送门 1067 Bash游戏 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 10   有一堆石子共有N个.A B两个人轮流拿,A先拿.每次只能拿1,3,4颗,拿到最后1颗石子的人 ...

  10. MOVE降低高水位 HWM

    MOVE降低高水位 HWM --创建实验表空间SQL> create tablespace andy03 datafile '/home/oracle/app/oradata/orcl/andy ...