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个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...
随机推荐
- GTID复制
什么是GTID呢, 简而言之,就是全局事务ID(global transaction identifier ),最初由google实现,官方MySQL在5.6才加入该功能.GTID是事务提交时创建分配 ...
- 华为设备ACL与NAT技术
ACL 访问控制列表(Access Control Lists),是应用在路由器(或三层交换机)接口上的指令列表,用来告诉路由器哪些数据可以接收,哪些数据是需要被拒绝的,ACL的定义是基于协议的,它适 ...
- MySQL的简介、启动及其DDL
MySQL的各项配置: 默认会启用TCP/IP网络: 默认客户端/服务器端口:3306: 将数据库的BIN目录写入Windows的的path环境变量: 默认不允许root用户在其他机器上远程登录: M ...
- 【IntelliJ IDEA】从资源文件读取出来就中文乱码的解决方法
在application.properties资源文件中设置两个自定义的属性以及属性值: com.sxd.name = "德玛西亚" com.sxd.want = "王者 ...
- 点击导航目录页面滑动到指定div区域
$(document).on("click", ".navbar-nav li[link]", function() { nav.find('li').remo ...
- -bash: fork: retry: 没有子进程
今天遇到一个问题 -bash: fork: retry: 没有子进程 解决方法 设置各linux 用户的最大进程数,下面我把某linux用户的最大进程数设为10000个: ulimit -u 10 ...
- 阿里云自动获取token值(python)
一,token说明 token的意思是“令牌”,是服务端生成的一串字符串,作为客户端进行请求的一个标识.当用户第一次登录后,服务器生成一个token并将此token返回给客户端,以后客户端只需带上这个 ...
- JS基础_嵌套的for循环
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 如果您的浏览器不支持javascript功能
如果您的浏览器不支持javascript功能或被禁止使用,那么在访问许多网站(包括此网站)时,某些功能将不可用.我们建议您打开javascript功能以获得最佳的浏览效果.以下是打开它的可能原因和方法 ...
- ubuntu install opencv
1. install the newest opencv version pip install opencv-python