【poj2431】驾驶问题-贪心,优先队列
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 29360 | Accepted: 8135 |
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
Line 1: A single integer giving the minimum number of fuel stops
necessary to reach the town. If it is not possible to reach the town,
output -1.
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<cstdio>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<algorithm>
#define int long long
#define N 100050
using namespace std;
int n,L,p,sum,ans,cnt;
struct node
{
int dis,l;
bool operator < (const node &a) const
{
return dis > a.dis;
}
}e[N];
priority_queue <int> q;
signed main()
{
scanf("%lld",&n);
for(int i=0,a,b;i<n;i++)
{
scanf("%lld%lld",&e[i].dis,&e[i].l);
}
scanf("%lld%lld",&L,&p);
int tmp=0;
q.push(p);
sort(e,e+n);
while(L>0&&!q.empty())
{
L-=q.top();
ans++;
q.pop();
while(L<=e[tmp].dis&&tmp<n)
q.push(e[tmp++].l);
}
if(L>0) printf("-1\n");
else printf("%d\n",ans-1);
return 0;
}
【poj2431】驾驶问题-贪心,优先队列的更多相关文章
- hihoCoder 1309:任务分配 贪心 优先队列
#1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN, ...
- UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrict ...
- C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列
C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解
思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...
- 贪心+优先队列 HDOJ 5360 Hiking
题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...
- [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)
传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...
- Painting The Fence(贪心+优先队列)
Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...
- CF140C New Year Snowmen(贪心+优先队列)
CF140C 贪心+优先队列 贪心策略:每次取出数量最多的三种球,合成一个答案,再把雪球数都-1再插回去,只要还剩下三种雪球就可以不断地合成 雪球数用优先队列维护 #include <bits/ ...
- BZOJ1029: [JSOI2007]建筑抢修[模拟 贪心 优先队列]
1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec Memory Limit: 162 MBSubmit: 3785 Solved: 1747[Submit][Statu ...
随机推荐
- SAS学习笔记36 二分类logistic回归
这里所拟合模型的AIC和SC统计量的值均小于只有截距的模型的相应统计量的值,说明含有自变量的模型较仅含有常数项的要好 但模型的最大重新换算 R 方为0.0993,说明模型拟合效果并不好,可能有其他危险 ...
- sqlserver 聚集索引 非聚集索引
聚集索引是一种对磁盘上实际数据重新组织以按指定的一列或者多列值排序.像我们用到的汉语字典,就是一个聚集索引.换句话说就是聚集索引会改变数据库表中数据的存放顺序.非聚集索引不会重新组织表中的数据,而是对 ...
- mybaits实现oracle批量新增数据,回填主键
项目有需求,百度了很久,反正他们说的方法,我都没成功,我也不知道是不是我写代码的姿势不正确,没办法只能自己想法子了 我们这个项目用到了通过Mapper,通用Mapper里通过OracleProvide ...
- SQL优化中的重要概念:锁定
原文:SQL优化中的重要概念:锁定 上篇文章讲的是事务,这篇就引出另一个重要概念,就是锁定. 当一个用户要读取另一个用户正在修改的数据,或者一个用户正在修改另一个用户正在读取的数据,或者一个用户要修改 ...
- vue 数组对象取对象的属性: Cannot read property 'xxxx' of undefined
{{ list[0].name }} list[0]没有定义 能正确打印出想要的结果,但就是报错,外面套个v-for就没错了 很费解 看到文章说是与异步有关,解决办法: <template v- ...
- Abp SSO
官方的文档有个坑. 首先建立的应该是 .net core MPA版本. 把文档上的startup.cs配置写入 MVC 项目中. 这样测试才能通过.不然,测试项目 var disco = a ...
- JavaScript检测浏览器
Detect Browser <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- [NOIP2018模拟赛10.22]咕咕报告
闲扯 这是篇咕咕了的博客 考场上码完暴力后不知道干什么,然后忽然发现这个T1好像有点像一道雅礼集训时讲过的CF题目 Rest In Shades ,当时那道题还想了挺久不过思路比较妙,于是我就也\(y ...
- kong命令(三)route
介绍 route 是一套匹配客户端请求的规则.每个route都会匹配一个service,每个service可定关联多个route. 可以说service:route=1:n.一对多的关系.每个匹配到r ...
- Uniswap详解之一(概览)
一.Uniswap简介 Uniswap是以太坊上的DEX实现,基于"恒定乘积自动做市"模型,与传统的中心化和DEX具有很大的差别. 主要特点: 无订单簿,无做市商 兑换币具有很低的 ...