传送门

题意

给出n个区间[l,r]及花费\(cost_i\),找两个区间满足

1.区间和为指定值x

2.花费最小

分析

先用vector记录(l,r,cost)和(r,l,cost),按l排序,再设置一个数组bestcost[i]代表长度为i的最小花费。

O(n)扫一遍,如果碰到区间左端点,更新答案;碰到右端点,更新bestcost[len],具体见代码

trick

1.更新答案会爆int

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define F(i,a,b) for(int i=a;i<=b;++i)
  4. #define R(i,a,b) for(int i=a;i<b;++i)
  5. #define mem(a,b) memset(a,b,sizeof(a))
  6. #define mp(a,b) make_pair(a,b)
  7. #define pb(x) push_back(x)
  8. #define LL long long
  9. //#pragma comment(linker, "/STACK:102400000,102400000")
  10. //inline void read(int &x){x=0; char ch=getchar();while(ch<'0') ch=getchar();while(ch>='0'){x=x*10+ch-48; ch=getchar();}}
  11. const int maxn=200200;
  12. const int inf = 2e9+20;
  13. int n,x,l,r,cost;
  14. std::vector<pair<pair<int,int>,pair<int,int> > > v;
  15. int bestcost[maxn+10];
  16. int main()
  17. {
  18. scanf("%d %d",&n,&x);
  19. R(i,0,n)
  20. {
  21. scanf("%d %d %d",&l,&r,&cost);
  22. v.pb(mp(mp(l,-1),mp(r,cost)));
  23. v.pb(mp(mp(r,1),mp(l,cost)));
  24. }
  25. F(i,0,maxn) bestcost[i]=inf;
  26. sort(v.begin(),v.end());
  27. LL ans=inf;
  28. int type,sz=v.size();
  29. R(i,0,sz)
  30. {
  31. type=v[i].first.second;
  32. if(type==-1)
  33. {
  34. int len=v[i].second.first-v[i].first.first+1;
  35. //printf("%d\n",bestcost[x-len]);
  36. if(x>len) ans=min(ans,(LL)(v[i].second.second)+(LL)bestcost[x-len]);
  37. }
  38. else
  39. {
  40. int len=v[i].first.first-v[i].second.first+1;
  41. bestcost[len]=min(bestcost[len],v[i].second.second);
  42. }
  43. }
  44. printf("%I64d\n",(ans>=inf)?-1:ans);
  45. return 0;
  46. }

Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!(更新数组)的更多相关文章

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

  2. Codeforces Round #422 (Div. 2)

    Codeforces Round #422 (Div. 2) Table of Contents Codeforces Round #422 (Div. 2)Problem A. I'm bored ...

  3. 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(二分写法)

    [题目链接]:http://codeforces.com/contest/822/problem/C [题意] 有n个旅行计划, 每个旅行计划以开始日期li,结束日期ri,以及花费金钱costi描述; ...

  4. 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(hash写法)

    接上一篇文章; 这里直接把左端点和右端点映射到vector数组上; 映射一个open和close数组; 枚举1..2e5 如果open[i]内有安排; 则用那个安排和dp数组来更新答案; 更新答案完之 ...

  5. Codeforces Round #422 (Div. 2) E. Liar 后缀数组+RMQ+DP

    E. Liar     The first semester ended. You know, after the end of the first semester the holidays beg ...

  6. Codeforces Round #422 (Div. 2) B. Crossword solving 枚举

    B. Crossword solving     Erelong Leha was bored by calculating of the greatest common divisor of two ...

  7. Codeforces Round #422 (Div. 2) A. I'm bored with life 暴力

    A. I'm bored with life     Holidays have finished. Thanks to the help of the hacker Leha, Noora mana ...

  8. 【Codeforces Round #422 (Div. 2) D】My pretty girl Noora

    [题目链接]:http://codeforces.com/contest/822/problem/D [题意] 有n个人参加选美比赛; 要求把这n个人分成若干个相同大小的组; 每个组内的人数是相同的; ...

  9. 【Codeforces Round #422 (Div. 2) B】Crossword solving

    [题目链接]:http://codeforces.com/contest/822/problem/B [题意] 让你用s去匹配t,问你最少需要修改s中的多少个字符; 才能在t中匹配到s; [题解] O ...

随机推荐

  1. 安卓自己定义View进阶-Canvas之绘制基本形状

    Canvas之绘制基本形状 作者微博: @GcsSloop [本系列相关文章] 在上一篇自己定义View分类与流程中我们了解自己定义View相关的基本知识,只是,这些东西依然还是理论,并不能拿来(zh ...

  2. react 创建组件 (四)Stateless Functional Component

    上面我们提到的创建组件的方式,都是用来创建包含状态和用户交互的复杂组件,当组件本身只是用来展示,所有数据都是通过props传入的时候,我们便可以使用Stateless Functional Compo ...

  3. react 创建组件 (二)component

    因为ES6对类和继承有语法级别的支持,所以用ES6创建组件的方式更加优雅,下面是示例: import React from 'react'; class Greeting extends React. ...

  4. JAVA程序设计(12.3)---- 监听器0基础应用:五子棋

    1.制作五子棋游戏软件 由于老师已经基本做完了.重做的时候快了非常多--可是还是感觉思维非常混乱-- 哪边先哪边后,哪个方法在哪边好之类的问题 太纠结了-- 先是窗体 内部类:鼠标适配器  窗体的构造 ...

  5. Collection接口和Collections类的简单区别和讲解

    这里仅仅进行一些简单的比较,如果你想要更加详细的信息话,请自己百度. 1.Collection: 是集合类的上层接口.本身是一个Interface,里面包含了一些集合的基本操作. Collection ...

  6. 【第四篇章-android平台MediaCodec】解决Observer died. Quickly, do something, ... anything...

    当出现!!!Observer died. Quickly, do something, ... anything...说明你的程序已经出现严重异常了,那会是什么情况呢?这个问题困扰了我许久,后来原来是 ...

  7. F08标准中Open命令的newunit选项

    从gfortran 4.5开始Open命令开始支持newunit选项,示例如下: integer :: u open(newunit=u, file="log.txt", posi ...

  8. 基于mqtt协议实现手机位置跟踪

    Mqtt协议是物联网领域的一个标准协议,具有轻巧,对设备,带宽要求低,可靠稳定的特点,适合用来实现手机定位跟踪功能. 目前我初步搭建起来了整个可运行的框架,大致为如下思路:1.手机端通过位置服务,获取 ...

  9. bind_ip

    https://docs.mongodb.com/manual/reference/configuration-options/index.html 192.168.2.* --23T10:: I C ...

  10. 关闭mongodb 集群

    [root@hadoop1 ~]# ps -aux | grep mongodb; root ? Sl : : /usr/local/mongodb/bin/mongod -f /usr/local/ ...