Expedition
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29360   Accepted: 8135

Description

A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.

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

* Line 1: A single integer, N

* 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

INPUT DETAILS:

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】驾驶问题-贪心,优先队列的更多相关文章

  1. hihoCoder 1309:任务分配 贪心 优先队列

    #1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN,  ...

  2. UVA 11134 - Fabled Rooks(贪心+优先队列)

    We would like to place  n  rooks, 1 ≤  n  ≤ 5000, on a  n×n  board subject to the following restrict ...

  3. 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 ...

  4. HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解

    思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...

  5. 贪心+优先队列 HDOJ 5360 Hiking

    题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...

  6. [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)

    传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...

  7. Painting The Fence(贪心+优先队列)

    Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...

  8. CF140C New Year Snowmen(贪心+优先队列)

    CF140C 贪心+优先队列 贪心策略:每次取出数量最多的三种球,合成一个答案,再把雪球数都-1再插回去,只要还剩下三种雪球就可以不断地合成 雪球数用优先队列维护 #include <bits/ ...

  9. BZOJ1029: [JSOI2007]建筑抢修[模拟 贪心 优先队列]

    1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec  Memory Limit: 162 MBSubmit: 3785  Solved: 1747[Submit][Statu ...

随机推荐

  1. Struts2连接Mysql的Crud使用

    今天分享的是struts2框架中增删改查的用法: 一:利用Struts2框架 1.1在pom.xml中导入相关依赖 <project xmlns="http://maven.apach ...

  2. Luogu4081 USACO17DEC Standing Out from the Herd(广义后缀自动机)

    建出广义SAM,通过parent树对每个节点求出其是否仅被一个子串包含及被哪个包含. 写了无数个sam板子题一点意思都没啊 #include<bits/stdc++.h> using na ...

  3. (八)Redis之持久化之AOF方式

    一.概念 AOF方式:将以日志,记录每一个操作 优势:安全性相对RDB方式高很多: 劣势:效率相对RDB方式低很多: 二.案例 appendonly no默认关闭aof方式 我们修改成yes 就开启 ...

  4. 冒泡(bubblesort)、选择排序、插入排序、快速排序

    冒泡排序(bubblesort) 特点:通过换位置的方式,一直向上冒泡 package main import "fmt" func bubbleSortAsc(arrayA [] ...

  5. 用一个N点复序列的FFT同时计算两个N点实序列离散傅里叶变换

    一.功能 用一个\(N\)点复序列快速傅立叶变换算法来同时计算两个\(N\)点实序列的离散傅立叶变换. 二.方法简介 假设\(x(n)\)与\(y(n)\)都是长度为\(N\)的实序列,为计算其离散傅 ...

  6. Android 启动流程分析

    原文:https://www.jianshu.com/p/a5532ecc8377 作者曾经在高通的Android性能组工作,主要工作是优化Android Application的启动时间. APP基 ...

  7. Mac中设置网络优先级

     

  8. iframe中的target属性

    在使用iframe的时候,我们有时候会遇到,外面的链接,去操作iframe中的页面 <!DOCTYPE html> <html> <head> <meta c ...

  9. Kinect for Windows SDK开发入门(四):景深数据处理 上

    原文来自:http://www.cnblogs.com/yangecnu/archive/2012/04/04/KinectSDK_Depth_Image_Processing_Part1.html ...

  10. tensorflow几个常见错误

    错误一:二分类,标签y ValueError: Cannot feed value of shape (128,1) for Tensor u'input_y_2:0', which has shap ...