8VC Venture Cup 2016 - Final Round C. Package Delivery 优先队列
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 优先队列的更多相关文章
- 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 ...
- 8VC Venture Cup 2016 - Final Round (Div. 1 Edition) E - Preorder Test 树形dp
E - Preorder Test 思路:想到二分答案了之后就不难啦, 对于每个答案用树形dp取check, 如果二分的值是val, dp[ i ]表示 i 这棵子树答案不低于val的可以访问的 最多 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 8VC Venture Cup 2016 - Final Round (Div2) E
贪心.当前位置满油可达的gas station中,如果有比它小的,则加油至第一个比他小的.没有,则加满油,先到达这些station中最小的.注意数的范围即可. #include <iostrea ...
随机推荐
- peewee外键性能问题
# 转载自:https://www.cnblogs.com/miaojiyao/articles/5217757.html 下面讨论一下用peewee的些许提高性能的方法. 避免N+1查询 N+1查询 ...
- ES6 新增的一些东西
一.常量 不允许重复定义 const a='HELLO' const a='world'//报错Uncaught SyntaxError: Identifier 'a' has already bee ...
- DSP学习教程基于28335(一)
首先说明:开发环境Manjaro linux,内核5.0,滚动升级版本,随时都是最新,CCS也是最新的CCv 8 #include "DSP2833x_Device.h" // 这 ...
- tex src
https://github.com/jepsonr/Text-Exercises https://github.com/Khan/KaTeX https://github.com/goldsboro ...
- FIS3 大白话【一】
1.fis3可以用fis.set进行一些全局的配置,包括忽略文件.文件后缀处理类型.源码过滤等等,用fis3.get可以得到配置信息,详见: http://fis.baidu.com/fis3/doc ...
- [ python ] 下划线的意义和一些特殊方法
Python用下划线 Python用下划线为变量前缀和后缀制定特殊变量 _xxx 不能用 'from module import *' 导入__xxx__ 系统定义名字__xxx 类中的私有变量名 核 ...
- LeetCode解题报告—— Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...
- Python软件源PyPI中国镜像 2016
作为 easy_install 的升级版,pip 为 Pyhton 的包管理提供了极大的方便.一行命令即可完成所需模块的安装: pip install pandas 可是官方镜像的访问速度相当慢,几乎 ...
- bzoj 1485 卡特兰数 + 分解因子
思路:打表可以看出是卡特兰数,但是模数不一定是素数,所以需要分解一下因数. #include<bits/stdc++.h> #define LL long long #define fi ...