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 分析:按照挑战书的解法,每走到一个加油站相当于 ...
随机推荐
- maven搭建MVC项目具体步骤
一.目标 在这篇文章中,我将要向您展示如何使用spring Frameworks 和 Maven build创建您的第一个J2ee 应用程序. 二.信息 Maven是一个Java项目的构建工具(或者自 ...
- MongoDB基本命令行操作
1. 连接MongoDB: Mongodb://username:password@hostname/dbname 2. 创建数据库: use dbname:如果数据库不存在则创建数据库,否则切换到指 ...
- python的小基础
变量python中的变量为指向常量的地址当常量没有指向时,系统自动回收内存空间如A = 1B = AA = 2print(A,B)#2,1id(A),id(B)id()为python虚拟机的虚拟地址, ...
- POJ3468(线段树 区间修改 lazy-tag)
我的线段树真的没救了......还是多练几道吧....... You have N integers, A1, A2, ... , AN. You need to deal with two kind ...
- CodeForces - 556B Case of Fake Numbers
//////////////////////////////////////////////////////////////////////////////////////////////////// ...
- 1026: [SCOI2009]windy数
1026: [SCOI2009]windy数 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 8247 Solved: 3708[Submit][Sta ...
- HDU 6069 Counting Divisors
Counting Divisors Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Oth ...
- Android 开发笔记___shape
shape_oval <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android= ...
- Python爬虫入门:URLError异常处理
大家好,本节在这里主要说的是URLError还有HTTPError,以及对它们的一些处理. 1.URLError 首先解释下URLError可能产生的原因: 网络无连接,即本机无法上网 连接不到特定的 ...
- ML神器:sklearn的快速使用
传统的机器学习任务从开始到建模的一般流程是:获取数据 -> 数据预处理 -> 训练建模 -> 模型评估 -> 预测,分类.本文我们将依据传统机器学习的流程,看看在每一步流程中都 ...