POJ 2431 优先队列
汽车每过一单位消耗一单位油,其中有给定加油站可加油,问到达终点加油的最小次数。
做法很多的题,其中优先对列解这题是很经典的想法,枚举每个加油站,判断下当前油量是否小于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 优先队列的更多相关文章
- POJ 2431 Expedition (贪心+优先队列)
题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...
- poj 2431 【优先队列】
poj 2431 Description A group of cows grabbed a truck and ventured on an expedition deep into the jun ...
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
- poj - 2431 Expedition (优先队列)
http://poj.org/problem?id=2431 你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇, ...
- POJ 2431 Expedition (贪心 + 优先队列)
题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...
- POJ 2431——Expedition(贪心,优先队列)
链接:http://poj.org/problem?id=2431 题解 #include<iostream> #include<algorithm> #include< ...
- 优先队列(挑程)poj 2431
每次写poj的题都很崩溃,貌似从来没有一次一发就ac的,每次都有特别多的细节需要考虑.还有就是自己写的太粗糙了,应该把每种情况都想到的,总是急着交,然后刷一页wa. 优先队列直接用stl就可以,简单实 ...
- poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...
- poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10025 Accepted: 2918 Descr ...
随机推荐
- Beta冲刺第二周王者荣耀交流协会第四次会议
1.例会照片: 成员:王超,高远博,冉华,王磊,王玉玲,任思佳,袁玥全部到齐. master:王玉玲 2.时间跨度: 2017年11月20日 18:00 — 18:13,总计13分钟. 3.地点: 一 ...
- ncnblogs.com的用户体验
你是什么样的用户, 有什么样的心理, 对cnblogs 的期望值是什么? 我是一名普通的学生,上cnblogs的期望是发表博客完成老师布置的任务. 当你第一次使用cnblogs 的功能的时候, 碰到了 ...
- 按照Right-BICEP要求设计四则运算3程序的单元测试用例
按照Right-BICEP要求: Right——结果是否正确? B——是否所有的边界条件都是正确的? I——能查一下反响关联吗? C——能用其它手段交叉检查一下吗? E——你是否可以强制错误条件发生? ...
- jQuery之元素查找
在已经匹配出的元素集合中根据选择器查找孩子/父母/兄弟标签1. children(): 子标签中找2. find() : 后代标签中找3. parent() : 父标签4. prevAll() : 前 ...
- css选择器和新增UI样式总结
经过两天的学习,初步对css3选择器和新增UI样式有了进一步的理解.
- 用iptables做代理
出于安全考虑,Linux系统默认是禁止数据包转发的.配置Linux系统的ip转发功能,打开系统转发功能:echo "1" > /proc/sys/net/ipv4/ip_fo ...
- 各大巨头电商提供的IP库API接口-新浪、搜狐、阿里
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js (不可用)新浪多地域测试方法:http://i ...
- Android内存泄漏第一课【转】--------(使用单例模式造成的内存泄漏)
使用单例模式造成的内存泄漏 Android的单例模式在我们项目开发中经常会用到,不过使用的不恰当的话也会造成内存泄漏.因为单例的静态特性使得单例的生命周期和应用的生命周期一样长, 这就说明了如果一个对 ...
- Qt 事件处理机制
Qt 事件处理机制 因为这篇文章写得特别好,将Qt的事件处理机制能够阐述的清晰有条理,并且便于学习.于是就装载过来了(本文做了排版,并删减了一些冗余的东西,希望原主勿怪),以供学习之用. 简介 在Qt ...
- pthread的pthread_join()函数理解实验
一.使用方式 pthread_t tid;pthread_create(&tid, NULL, thread_run,NULL);pthread_join(tid,NULL);创建线程之后直接 ...