POJ 2431 贪心+优先队列
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
priority_queue<int> que;
struct Stop {
int dis,fuel;
}stop[];
bool cmp (Stop a,Stop b) {
if(a.dis==b.dis) return a.fuel>b.fuel;
return a.dis<b.dis;
}
int main() {
int n,L,p;
while(~scanf("%d",&n)) {
for(int i=;i<n;i++) {
scanf("%d%d",&stop[i].dis,&stop[i].fuel);
}
scanf("%d%d",&L,&p);
for(int i=;i<n;i++) {
stop[i].dis=L-stop[i].dis;
}
sort(stop,stop+n,cmp);
stop[n].dis=L;stop[n].fuel=;
int cnt=,pos=,flag=;
for(int i=;i<=n;i++) {
int d=stop[i].dis-pos;
while(p<d) {
if(!que.empty()) {
p+=que.top();
que.pop();
cnt++;
}else break;
}
if(p<d) {
flag=;
puts("-1");break;
}
p-=d;
que.push(stop[i].fuel);
pos=stop[i].dis;
}
if(flag==) printf("%d\n",cnt);
while(!que.empty()) que.pop();
}
return ;
}
POJ 2431 贪心+优先队列的更多相关文章
- poj 2431 【优先队列】
poj 2431 Description A group of cows grabbed a truck and ventured on an expedition deep into the jun ...
- Stall Reservations(POJ 3190 贪心+优先队列)
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4434 Accepted: 158 ...
- POJ 2431 (优先队列)
题目链接:https://vjudge.net/problem/POJ-2431 思路: “ 在卡车行驶途中, 只有经过加油站才能加油.” 我们不妨转变思路, 理解成“当卡车驶过加油站时就获得了加油的 ...
- poj 3190 贪心+优先队列优化
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4274 Accepted: 153 ...
- POJ 2431 Expedition (贪心+优先队列)
题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...
- POJ 2431 优先队列
汽车每过一单位消耗一单位油,其中有给定加油站可加油,问到达终点加油的最小次数. 做法很多的题,其中优先对列解这题是很经典的想法,枚举每个加油站,判断下当前油量是否小于0,小于0就在前面挑最大几个直至油 ...
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
- poj 1862 Stripies/优先队列
原题链接:http://poj.org/problem?id=1862 简单题,贪心+优先队列主要练习一下stl大根堆 写了几种实现方式写成类的形式还是要慢一些... 手打的heap: 1: #inc ...
- poj 3431 Expedition 优先队列
poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ...
随机推荐
- Retrofit学习笔记(一)
github上的介绍,简单明了 Type-safe HTTP client for Android and Java by Square, Inc. http://square.github.io/r ...
- 【BZOJ2555】SubString 后缀自动机+LCT
[BZOJ2555]SubString Description 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2 ...
- 《从零开始学Swift》学习笔记(Day 7)——Swift 2.0中的print函数几种重载形式
原创文章,欢迎转载.转载请注明:关东升的博客 Swift 2.0中的print函数有4种重载形式: l print(_:).输出变量或常量到控制台,并且换行. l print(_:_:).输出 ...
- JS复制粘贴解决方案
var clipboardData = window.clipboardData; //for IE if (!clipboardData) { // for chrome window.prompt ...
- 进程 query foreach
http://php.net/manual/en/pdo.query.php PDO::query() executes an SQL statement in a single function c ...
- 费马小定理 Fermat Theory
w 整数的质数次方和自身的差是是质数的倍数 费马小定理(Fermat Theory)是数论中的一个重要定理,其内容为: 假如p是质数,且Gcd(a,p)=1,那么 a(p-1)≡1(mod p).即: ...
- 使用jquery的ajax方法获取下拉列表值
AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新,用户体验非常好. ...
- 为什么一定要调用 setlocale 呢? 因为在 C/C++ 语言标准中定义了其运行时的字符集环境为 "C" ,也就是 ASCII 字符集的一个子集。使用setlocal改变整个应用程序的字符集编码方式(wcstombs使用前要设置 setlocale (LC_ALL, "chs"); )
setlocale 配置地域化信息. 语法: string setlocale(string category, string locale); 返回值: 字符串 函数种类: 操作系统与环境 内容 ...
- 阿里云下 centos7下启动程序总是被killed ,看内存占用情况以检查哪些服务存在问题并调整参数作调优
很久不搭理自己的网站了,几天突然发现启动程序总是被killed, 于是查看了系统日志 vi /var/log/messages 发现出现 kernel: Out of memory: Kill pro ...
- opencv3计算机视觉+Python(一)
基本I/O脚本 读/写图像文件 OpenCV的imread函数和imwrite函数能支持各种静态图像文件格式.不同系统支持的文件格式不一样,但都支持BMP格式,通常还应该支持PNG.JPEG和TIFF ...