题目传送门

题意:一辆卡车要行驶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. 标准代码书写 C++ 的string类的用法总结

    相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果 离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是 ...

  2. npm ERR! code EINTEGRITY npm ERR! sha1- 报错解决办法

    npm ERR! code EINTEGRITY npm ERR! sha1- 报错日志 npm ERR! code EINTEGRITY npm ERR! sha1-OGchPo3Xm/Ho8jAM ...

  3. 文件宝iOS/iPhone/iPad客户端简介

    App Store地址:https://itunes.apple.com/cn/app/id1023365565?mt=8 文件宝-装机必备的文件管家,专业的rar-zip 解压工具,局域网看片神器, ...

  4. POJ 2482 Stars in Your Window(线段树+扫描线)

    题目链接 非常不容易的一道题,把每个点向右上构造一个矩形,将问题转化为重合矩形那个亮度最大,注意LL,注意排序. #include <cstdio> #include <cstrin ...

  5. MongoDB Spark Connector

    [在文件/usr/bin/yum./usr/libexec/urlgrabber-ext-down最上面的一行改为#!/usr/bin/python2.7] yum install git Note: ...

  6. Hibernate exception

    1.a different object with the same identifier value was already associated with the session. 错误原因:在h ...

  7. 使用C语言获取字符串或文件的MD5值

    libmd5地址:https://sourceforge.net/projects/libmd5-rfc/ MD5Demo1.c #include <stdio.h> #include & ...

  8. fminunc

    options = optimset('GradObj', 'on', 'MaxIter', 400); % Run fminunc to obtain the optimal theta% This ...

  9. rails用generate为两个模型创建has_and_belongs_to_many中间表

    假设 teachers和students具备many-to-many的关系,那么需要一个Join 表,has_and_belongs_to_many默认该表名字为teachers_students,这 ...

  10. leetcode 684. Redundant Connection

    We are given a "tree" in the form of a 2D-array, with distinct values for each node. In th ...