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. 使用PTGui软件将全景图变成鱼眼图

    把全景图变成鱼眼图.方法一部分是自己研究的,一部分是参考学妹街景合成鱼眼照片的方法. 需要使用的软件是PTGui.是个收费软件,价格还不便宜.操作一下,安装完后就可以开始合成鱼眼图了. 加载图像 打开 ...

  2. 网络知识===wireshark抓包数据分析(一)

    wireshark分析: 上图是我进行一个HTTP协议的下载,文件内容大概是1.7M左右. 抓包数据: https://files.cnblogs.com/files/botoo/wireshark% ...

  3. mac10.9下安装Android

    这里记录一下mac下安装android比较快捷的方法 首先,到这里下载Android SDK,这个是集成的,所有工具一应俱全,免去了下载一堆东西的烦恼.具体包括如下: Eclipse + ADT pl ...

  4. PHP常用函数总结(180多个)

    PHP常用函数总结 数学函数 1.abs(): 求绝对值 $abs = abs(-4.2); //4.2 数字绝对值数字 2.ceil(): 进一法取整 echo ceil(9.999); // 10 ...

  5. POJ-1830

    开关问题 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6294   Accepted: 2393 Description ...

  6. 【DUBBO】dubbo的Router接口

    Router服务路由, 根据路由规则从多个Invoker中选出一个子集AbstractDirectory是所有目录服务实现的上层抽象, 它在list列举出所有invokers后,会在通过Router服 ...

  7. Subsets I&&II——经典题

    Subsets I Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a s ...

  8. c# 发送web请求

    我们目前涉及到的现有的接收请求方式有三种, 第一种: 页面式的Form表单 第二种: 服务的webservice形式的xml 第三个: restful风格的post包体json 第一种比较老,博客园的 ...

  9. 单断言VS多断言

    STST 想和大家讨论一下,一个测试用例里只做一个断言还是一个用例里做多个相关的断言 比如有一个查询函数Query(id) 返回[姓名,性别,年龄] 那么是在一个测试用例里对这三个属性进行断言好? 还 ...

  10. JS中事件绑定问题

    今天编写代码时遇到一个问题,我的判断语句(IFLESE)老是顺序执行结束后又跳到中间的语句里去执行了,找了半天没发现问题,最后才发现是事件绑定闹得鬼,不多说,先上代码为敬. JSP里 <butt ...