http://www.codeforces.com/contest/627/problem/C
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.
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.
Print a single integer — the minimum cost to complete the delivery. If there is no way to complete the delivery, print -1.
10 4 4
3 5
5 8
6 3
8 4
22
你从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);
}