POJ 2431 Expedition(优先队列、贪心)
题目链接: 传送门
Expedition
Time Limit: 1000MS Memory Limit: 65536K
题目描述
驾驶一辆卡车行驶L单位距离。最开始有P单位的汽油。卡车每开1单位距离消耗1单位的汽油。在途中一共有N个加油站。假设卡车的燃料箱容量无限大,问如果到达终点最少的加油次数。
思路
在卡车开往终点途中,只有在加油站才可以加油,换做认为“在到达加油站i时,就获得了一次在之后任何时候都可以加油的权利”
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
struct oil{
int dis,fuel;
};
bool cmp(struct oil x,struct oil y)
{
return x.dis <= y.dis;
}
int main()
{
int N;
while (scanf("%d",&N) != EOF)
{
struct oil stops[10005];
priority_queue<int>pque;
int L,P;
for (int i = 0;i < N;i++)
{
scanf("%d%d",&stops[i].dis,&stops[i].fuel);
}
scanf("%d%d",&L,&P);
for (int i = 0;i < N;i++)
{
stops[i].dis = L - stops[i].dis;
}
sort(stops,stops + N,cmp);
int cnt = 0;
bool flag = true;
for (int i = 0;i < N;i++)
{
if (P >= L)
{
break;
}
if (P < stops[i].dis)
{
if (pque.empty())
{
flag = false;
break;
}
else
{
while (!pque.empty() && P < stops[i].dis)
{
cnt++;
int tmp = pque.top();
pque.pop();
P += tmp;
}
if (pque.empty() && P < stops[i].dis)
{
flag = false;
break;
}
pque.push(stops[i].fuel);
}
}
else
{
pque.push(stops[i].fuel);
}
}
if (P < L && !pque.empty())
{
int tmp = pque.top();
pque.pop();
P += tmp;
cnt++;
}
if (!flag)
{
printf("-1\n");
}
else
{
printf("%d\n",cnt);
}
}
return 0;
}
POJ 2431 Expedition(优先队列、贪心)的更多相关文章
- POJ 2431 Expedition【贪心】
题意: 卡车每走一个单元消耗一升汽油,中途有加油站,可以进行加油,问能否到达终点,求最少加油次数. 分析: 优先队列+贪心 代码: #include<iostream> #include& ...
- POJ 2431 Expedition (贪心 + 优先队列)
题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...
- POJ 2431——Expedition(贪心,优先队列)
链接:http://poj.org/problem?id=2431 题解 #include<iostream> #include<algorithm> #include< ...
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
- poj 3431 Expedition 优先队列
poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ...
- POJ 2431 Expedition (贪心+优先队列)
题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...
- poj 2431 【优先队列】
poj 2431 Description A group of cows grabbed a truck and ventured on an expedition deep into the jun ...
- poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...
- poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10025 Accepted: 2918 Descr ...
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
随机推荐
- Theano2.1.1-基础知识之准备工作
来源:http://deeplearning.net/software/theano/tutorial/index.html#tutorial 这里介绍的是使用theano的一些基础知识,虽然thea ...
- opencv6.3-imgproc图像处理模块之边缘检测
接opencv6.2-improc图像处理模块之图像尺寸上的操作 本文大部分都是来自于转http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutori ...
- SQLServer(MSSQL)、MySQL、SQLite、Access相互迁移转换工具 DB2DB v1.4
最近公司有一个项目,需要把原来的系统从 MSSQL 升迁到阿里云RDS(MySQL)上面.为便于测试,所以需要把原来系统的所有数据表以及测试数据转换到 MySQL 上面.在百度上找了很多方法,有通过微 ...
- 基于DDD的.NET开发框架 - ABP启动配置
返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...
- C# 序列化与反序列化
我们先说说什么是序列化.所谓的序列化就是把要保存的内容以特定的格式存入某种介质中.比如我们要保存一些数据在下次程序启动的时候再读入.我们一般就是把这个数据写在一个本地的文本中.然后程序启动的时候去读入 ...
- How to build windows azure PowerShell Source Code
Download any version source code of Windows Azure Powershell from https://github.com/Azure/azure-sdk ...
- Android Studio 优秀插件汇总
第一部分 插件的介绍 Google 在2013年5月的I/O开发者大会推出了基于IntelliJ IDEA java ide上的Android Studio.AndroidStudio是一个功能齐全的 ...
- jquery 获取Select option 选择的Text和Value
jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关设置 获取一组radio被选中项的值:var item = $(' ...
- Chrome DevTools good good study day day up
Chrome DevTools 官方页面 https://developer.chrome.com/devtools
- Activiti 学习笔记(2016-8-30)
前言 不管学习什么,都必须对知识体系中专有的名词或者特定的语言组织方式 有所了解. 本文仅记录学习过程中的笔记,参考意义因人而定,不保证所言全部正确. 学习方式,百度传课的视频<权威Activi ...