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. ...
随机推荐
- 前台JSON字符串,spring mvc controller也接收字符串
前台JSON字符串,spring mvc controller也接收字符串 前台: $.post(url, { data : JSON.stringify(obj) }, function(data) ...
- ios最新设置状态栏字体颜色总结
状态栏的字体为黑色:UIStatusBarStyleDefault 状态栏的字体为白色:UIStatusBarStyleLightContent 一.在info.plist中,将View contro ...
- cacti安装和第三方模块的导入
Cacti是一套基于PHP,MySQL,SNMP及RRDTool开发的网络流量监測图形分析工具.Cacti是通过 snmpget来获取数据,使用 RRDtool绘绘图形,并且你全然能够不须要了解RRD ...
- Python入门 不必自己造轮子
操作list list切片 字符串的分割 字符串的索引和切片 读文件 f = file('data.txt') data = f.read() print data f.close() 写文件 dat ...
- 0423-mysql插入语句大全
/*注意: 1.字段和值要一一对应 2.值的数据类型是字段的数据类型 3.当输入的字段是表中全部字段时,字段可以省略不写: insert into login values ('zhangsan',‘ ...
- Appium + python - swipe滑屏操作实例
方法一: from appium import webdriverfrom time import sleep descred_caps = { "platformName":&q ...
- idea常用快捷键(转)
---恢复内容开始--- IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表达式时按 “!”键 Ctrl+E,最近的文件 Ctrl+S ...
- qW3xT.2挖矿病毒 解决过程及坑
周一早上老大让我把项目更新一下,然后配置一下elasticsearch,我登上服务器之后部署的时候没有什么感觉,但是在配置elasticsearch的过程中感觉服务器哪个地方有点不对,下意识的top了 ...
- Oracle配置说明
当Oracle安装完成后,为后续能够顺利得导出空表,特做一下配置(重点关注2.1) 1.1.查询空表select table_name from user_tables where NUM_ROWS= ...
- EasyUI DataGrid组织事件冒泡
在事件内部需要阻止的地方添加如下代码 ]; 解释: arguments.callee是获得我自定义的事件处理方法OnSelected的方法体. .caller是获得调用OnSelected的上层方法( ...