POJ2431 优先队列+贪心 - biaobiao88
以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int x;
int y;
bool operator<(const node &a) const//此操作是对操作符"<"进行重构
{
return x < a.x;//对结构体数组x进行从大到小排序
// return y > a.y;//对结构体数组y进行从大到小排序
}
}s[100]; //bool cmp(int x,int y)//这个重构函数不能用在结构体数组中
//{
// return x > y;
//} int main()
{
int n;
cin >> n;
for(int i = 0;i < n;i++)
cin >> s[i].x >> s[i].y; sort(s,s+n);
for(int i = 0;i < n;i++)
cout << s[i].x << " " << s[i].y << endl;
cout << endl;
return 0;
}
运行结果:
也可以这样
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int x;
int y;
bool operator<(const node &a) const
{
return x > a.x;
}
}s[]; bool cmp(node m,node n)
{
m.x < n.x;
} int main()
{
int n;
cin >> n;
for(int i = ;i < n;i++)
cin >> s[i].x >> s[i].y; sort(s,s+n);
for(int i = ;i < n;i++)
cout << s[i].x << " " << s[i].y << endl;
cout << endl;
return ;
}
对优先队列的应用,POJ2431是一个很好的题目,此题用了优先队列+贪心
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 29720 | Accepted: 8212 |
Description
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
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
int dis;
int fuel;
bool operator<(const node &a) const//此重构操作,请参考上面的代码
{
return dis > a.dis;//返回dis,说明是对dis这个数组进行排序操作
}
}stop[]; priority_queue<int> que; int main()
{
int n,l,p;
cin >> n;
for(int i = ;i < n;i++)
cin >> stop[i].dis >> stop[i].fuel;
cin >> l >> p;
int ans = ;
sort(stop,stop + n);//对加油站距离dis数组进行从大到小排序
que.push(p);//队列自动从大到小排序,即排序汽油p
int temp = ;
while(l > && !que.empty())//卡车未到达终点并且卡车在当前汽油用完前有路过加油站
{
ans++;//卡车停下加一次油计数器
l -= que.top();//加油,更新一次汽油用尽后距离终点的距离
que.pop();//删除已用的加油站的汽油
while(l <= stop[temp].dis && temp < n)//卡车距离终点的距离小于等于最近加油站的距离并且这个加油站的位置在终点加油站前面,这里假设终点也为一个加油站。//l <= stop[i].dis意思是卡车能经过离它最近的一个加油站,如果大于的话,说明卡车停下时没有加油站可加油
que.push(stop[temp++].fuel);//将经过的加油站压入优先队列,要使用的时候就取队头元素(队头中存的汽油最大) //如果经过加油站,则一定要将该加油站的可加油量添加到优先队列当中
}//temp++说明离卡车最近的加油站,卡车继续往前开,加油站点也依次往后,所以变量temp需要自增
if(l > )//如果卡车距离终点的距离还大于0的话,即通过不了终点
cout << "-1" << endl;
else
cout << ans - << endl;//在起点深度时候记为一次加油,这里需要减去1
return ;
}
其中代码有详细的注释,希望注释能加深理解
代码参考于:博客园-小小菜鸟
POJ2431 优先队列+贪心 - biaobiao88的更多相关文章
- 最高的奖励 - 优先队列&贪心 / 并查集
题目地址: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(优先队列+贪心)
题目链接:http://poj.org/problem?id=2431 题目大意:一辆卡车,初始时,距离终点L,油量为P,在起点到终点途中有n个加油站,每个加油站油量有限,而卡车的油箱容量无限,卡车在 ...
- H - Expedition 优先队列 贪心
来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ...
- ZOJ-3410Layton's Escape(优先队列+贪心)
Layton's Escape Time Limit: 2 Seconds Memory Limit: 65536 KB Professor Layton is a renowned arc ...
- CodeForces - 853A Planning (优先队列,贪心)
Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...
- 1350: To Add Which? (优先队列+贪心 或者 数组模拟)
1350: To Add Which? Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitt ...
- 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 C.二元-K个二元组最小值和最大-优先队列+贪心(思维)
链接:https://ac.nowcoder.com/acm/contest/558/C来源:牛客网 小猫在研究二元组. 小猫在研究最大值. 给定N个二元组(a1,b1),(a2,b2),…,(aN, ...
- hdu 4544 优先队列+贪心
题意:最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏.游戏规则很简单,用箭杀死免子即可.箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别为Di ...
随机推荐
- Spring 梳理-接收请求的输入(原)
Spring MVC 允许一下方式将客户端的数据传送到控制器的处理方法中 查询参数(Query Parameter) 表单参数(Form Parameter) 路径变量(Path Variable ...
- Spring MVC-从零开始-@RequestMapping 注解headers 属性
package com.jt; import org.springframework.stereotype.Controller; import org.springframework.web.bin ...
- 每个人都要学的图片压缩终极奥义,有效解决 Android 程序 OOM
# 由来 在我们编写 Android 程序的时候,几乎永远逃避不了图片压缩的难题.除了应用图标之外,我们所要显示的图片基本上只有两个来源: 来自网络下载 本地相册中加载 不管是网上下载下来的也好,还是 ...
- 在vue的mounted下使用setInterval的误区
1. vue对象的生命周期 1). 初始化显示(只执行一次) * beforeCreate() * created() * beforeMount() * mounted() 2). 更新状态(可执行 ...
- win7远程连接全屏和窗口模式切换
最近开发需要win7远程连接,我知道在连接的时候可以设置全屏模式 但是进去之后想要切换就只能通过快捷键了上网查了一下是ctrl+alt+break.网上说的没有错.我查官方文档也是这样.但是我按的时候 ...
- vue-cli 脚手架安装
1.安装node;选择适合自己系统的文件,下载一路next , a安装成功后,打开运行输入cmd 进入命令行: 在命令行工具中输入 npm -v 检查版本号 如果出现 则安装成功:(npm为node ...
- Flume 学习笔记之 Flume NG概述及单节点安装
Flume NG概述: Flume NG是一个分布式,高可用,可靠的系统,它能将不同的海量数据收集,移动并存储到一个数据存储系统中.轻量,配置简单,适用于各种日志收集,并支持 Failover和负载均 ...
- Ionic2优于Ionic1的6个理由
经历了一个从0到有的app的开发,我已经很熟悉Ionic1了,在此期间我曾发现过Ionic1的一些bug,和一些不合理的地方(根基版本 不同,后续我会陆续发表这些文章),我甚至在此期间对Ionic1进 ...
- .Net Core上传文件到服务器
/// <summary> /// 上传文件 /// </summary> /// <returns></returns> [HttpPost(&quo ...
- Fcitx使用搜狗词库与皮肤
在 \(\text{Linux}\) 环境下,\(\text{Fcitx}\) 确实是最好用的开源输入法之一.然而 \(\text{Windows}\) 下的巨头输入法 -- 搜狗,对 \(\text ...