POJ2431-Expedition【优先队列+贪心】
题目大意:卡车每走一公里就消耗一单位的汽油,初始时给你p单位油,你要到达l距离的终点。其中路上有n个补给点可以加油,并且油箱容量无限大,问你最少可以停车几次。
思路:因为油箱无限大,所以我们可以这么认为,我们路过一个加油站之后,我们在之后的路上随时可以选择加那个加油站的油,而且肯定是一次加完B_i,所以我们从汽车初始状态开始开,到没油了,看看路上路过有加油站没,选路过过油最多的,加上,继续这样。最后加油次数一定是最少的
#include<cstdio>
#include<queue>
#include <iostream>
#include<algorithm>
using namespace std;
const int maxn=10005;
struct node
{
int d,f;
};
node a[maxn];
bool cmp(node x,node y)
{
return x.d>y.d;
}
int main()
{
int n,l,p;
while(scanf("%d",&n)!=EOF)
{
priority_queue<int> q;
for(int i=0;i<n;++i)
scanf("%d%d",&a[i].d,&a[i].f);
sort(a,a+n,cmp);
scanf("%d%d",&l,&p);
int ans=0;
q.push(p);
int index=0;
while(l>0 && !q.empty())
{
++ans;
int temp=q.top();
q.pop();
l-=temp;
while(index<n && l<=a[index].d)
{
q.push(a[index].f);
++index;
}
}
if(l<=0)
printf("%d\n",ans-1);
else
printf("-1\n");
}
return 0;
}
POJ2431-Expedition【优先队列+贪心】的更多相关文章
- H - Expedition 优先队列 贪心
来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ...
- poj2431(优先队列+贪心)
题目链接:http://poj.org/problem?id=2431 题目大意:一辆卡车,初始时,距离终点L,油量为P,在起点到终点途中有n个加油站,每个加油站油量有限,而卡车的油箱容量无限,卡车在 ...
- poj2431 Expedition优先队列
Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Bein ...
- POJ2431 优先队列+贪心 - biaobiao88
以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...
- poj 3431 Expedition 优先队列
poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ...
- POJ 2431 Expedition【贪心】
题意: 卡车每走一个单元消耗一升汽油,中途有加油站,可以进行加油,问能否到达终点,求最少加油次数. 分析: 优先队列+贪心 代码: #include<iostream> #include& ...
- 最高的奖励 - 优先队列&贪心 / 并查集
题目地址:http://www.51cpc.com/web/problem.php?id=1587 Summarize: 优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先: ...
- hdu3438 Buy and Resell(优先队列+贪心)
Buy and Resell Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- POJ2431 Expedition(排序+优先队列)
思路:先把加油站按升序排列. 在经过加油站时.往优先队列里增加B[i].(每经过一个加油站时,预存储一下油量) 当油箱空时:1.假设队列为空(能够理解成预存储的油量),则无法到达下一个加油站,更无法到 ...
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
随机推荐
- NGINX之——配置HTTPS加密反向代理訪问–自签CA
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46695495 出于公司内部訪问考虑,採用的CA是本机Openssl自签名生成的,因 ...
- HDU 2563 统计问题(递推)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=2563 将向上移的步数设为a[n],将向左右移的步数设为b[n],有a[n]=a[n-1]+b[n-1 ...
- 黑马程序猿——————java基础
一.软件开发 软件是什么? 软件是简单的来说,计算机数据和指令的集合,数据(比方年龄,性别).指令及时告诉计算机怎样对他进行处理.计算机但是没有人那么聪明啊! 二.图形化界面(GUI),主要特点就是. ...
- Android之——常见Bug及其解决方式
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46942139 1.android.view.WindowManager$BadTo ...
- nyoj--990--蚂蚁感冒(模拟)(思维题)
蚂蚁感冒 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 长100厘米的细长直杆子上有n只蚂蚁.它们的头有的朝左,有的朝右. 每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/ ...
- codeforces 916E Jamie and Tree dfs序列化+线段树+LCA
E. Jamie and Tree time limit per test 2.5 seconds memory limit per test 256 megabytes input standard ...
- 安装Windows包管理工具Chocolatey
1.开始菜单里面用PS的管理员模式打开,执行一下命令. Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object Syste ...
- Cracking the Coding Interview 10.7
Design an algorithm to find the kth number such that the only prime factors are 3,5 and 7 方法一: a[i]= ...
- css下拉菜单写法
网页导航栏的下拉效果,通过div框的显示和隐藏实现. <html> <head> <meta charset="UTF-8"> <titl ...
- 开源作品-PHP写的Redis管理工具(单文件绿色版)-SuRedisAdmin_PHP_1_0
前言:项目开发用到了Redis,但是在调试Redis数据的时候,没有一款通用的可视化管理工具.在网络找了一些,但是感觉功能上都不尽人意,于是决定抽出一点时间,开发一个用起来顺手的Redis管理工具.秉 ...