poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10025 | Accepted: 2918 |
Description
it travels.
To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows
can stop to acquire additional fuel (1..100 units at each stop).
The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that
there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000).
Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.
Input
* Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop.
* Line N+2: Two space-separated integers, L and P
Output
Sample Input
4
4 4
5 2
11 5
15 10
25 10
Sample Output
2
Hint
The truck is 25 units away from the town; the truck has 10 units of fuel. Along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck). These fuel stops can supply
up to 4, 2, 5, and 10 units of fuel, respectively.
OUTPUT DETAILS:
Drive 10 units, stop to acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.
Source
<span style="font-size:24px;"><a target=_blank href="http://poj.org/searchproblem?field=source&key=USACO+2005+U+S+Open+Gold" style="text-decoration: none;">#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node{
int d;
int v;
bool operator <(Node b) const{
return v<b.v;
};
}node[10005];
bool cmp(Node a,Node b)
{
return a.d<b.d;
}
int main()
{
int n,s,p;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d %d",&node[i].d,&node[i].v);
scanf("%d %d",&s,&p);
for(int i=1;i<=n;i++)
node[i].d=s-node[i].d;
sort(node+1,node+n+1,cmp);
priority_queue<Node> q;
n++;
node[n].v=0;node[n].d=s;//为了方便,将终点也设成一个加油站
int t=p,cnt=0;
for(int i=1;i<=n;i++)
{
int x=node[i].d-node[i-1].d;
while(x>t) /*刚开始是写成if,,wrong 了无数次,后来才发现到达某个点不一定是取一个加油站,可以取多个然后过该点。*/</a></span>
<span style="font-size:24px;"><a target=_blank href="http://poj.org/searchproblem?field=source&key=USACO+2005+U+S+Open+Gold" style="text-decoration: none;"> {
if(q.empty())
{
printf("-1\n");
return 0;
}
t+=q.top().v;
q.pop();
cnt++;
}
q.push(node[i]);/*能到达该点,就能在它这里加油,因此入维护油量最大的优先队列*/
t-=x;
}
printf("%d\n",cnt);
return 0;
}</a><a target=_blank href="http://poj.org/searchproblem?field=source&key=USACO+2005+U+S+Open+Gold">
</a></span>
poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!的更多相关文章
- POJ 2431 Expedition (贪心+优先队列)
题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...
- poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
- POJ 2431 Expedition(优先队列、贪心)
题目链接: 传送门 Expedition Time Limit: 1000MS Memory Limit: 65536K 题目描述 驾驶一辆卡车行驶L单位距离.最开始有P单位的汽油.卡车每开1 ...
- poj - 2431 Expedition (优先队列)
http://poj.org/problem?id=2431 你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇, ...
- poj 2431 Expedition 贪心
简单的说说思路,如果一开始能够去到目的地那么当然不需要加油,否则肯定选择能够够着的油量最大的加油站加油,,不断重复这个贪心的策略即可. #include <iostream> #inclu ...
- POJ 2431 Expedition 贪心 优先级队列
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30702 Accepted: 8457 Descr ...
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
- POJ 2431 Expedition (贪心 + 优先队列)
题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...
随机推荐
- 文件操作:w,w+,r,r+,a,wb,rb
1.文件操作是什么? 操作文件: f = open("文件路径",mode="模式",encoding="编码") open() # 调用操 ...
- mysql45讲
一共48讲,学习时间2天. 开篇词 (1讲) 开篇词 | 这一次,让我们一起来搞懂MySQL 理论指导实践:先系统性的学习原理,再实践验证. 你可以从点到线再到面,形成自己到mysql知识网络. 在使 ...
- UDP通信简单 小结
Android手机版和电脑版 效果图: 通过WiFi局域网 电脑和手机连接通信. 电脑版本和手机版本使用了相同的消息发送头协议, 可以相互接收消息. 若有做的不好的地方还希望大家指导一下. 1. 手机 ...
- 【zhifu】web开发中的支付宝支付和微信支付
一.支付类型: 支付宝支付: 支付宝app内的网页支付: app外(即普通浏览器)网页支付: 微信支付: 微信app内的支付(在这里叫公众号支付) app外的支付(微信H5支付): 微信公众号的支付宝 ...
- 使用 “Unicode 字符集 ” 使用错误,应该使用 “使用多字节字符集”
“void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)”: 不能将参数 1 从“const char ...
- 快速写个node命令行工具
1.package.json-bin配置 [创建bat文件,把bat路径添加到PATH中]这些固定的工作可以由npm帮我们完成.package.json中有个bin字段配置: bin: { " ...
- 记一次被自己DDOS攻击
服务器报警初步分析进一步分析最终分析总结 TOC 服务器报警 7月24号下午5点半开始,突然服务器报警,检查监控,发现CPU异常100%. 该服务器正常情况下CPU使用率在40%已经算高了,另外负载经 ...
- vue中移动端滚动事件,点击一次触发了事件两次(better-scroll)
解决办法一: 将button标签换成a标签 问题代码: <span class="submitBtn" @click.stop="replyReport()&quo ...
- JavaWeb【Servlet】
概念 Servlet是在服务器上运行的小程序.一个Servlet请求对应一个Java类(对应一个Wrapper容器),可以通过请求-响应模式访问这个驻留在内存中的小程序. Tomcat容器等级 上图表 ...
- fastadmin 随笔 刷新表格数据 获取当前登录人信息 服务端导出Excel
table.bootstrapTable('refresh',{url:'你的url'}); 获取当前登录人信息 $this->auth就能获取当前用户信息,比如$this->auth-& ...