poj 3431 Expedition 优先队列
poj 3431 Expedition 优先队列
题目链接:
http://poj.org/problem?id=2431
思路:
优先队列。对于一段能够达到的距离,优先选择其中能够加油最多的站点,这样,行驶过这段距离之后还能走更远的距离。
将输入的数据进行排序处理,按照位置的先后。注意输入的距离是与终点的,要转化成与起点的。
代码:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <queue>
using namespace std;
const int maxn = 10005;
struct node {
int a,b;
} ans[maxn];
bool cmp(const node& x, const node& y) {return x.a<y.a;}
priority_queue<int,vector<int>,less<int> > q;
int main() {
int n,l,p;
scanf("%d",&n);
for(int i=0;i<n;++i) scanf("%d %d",&ans[i].a,&ans[i].b);
scanf("%d %d",&l,&p);
for(int i=0;i<n;++i) ans[i].a=l-ans[i].a;
ans[n].a=l;
ans[n].b=0;
n++;
sort(ans,ans+n,cmp);
int index=0,last=p,cnt=0;
for(int i=0;i<n;++i) {
int dist=ans[i].a-index;
while(dist>last) {
if(q.empty()) {
puts("-1\n");
return 0;
}
last+=q.top();
q.pop();
cnt++;
}
last-=dist;
index=ans[i].a;
q.push(ans[i].b);
}
printf("%d\n",cnt);
return 0;
}
poj 3431 Expedition 优先队列的更多相关文章
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
- poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10025 Accepted: 2918 Descr ...
- poj - 2431 Expedition (优先队列)
http://poj.org/problem?id=2431 你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇, ...
- POJ 2431 Expedition (贪心+优先队列)
题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...
- POJ 2431 Expedition (贪心 + 优先队列)
题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
- POJ 2431——Expedition(贪心,优先队列)
链接:http://poj.org/problem?id=2431 题解 #include<iostream> #include<algorithm> #include< ...
- POJ 2431 Expedition(优先队列、贪心)
题目链接: 传送门 Expedition Time Limit: 1000MS Memory Limit: 65536K 题目描述 驾驶一辆卡车行驶L单位距离.最开始有P单位的汽油.卡车每开1 ...
- 优先队列 POJ 2431 Expedition
题目传送门 题意:一辆卡车要行驶L长度,初始有P油,每行驶一个单位长度消耗一单位油.有n个加油站可以加油,问最少加油几次才能行驶L长度,如果不能输出-1 分析:按照挑战书的解法,每走到一个加油站相当于 ...
随机推荐
- jQuery的区别:$().click()和$(document).on('click','要选择的元素',function(){})的不同
jQuery的出现,大大简化了对dom的操作,但是如果不是仔细阅读api和进行操作,就不知道其中最大的优点和使用方式.就拿$().click()和$(document).on('click','要选择 ...
- Machine-learning of Andrew Ng(Stanford University)
1.基础概念 机器学习是一门研究在非特定编程条件下让计算机采取行动的学科.最近二十年,机器学习为我们带来了自动驾驶汽车.实用的语音识别.高效的网络搜索,让我们对人类基因的解读能力大大提高.当今机器学习 ...
- angular高级篇之transclude使用详解
angular指令的transclude属性是一个让初学者比较难以理解的地方,transclude可以设置为false(默认),true或者对象三种值,如果不设该属性就默认为false,也就是说你不需 ...
- angualr高级篇之elem.scope()、elem.isolateScope和$compile(elem)(scope)中scope的区别
在angular的使用过程中我们经常用$rootScope.$new()为elem创建一个新的作用域scope,然后使用$compile(elem)(scope)编译这个含有指令的元素.那么这里传进去 ...
- Ionic3 遇到的一些错误-Error: Cannot find module 'reflect-metadata'
E:\Projects\ionic\myApp5>ionic serve Error: Cannot find module 'reflect-metadata' 解决方法: npm insta ...
- dynamic_cast 转换示例
dynamic_cast 转换示例 /* 带虚函数与不带虚函数转换的区别 dynamic_cast:必须要有虚函数才可以转换 dynamic_cast:只能处理转换指针或引用,不能转换对象 dynam ...
- The Euler function(线性筛欧拉函数)
/* 题意:(n)表示小于n与n互质的数有多少个,给你两个数a,b让你计算a+(a+1)+(a+2)+......+b; 初步思路:暴力搞一下,打表 #放弃:打了十几分钟没打完 #改进:欧拉函数:具体 ...
- zookeeper详解
ZooKeeper 1.Zookeeper(***必须掌握***) 官方网址:http://zookeeper.apache.org/ Ø 什么是Zookeeper? l Zookeeper 是 G ...
- lynx---CentOS终端访问IP
1.官网 http://lynx.isc.org 2.稳定版本 http://invisible-mirror.net/archives/lynx/tarballs/lynx2.8.8rel.2.ta ...
- 混合app
cordova run android 把应用发送到手机 ionic serve 电脑浏览器调试命令 创建: cordova create hello co ...