8VC Venture Cup 2016 - Final Round C. Package Delivery 优先队列
2016-03-03 19:23
405 查看
C. Package Delivery
题目连接:
http://www.codeforces.com/contest/627/problem/CDescription
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 43 5
5 8
6 3
8 4
Sample Output
22Hint
题意
你从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); }
相关文章推荐
- HDFS简介
- 使用Proxmark3进行MIFARE Classic卡的安全测试
- Chipmunk物理引擎
- [置顶] zookeeper 集群搭建教程之应用案例
- 2016蓝桥杯假期任务之《身份证升级》
- Http请求中Content-Type讲解以及在Spring MVC中的应用
- main函数的底层理解
- leetcode--Length of Last Word
- MAX NUM
- 最简版QQ~~牛逼的Linux Netcat 命令
- 那些被遗漏的Objective-C保留字
- 定义ll命令
- javaweb2.2 DOM解析之增删改查
- bellman-ford 学习总结性文档
- Context类浅析
- 《JAVA与模式》之桥梁模式
- 详细分析.net 反序题目
- mysql的安装
- 网站交互的理解
- x86、i386、i486、i586、i686和x86_64