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 分析:按照挑战书的解法,每走到一个加油站相当于 ...
随机推荐
- web项目生成war包的问题
今天面试一家公司,问我生成war包的命令是什么? 当时没明白,就说自己用的eclipse直接右键 export --->war 完了重启tomcat(第一种) 好久没用maven了.回来一查才明 ...
- LeetCode 563. Binary Tree Tilt (二叉树的倾斜度)
Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...
- CRUSH: Controlled, Scalable, Decentralized Placement of Replicated Data译文
原文地址:http://www.oschina.net/translate/crush-controlled-scalable-decentralized-placement-of-replicate ...
- celery rabbit mq 详解
Celery介绍和基本使用 Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery, ...
- Anaconda Error opening file for writing , failed to create anacoda menu等报错问题解决方案
安装anaconda的时候可能会遇到这个报错, 原因可能是:路径不允许有空格 此外发生报错failed to create anacoda menu, 解决方案 进入 cmd,找到你安装的位置(我的是 ...
- C++ 空间配置器(allocator)
C++ 空间配置器(allocator) 在STL中,Memory Allocator 处于最底层的位置,为一切的 Container 提供存储服务,是一切其他组件的基石.对于一般使用 STL 的用户 ...
- Windows Message Queue
Windows Message Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- RPC 调用简述
首先了解什么叫RPC,为什么要RPC,RPC是指远程过程调用,也就是说两台服务器A,B,一个应用部署在A服务器上,想要调用B服务器上应用提供的函数/方法,由于不在一个内存空间,不能直接调用,需要通过网 ...
- 二叉搜索树的第 k 个结点
题目 给定一颗二叉搜索树,请找出其中的第k小的结点,即将二叉树中所有元素从小到大排序的第 k 个结点. 解析 按中序遍历二叉搜索树就可以获得一个非递减的序列,此时第 k 个就为答案.实际上我们只需要按 ...
- Struts2开发文档
struts2配置 struts2核心配置文件约束 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts ...