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 ...
随机推荐
- 马士兵老师hadoop讲解总结博客地址记录(啊啊啊啊啊,自己没有保存写好的博客...)
http://www.cnblogs.com/yucongblog/p/6650822.html
- UVALive - 6887 Book Club 有向环的路径覆盖
题目链接: http://acm.hust.edu.cn/vjudge/problem/129727 D - Book Club Time Limit: 5000MS 题意 给你一个无自环的有向图,问 ...
- J2EE,J2SE,J2ME,JDK,SDK,JRE,JVM区别(转载)
转载地址:http://blog.csdn.net/alspwx/article/details/20799017 一.J2EE.J2SE.J2ME区别 J2EE——全称Java 2 Enterpri ...
- 使用fprof基本步骤
$erl -name a@localhost -setcookie abc -remsh b@localhost >fprof:trace([start, {file, "/home/ ...
- "数学口袋精灵"bug
首先要部署这个app项目就是第一步: 一.前提下载并安装JDK 在线图解:手把手教你安装JDK http://www.lvtao.net/server/windows-setup-jdk.h ...
- 小记IptabLes和IptabLex病毒清理过程
去年有台Linux服务器被黑了,看了500万行日志(现在觉得当时好厉害呀),反正当时的日志文件有700Mb以上大.前两天师兄告诉我,信息中心的老师给他说我们有台服务器应该是被人入侵了,当作内网的跳板, ...
- 框架整合小小总结【SSH】注解式
Spring 注解式注册 bean: 大致分为以下几步: 开启 context 空间支持 开启自动扫描功能,指定扫描包路径 使用注解配置 bean (使用@Component 注解) 给 bean 注 ...
- hdu-题目1159:Common Subsequence
http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Java/Oth ...
- 【JavaScript】JAVA-js中比较日期大小
业务场景:js中根据yyyy-MM-dd格式的日期进行比较来动态显示相关图标的出现与否 var DS173305 = { DS173305Grid: null, initDataGrid: funct ...
- "strcmp()" Anyone? UVA - 11732(trie出现的次数)
给你n个单词,让他们两两比较,要求他们运用strcmp时,进行比较的次数. 边建树边统计 #include <iostream> #include <cstdio> #incl ...