每次写poj的题都很崩溃,貌似从来没有一次一发就ac的,每次都有特别多的细节需要考虑。还有就是自己写的太粗糙了,应该把每种情况都想到的,总是急着交,然后刷一页wa。

优先队列直接用stl就可以,简单实用。

 #include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
using namespace std;
const double Pi=3.14159265358979323846;
typedef long long ll;
const int MAXN=+;
const int dx[]={,,,,-};
const int dy[]={,-,,,};
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll mod=1e9+;
struct node{
int p,d;
}a[MAXN];
bool cmp(node a,node b)
{
return a.d>b.d;
}
int main()
{
int n;scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d",&a[i].d,&a[i].p);
}
int L,P;
scanf("%d%d",&L,&P);
for(int i=;i<=n;i++)
{
a[i].d=L-a[i].d;
}
sort(a+,a+n+,cmp);
/*for(int i=1;i<=n;i++)
{
cout <<a[i].d<<endl;
}*/
int ans=;
priority_queue <int> pque;
if(n==&&P-L<)
{
puts("-1");
return ;
}
for(int i=n;i>=;i--)
{
int dis=a[i].d-a[i+].d; if(dis>P)
{
while(dis>P&&!pque.empty())
{
P+=pque.top();
pque.pop();
ans++;
}
if(dis>P)
{
puts("-1");
return ;
}else
{
P=P-dis;
}
} else
{
P=P-dis;
}
pque.push(a[i].p);
}
int l=L-a[].d;
while(!pque.empty()&&P<l)
{
P+=pque.top();
ans++;
pque.pop();
}
if(P<l) puts("-1");
else printf("%d\n",ans);
return ;
}

优先队列(挑程)poj 2431的更多相关文章

  1. POJ 2431 优先队列

    汽车每过一单位消耗一单位油,其中有给定加油站可加油,问到达终点加油的最小次数. 做法很多的题,其中优先对列解这题是很经典的想法,枚举每个加油站,判断下当前油量是否小于0,小于0就在前面挑最大几个直至油 ...

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

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

  3. poj 2431 【优先队列】

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

  4. POJ 2431 Expedition(探险)

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

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

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

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

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

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

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

  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. CentOS 7使用yum安装SNMP教程

    一.安装SMNP yum install -y net-snmp net-snmp-utils 可以理解为net-snmp是服务端,net-snmp-utils是客户端工具集:如果机器上只需要搭建服务 ...

  2. xshellDoc_1base

    1◆ 到根目录 cd /     2◆ 创建文件夹 mkdir test   3◆ 到目录 cd /root/test/~~   4◆修改文件名 mv test testbak   5◆ 查看 ls ...

  3. Win7系统的虚拟机中安装win7系统

    今天因兼职需要,在家里的win7电脑上安装WIN7虚拟机. 之前在xp和win10系统的虚拟机中,安装各种版本的windows系统都很轻松,这一次居然折腾了很久都没搞定. 下载了好几个系统ios镜像都 ...

  4. ubuntu启用root登陆

    ubuntu系统不能够默认以root用户登陆系统如果你为了方便开发想每次登陆的时候以root用户登陆那么需要手动的做写更改打开终端 首先输入命令 sudo passwd  root更新你的密码然后输入 ...

  5. Win10系列:UWP界面布局进阶8

    StackPanel StackPanel能够以水平或垂直的方式整齐地排列位于其内部的元素.通过设置StackPanel的Orientation属性可以定义内部元素的排列方式,当将Orientatio ...

  6. bzoj1238

    题解: 傻逼模拟题 果断的复制了题解(还没有c++题解?) 代码: program p2509; type arr=array[..] of boolean; var tot:longint; s:a ...

  7. sqlalchemy 模型中添加根据身份证号计算性别和年龄

    class Users(Base, BaseMixin): username = Column(String(24)) id_Num = Column(String(18) ) # 身份证号码 @pr ...

  8. 经典DFS问题实践

    八皇后问题: //八皇后问题 经典的DFS问题实践 #include<iostream> #include<cmath> #include<algorithm> # ...

  9. RabbitMQ 设置消息的TTL(过期时间)

    我们在RabbitMQ中发布消息时,在代码中有两种方法设置某个队列的消息过期时间: 1.针对队列来说,可以使用x-message-ttl参数设置当前队列中所有消息的过期时间,即当前队列中所有的消息过期 ...

  10. 前端之Bootstrap框架

    一.Bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快速开发Web应用程序而设计的一套前端工具包. 它支持响应式布局, ...