题目传送门

题意:一辆卡车要行驶L长度,初始有P油,每行驶一个单位长度消耗一单位油。有n个加油站可以加油,问最少加油几次才能行驶L长度,如果不能输出-1

分析:按照挑战书的解法,每走到一个加油站相当于获得一次加油的权利,等到油没有的时候再选择之前可加油的站的最大油量加上,可以用优先队列高效得到最大值,如果队列里没油使得继续前进则为-1

收获:换一种思考方式,加上高效的数据结构能完美解决难题

代码:

/************************************************
* Author :Running_Time
* Created Time :2015/9/14 星期一 08:28:25
* File Name :POJ_2431.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e4 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct Oil {
int p, v;
bool operator < (const Oil &r) const {
return p < r.p;
}
}o[N]; int main(void) {
int n;
while (scanf ("%d", &n) == 1) {
int L, P;
for (int i=n; i>=1; --i) scanf ("%d%d", &o[i].p, &o[i].v);
scanf ("%d%d", &L, &P);
for (int i=n; i>=1; --i) o[i].p = L - o[i].p; //起点到加油站的距离
o[++n].p = L; o[n].v = 0;
sort (o+1, o+1+n);
priority_queue<int> Q;
int ans = 0, pos = 0;
for (int i=1; i<=n; ++i) {
int d = o[i].p - pos;
P -= d;
while (P < 0) {
if (Q.empty ()) {
ans = -1; break;
}
P += Q.top (); Q.pop ();
ans++;
}
if (ans == -1) break;
pos = o[i].p;
Q.push (o[i].v);
}
printf ("%d\n", ans);
} return 0;
}

  

优先队列 POJ 2431 Expedition的更多相关文章

  1. POJ 2431 Expedition(探险)

    POJ 2431 Expedition(探险) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A group of co ...

  2. POJ 2431 Expedition (贪心+优先队列)

    题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...

  3. poj - 2431 Expedition (优先队列)

    http://poj.org/problem?id=2431 你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇, ...

  4. POJ 2431 Expedition (贪心 + 优先队列)

    题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...

  5. POJ 2431——Expedition(贪心,优先队列)

    链接:http://poj.org/problem?id=2431 题解 #include<iostream> #include<algorithm> #include< ...

  6. poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10025   Accepted: 2918 Descr ...

  7. POJ 2431 Expedition (优先队列+贪心)

    题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...

  8. poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...

  9. POJ 2431 Expedition(优先队列、贪心)

    题目链接: 传送门 Expedition Time Limit: 1000MS     Memory Limit: 65536K 题目描述 驾驶一辆卡车行驶L单位距离.最开始有P单位的汽油.卡车每开1 ...

随机推荐

  1. 【献给CWNU的师弟】Web篇

    2014年10月8日 献给CWNU的师弟

  2. hadoop reduce 阶段遍历 Iterable 的 2 个“坑”

    01 package com.test; 02   03 import java.util.ArrayList; 04 import java.util.Iterator; 05 import jav ...

  3. 微信小程序 navigateTo 传对象参数

    当微信小程序navigateTo传入参数是个object时,请使用JSON.strtingify将object转化为字符串,代码如下: wx.navigateTo({ url: '../sendChe ...

  4. java包和javax包的区别

    基本类库和扩展类库 一般的lang,util都放在java.包 servlet放在javax包 以前sun把java中的叫核心库,把javax中的叫扩展库.现在sun已经把java和javax中的都叫 ...

  5. HDU 5438 Ponds

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  6. Silverlight 2中实现文件上传和电子邮件发送

    Silverlight 2中实现文件上传和电子邮件发送 [收藏此页] [打印]   作者:IT168 TerryLee  2008-05-30 内容导航: 使用Web Service上传文件   [I ...

  7. [Selenium] Explicit wait 方法

    (1)  new WebDriverWait(driver, 10). until(ExpectedConditions.elementToBeClickable(locator)); (2)  ne ...

  8. BZOJ_4609_[Wf2016]Branch Assignment_决策单调性+带权二分

    BZOJ_4609_[Wf2016]Branch Assignment_决策单调性+带权二分 Description 要完成一个由s个子项目组成的项目,给b(b>=s)个部门分配,从而把b个部门 ...

  9. 【USACO】 Balanced Photo

    [题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; int i,N,ans,l1,l2; ] ...

  10. C3P0Tool

    c3p0-config.xml <c3p0-config> <named-config name="c3p0"> <property name=&qu ...