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个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...
随机推荐
- Java实现的基础数据结构
Java实现的基础数据结构 0,常用的基础数据结构 图1 基础数据结构&相关特性 图2 Java自带的类集框架&继承关系图 1,数组[Array] 特点:长度固定.查找方便[直接使用i ...
- centos中mariadb的相关操作
Tip 1 在使用mariadb中启动服务报错 : Failed to start mariadb.service: Unit not found. 解决办法: yum install -y mari ...
- mysql java jdbc 如何 update select
2019年8月6日17:28:07 sql 不知道怎么写,也没去查,因为需求可能中途需要修改值,有点麻烦 直接用jdbc实现. 查询出来的值,直接根据update条件更新,写在一个方法里 public ...
- js 前端请求头里传 token
参考:https://blog.csdn.net/qq_34309704/article/details/80572077 1.Token:token是客户端频繁向服务器端请求数据,服务器频繁的去数据 ...
- jsp引入文件时候经常遇到的${ctx}
jsp引入文件时候经常遇到的${ctx} 在jsp页面中经常见到这样的代码: <script type="text/JavaScript" src="${ctx}/ ...
- Mac EI 10.11.3 MySQL5.7.11 .dmg 安装(便捷设置,密码重置,卸载)
MySQL 5.7+ 安装成功以后会弹出一个临时密码 后面需要通过临时密码设置新的密码 重置root密码:安装成功后,使用临时密码登陆:敲入命令,mysqladmin -u root -p passw ...
- django 使用mysql数据库
一 修改settings里面的配置文件 import pymysql # 一定要添加这两行!通过pip install pymysql! 或者pycharm 里面安装 pymysql.install_ ...
- 解决办法:Message: 对实体 "useUnicode" 的引用必须以 ';' 分隔符结尾
Hibernate 5.3.1 INFO: HHH000206: hibernate.properties not foundException in thread "main" ...
- python面向编程:面向对象、init、绑定方法、案例练习
一.类的定义 二.面向对象概念三.对象的使用四.__init__函数的使用五.绑定方法六.面向对象联系 一.类的定义 1.什么叫做类? 类就是分类,类型的意思,一堆具备相同特征和行为的事物的抽象概念 ...
- fragment初步认识