汽车每过一单位消耗一单位油,其中有给定加油站可加油,问到达终点加油的最小次数。

做法很多的题,其中优先对列解这题是很经典的想法,枚举每个加油站,判断下当前油量是否小于0,小于0就在前面挑最大几个直至油量大于0。

虽然是道挺水的题目,但还是要注意细节...

/** @Date    : 2017-09-21 22:45:37
* @FileName: POJ 2431 优先队列.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
#include <math.h>
//#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; struct yuu
{
int x, l;
}a[10010];
int n, len, p;
int pos[N*10]; int cmp(yuu a, yuu b)
{
if(a.x != b.x)
return a.x > b.x;
return a.l > b.l;
}
int main()
{
int n;
while(~scanf("%d", &n))
{
for(int i = 0; i < n; i++)
{
int x, l;
scanf("%d%d", &x, &l);
a[i].x = x;
a[i].l = l;
}
a[n].x = 0;
a[n].l = 0;
sort(a, a + n + 1, cmp);
scanf("%d%d", &len, &p);
priority_queue<int, vector<int>, less<int> >q;
int ans = 0;
int pos = 0;
for(int i = 0; i <= n; i++)
{
p -= len - a[i].x - pos;
if(p < 0)
{
while(!q.empty() && p < 0)
p += q.top(), q.pop(), ans++;
if(p < 0)//这里要退出 忘写了...
{
ans = -1;
break;
} }
//cout << p << endl;
pos = len - a[i].x;
q.push(a[i].l);
}
if(p < 0)
cout << -1 << endl;
else
cout << ans << endl;
}
return 0;
}

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

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

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

  2. poj 2431 【优先队列】

    poj 2431 Description A group of cows grabbed a truck and ventured on an expedition deep into the jun ...

  3. POJ 2431 Expedition(探险)

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

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

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

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

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

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

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

  7. 优先队列(挑程)poj 2431

    每次写poj的题都很崩溃,貌似从来没有一次一发就ac的,每次都有特别多的细节需要考虑.还有就是自己写的太粗糙了,应该把每种情况都想到的,总是急着交,然后刷一页wa. 优先队列直接用stl就可以,简单实 ...

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

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

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

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

随机推荐

  1. AOP:静态代理实现方式①通过继承②通过接口

    文件结构: 添加日志: package com.wangcf.manager; public class LogManager { public void add(){ System.out.prin ...

  2. Android开发第二阶段(5)

    今天:对图片的替换修改,使整个app的图案化更美观. 明天:对Android的对sdcard的操作学习

  3. Android开发第二阶段(4)

    今天:对按扭位置重新调整了一下布局了一下,改变了layout中见面的字体格式等等是其更美观. 明天:对图片的修改和替换.

  4. lintcode-383-装最多水的容器

    383-装最多水的容器 给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai).画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)和(i, 0 ...

  5. WebService(二)

    使用eclipse开发webservice的服务器端以及客户端的简单实例 1.服务端 在eclipse中像建立一个web项目一样,new->Dynamic Web Project A.建一个需要 ...

  6. Android性能测试工具APT

    APT源码地址:https://code.csdn.net/Tencent/apt APT,Android Performance Testing Tools,适用于开发自测和定位性能瓶颈,帮助测试人 ...

  7. CSS单位-长度

    css中的长度单位有很多,不同的单位在特定的需求下能够有相当不错的表现,随着css3的发布,又有了一些新的单位添加进来,使我们在做前端页面的时候能够有更多的选择,更方便快捷的达到我们预期的效果. 正题 ...

  8. 【JavaScript】Json

    一.前言        接着上一章的内容,继续js的学习. 二.内容        解析与序列化 JSON.stringify() —— 将js对象序列化为JSON字符串,接收三个参数:1.js对象2 ...

  9. C/C++语言中让电脑随机的在某个范围中的任一随机数

    这是我在笔试中碰见的一题中一部分,这就就记录下来.举例,输出[1,3]中任一随机数. #include<iostream> #include<cstdlib> #include ...

  10. 洛谷 P1501 [国家集训队]Tree II 解题报告

    P1501 [国家集训队]Tree II 题目描述 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\( ...