题目链接: 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. 腾讯云上运行java程序过程

    1: 购买服务器(腾讯云,阿里云等) 2:安装centos操作系统: 3:安装jdkhttp://www.cnblogs.com/Amos-Turing/p/7403696.html 4:安装数据库( ...

  2. requests不加代理

    requests里的proxies不加代理可以设置为空,就会使用本机IP proxies={}

  3. 微信小程序开发:学习笔记[1]——Hello World

    微信小程序开发:学习笔记[1]——Hello World 快速开始 1.前往微信公众平台下载微信开发者工具. 地址:https://mp.weixin.qq.com/debug/wxadoc/dev/ ...

  4. POJ题目算法分类总结博客地址

    http://blog.csdn.net/sunbaigui/article/details/4421705 又从这个地址找了一些:http://blog.csdn.net/koudaidai/art ...

  5. POJ 2063 Investment (完全背包)

    A - Investment Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Subm ...

  6. 7.8 LZW压缩的实现

    7-10 lzw.c #include <stdlib.h> #include <stdio.h> #define BITS 12 //每个数据项的二进制位数 #define ...

  7. linux下MySQL5.6安装记录

    MySQL下载地址: ftp://mirror.switch.ch/mirror/mysql/Downloads/MySQL-5.6/http://mirrors.sohu.com/mysql/   ...

  8. elasticsearch-installation

    1. 安装Java JDK 移步 :sdfa 2. 下载elasticsearch url : https://artifacts.elastic.co/downloads/elasticsearch ...

  9. codeforces 660B B. Seating On Bus(模拟)

    题目链接: B. Seating On Bus time limit per test 1 second memory limit per test 256 megabytes input stand ...

  10. 「NOIP2013」「LuoguP1967」货车运输(最大生成树 倍增 LCA

    题目描述 AA国有nn座城市,编号从 11到nn,城市之间有 mm 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 qq 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最 ...