POJ 2431 Expedition (贪心+优先队列)
题目地址:POJ 2431
将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止。
代码例如以下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
struct node
{
int d, p;
}stop[11000];
int cmp(node x, node y)
{
return x.d<y.d;
}
int main()
{
int n, i, j, l, p, flag=0, s, cnt;
scanf("%d",&n);
priority_queue<int>q;
for(i=0;i<n;i++)
{
scanf("%d%d",&stop[i].d,&stop[i].p);
}
scanf("%d%d",&l,&p);
for(i=0;i<n;i++)
{
stop[i].d=l-stop[i].d;
}
stop[n].d=l;
stop[n].p=0;
sort(stop,stop+n+1,cmp);
cnt=0;
for(i=0;i<=n;i++)
{
if(stop[i].d<0) continue ;
if(p<stop[i].d)
{
while(!q.empty()&&p<stop[i].d)
{
p+=q.top();
q.pop();
cnt++;
}
if(p<stop[i].d)
{
flag=1;
break;
}
}
q.push(stop[i].p);
}
if(flag) puts("-1");
else
printf("%d\n",cnt);
return 0;
}
POJ 2431 Expedition (贪心+优先队列)的更多相关文章
- 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 ...
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
- POJ 2431 Expedition(优先队列、贪心)
题目链接: 传送门 Expedition Time Limit: 1000MS Memory Limit: 65536K 题目描述 驾驶一辆卡车行驶L单位距离.最开始有P单位的汽油.卡车每开1 ...
- poj - 2431 Expedition (优先队列)
http://poj.org/problem?id=2431 你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇, ...
- poj 2431 Expedition 贪心
简单的说说思路,如果一开始能够去到目的地那么当然不需要加油,否则肯定选择能够够着的油量最大的加油站加油,,不断重复这个贪心的策略即可. #include <iostream> #inclu ...
- POJ 2431 Expedition 贪心 优先级队列
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30702 Accepted: 8457 Descr ...
- 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 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...
随机推荐
- OSChina底层数据库操作的类(QueryHelper)源代码
OSChina 使用的是 dbutils 这个JDBC的封装类库来进行数据库操作. 而 QueryHelper 则是在 dbutils 的基础上进行一级简单的封装,提供一些经常使用的数据库操作方法和对 ...
- 使用Xamarin在Visual Studio中开发Android应用
原文:使用Xamarin在Visual Studio中开发Android应用 本文使用的环境是Windows 8 Visual Studio 2012.2 1.下载Xamarin http://xam ...
- 程序设计实践C++ 程序代写(QQ 928900200)
程序设计实践 采用C++作为编程语言. 设计开发一个“学生信息”管理系统.该系统模拟数据库管理系统(DBMS)的功能,为用户提供数据存储.查找的能力. 该系统存储的学生信息包括: 学号.姓名.性别.语 ...
- Embedding Lua, in Scala, using Java(转)
LuaJ I was reading over the list of features that CurioDB lacks compared to Redis , that I’d previou ...
- 牟大哥:《App自我促销》连载2
直立人迁移走
[谋哥每天一干货,第六十九篇] 前篇说到声音在远古时代.是一个奇妙的东西,它可以非常快地把信息传播到其它地方,突破了短距离. 然而能人的后代直立人学会了直立行走,他们開始走出非洲,到达遥远的中东.中国 ...
- C. Captain Marmot (Codeforces Round #271)
C. Captain Marmot time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Java获取的一天、本星期、这个月、本季度、一年等 开始和结束时间
1.代码 package com.zhaochao.utils; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...
- 【转】d3d的投影矩阵推导
原帖地址:http://blog.csdn.net/popy007/article/details/4091967 上一篇文章中我们讨论了透视投影变换的原理,分析了OpenGL所使用的透视投影矩阵的生 ...
- 浅谈web网站架构演变过程(转)
前言 我们以javaweb为例,来搭建一个简单的电商系统,看看这个系统可以如何一步步演变. 该系统具备的功能: 用户模块:用户注册和管理 商品模块:商品展示和管理 交易模块:创建交易和管理 阶 ...
- 用正交多项式作最小二乘拟合的java实现(转)
import java.util.Scanner; public class Least_square_fit { public static double Least_square_method(i ...