首页 技术 正文
技术 2022年11月7日
0 收藏 413 点赞 751 浏览 1998 个字

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);
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,033
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,862