C. Package Delivery

题目连接:

http://www.codeforces.com/contest/627/problem/C

Description

Johnny drives a truck and must deliver a package from his hometown to the district center. His hometown is located at point 0 on a number line, and the district center is located at the point d.

Johnny's truck has a gas tank that holds exactly n liters, and his tank is initially full. As he drives, the truck consumes exactly one liter per unit distance traveled. Moreover, there are m gas stations located at various points along the way to the district center. The i-th station is located at the point xi on the number line and sells an unlimited amount of fuel at a price of pi dollars per liter. Find the minimum cost Johnny must pay for fuel to successfully complete the delivery.

Input

The first line of input contains three space separated integers d, n, and m (1 ≤ n ≤ d ≤ 109, 1 ≤ m ≤ 200 000) — the total distance to the district center, the volume of the gas tank, and the number of gas stations, respectively.

Each of the next m lines contains two integers xi, pi (1 ≤ xi ≤ d - 1, 1 ≤ pi ≤ 106) — the position and cost of gas at the i-th gas station. It is guaranteed that the positions of the gas stations are distinct.

Output

Print a single integer — the minimum cost to complete the delivery. If there is no way to complete the delivery, print -1.

Sample Input

10 4 4

3 5

5 8

6 3

8 4

Sample Output

22

Hint

题意

你从0点出发,你油箱最大为n升,你要到距离d的地方去。

现在这条线上有m个加油站,分别在x[i]位置,每升油价格为p[i]

一开始你油是满的,然后问你最少多少钱,可以使得从起点到终点

不能输出-1

题解:

优先队列

对于每个点,维护一下最便宜能够到这个点的汽油站,且能够接着往下走的汽油站是啥

由于每个点只会在队列中进来出去一次。

所以复杂度是nlogn的。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;
pair<int,int> p[maxn];
struct node
{
int x,y;
friend bool operator < (const node & a,const node & b)
{
if(a.y==b.y)return a.x>b.x;
return a.y>b.y;
}
};
priority_queue<node>Q;
int main()
{
int d,n,m;
scanf("%d%d%d",&d,&n,&m);
for(int i=1;i<=m;i++)
scanf("%d%d",&p[i].first,&p[i].second);
p[m+1]=make_pair(d,0);
sort(p+1,p+1+m);
long long ans = 0;
Q.push(node{0,0});
for(int i=0;i<=m;i++)
{
int now = p[i].first;
int dis = p[i+1].first - p[i].first;
if(dis>n)return puts("-1");
while(dis)
{
while(Q.size()&&Q.top().x+n<=now)Q.pop();
node G = Q.top();
int d = min(dis,n-(now-G.x));
dis-=d;
ans+=1ll*d*G.y;
now+=d;
}
Q.push(node{p[i+1].first,p[i+1].second});
}
printf("%lld\n",ans);
}

8VC Venture Cup 2016 - Final Round C. Package Delivery 优先队列的更多相关文章

  1. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)

    暴力 A - Orchestra import java.io.*; import java.util.*; public class Main { public static void main(S ...

  2. 8VC Venture Cup 2016 - Final Round (Div. 1 Edition) E - Preorder Test 树形dp

    E - Preorder Test 思路:想到二分答案了之后就不难啦, 对于每个答案用树形dp取check, 如果二分的值是val, dp[ i ]表示 i 这棵子树答案不低于val的可以访问的 最多 ...

  3. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A

    A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. 8VC Venture Cup 2016 - Final Round D. Preorder Test 二分 树形dp

    Preorder Test 题目连接: http://www.codeforces.com/contest/627/problem/D Description For his computer sci ...

  5. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D. Factory Repairs 树状数组

    D. Factory Repairs 题目连接: http://www.codeforces.com/contest/635/problem/D Description A factory produ ...

  6. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) C. XOR Equation 数学

    C. XOR Equation 题目连接: http://www.codeforces.com/contest/635/problem/C Description Two positive integ ...

  7. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)B. sland Puzzle 水题

    B. sland Puzzle 题目连接: http://www.codeforces.com/contest/635/problem/B Description A remote island ch ...

  8. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A. Orchestra 水题

    A. Orchestra 题目连接: http://www.codeforces.com/contest/635/problem/A Description Paul is at the orches ...

  9. 8VC Venture Cup 2016 - Final Round (Div2) E

    贪心.当前位置满油可达的gas station中,如果有比它小的,则加油至第一个比他小的.没有,则加满油,先到达这些station中最小的.注意数的范围即可. #include <iostrea ...

随机推荐

  1. linux编程之信号量编程

    信号量当我们在多用户系统,多进程系统,或是两者混合的系统中使用线程操作编写程序时,我们经常会发现我们有段临界代码,在此处我们需要保证一个进程(或是一个线程的执行)需要排他的访问一个资源.信号量有一个复 ...

  2. linux中断线程化分析【转】

    转自:http://blog.csdn.net/qq405180763/article/details/24120895 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在为3.8版本的Li ...

  3. monkey测试===如何获取android app的Activity

    方法一(推荐): 手机连接adb,手机界面在需要取得activity的界面. 推荐使用该命令: adb shell dumpsys activity top | findstr ACTIVITY 获取 ...

  4. Oracle sql中的正则表达式

    SELECT first_name, last_nameFROM employeesWHERE REGEXP_LIKE (first_name, '^Ste(v|ph)en$'); FIRST_NAM ...

  5. dev....把pivotgridview和chart一起导出

    首先~: 命名空间: using DevExpress.XtraPrinting;using DevExpress.XtraCharts.Native;using DevExpress.XtraPri ...

  6. 虚拟机安装苹果系统 VMware 12安装Mac OS X 10.10

    工具/原料 VMware Workstation Pro 12 (这个可以自己下载,并激活,你懂得) Unlocker 207 (链接:http://pan.baidu.com/s/1i43obDb ...

  7. Mysql 数据库学习笔记04 函数

    一.创建自定义函数 * 使用自定义函数,可以返回字符串.整型.实数或者其他类型: create [aggregate] function 名称 (参数列表) return type begin //函 ...

  8. 查询 IDE 的版本 方便安装第三方的时候选择

    TMD 很多第三方软件 的版本 定义 与 delphi是不一致的. 1.有的是以这个为准.

  9. Bootstrap新版里的a标签点击后出现下划线解决办法

    其实我从失去焦点后发现了下划线消失了就应该知道 Bootstrap对a标签进行了 focus焦点事件. 所以解决办法就是一句:a:focus{text-decoration: none}. 一个笑笑的 ...

  10. LintCode 13. Implement strStr()

    LintCode 13. Implement strStr() 题目描述 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出 ...