优先队列(挑程)poj 2431
每次写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的更多相关文章
- POJ 2431 优先队列
汽车每过一单位消耗一单位油,其中有给定加油站可加油,问到达终点加油的最小次数. 做法很多的题,其中优先对列解这题是很经典的想法,枚举每个加油站,判断下当前油量是否小于0,小于0就在前面挑最大几个直至油 ...
- 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 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...
- poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10025 Accepted: 2918 Descr ...
随机推荐
- vul/0day/shellcode/payload/poc/exp
vul--泛指漏洞 0day--未公开或虽已公开但还没有修复方法的漏洞 shellcode--远程溢出后执行的那段代码 payload--攻击载荷,送到远端机器执行的整段代码 poc--Proof o ...
- vs2015 编译时项目出现NuGet程序包还原失败,找不到xxx.xxx.xxx版本的程序包,怎么解决这个问题?
vs2015 编译时出现这个NuGet程序包还原失败问题,项目还是运行得了,就是每次看到错误列表中有很多个错误,就感觉不舒服. 总算被我找到解决方法了 问题截图:
- linux nginx 添加到全局变量中(环境变量)
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/ /usr/local/bin/就是环境变量目录
- Java Web(五) 监听器Listener
监听器概述 在上一篇里介绍了过滤器Filter,而Listener是Servlet的另一个高级特性.Listener用于监听Java Web程序中的事件,例如创建,修改,删除Session,reque ...
- laravel中的storePublicly对上传的文件设置上传途径
public function imgeUpload(Request $request) { //生成的文件名是md5随机的文件名字 //$path=$request->file('wangEd ...
- Android 音视频深入 三 MP4解码播放视频 (附源码下载)
本篇项目地址,名字是媒体解码MediaCodec,MediaExtractor,求starhttps://github.com/979451341/Audio-and-video-learning-m ...
- os 模块 和 os模块下的path模块
import os # os 主要用于与操作系统进行交互 #获取当前的工作目录 print(os.getcwd()) #切换工作目录 os .chdir("D:\上海python全栈4期\d ...
- WPF技术实现控件截图
1.http://www.cnblogs.com/TianFang/archive/2012/10/07/2714140.html 2.http://www.silverlightchina.net/ ...
- java新随笔
1.纯随机数发生器 Xn+1=(aXn + c)mod m Modulus=231-1=int.MaxValue Multiplier=75=16807 C=0 当显示过2^31-2个数之后,才可能重 ...
- 十三. Python基础(13)--生成器进阶
十三. Python基础(13)--生成器进阶 1 ● send()方法 generator.send(value) Resumes the execution, and "sends&qu ...