51nod K 汽油补给 大根堆+小根堆....
用优先队列瞎搞... 想着在每个地方 先算上一个点到这一个点要花费多少钱 这个用小根堆算就好 然后在这个地方加油 把油钱比自己多的替代掉 这个用大根堆维护一下 然后两个堆之间信息要保持互通 这个有点麻烦 我用的f数组维护 这样就好了 中间懒得改全部开了long long 不要介意
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
#define LL long long
using namespace std;
const int M=;
LL read(){
LL ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
LL n,k;
struct node1{//小根堆
LL h,w,pos;
bool operator<(const node1&x)const{return w>x.w;}
};
priority_queue<node1>q1;
struct node2{//大根堆
LL h,w,pos;
bool operator<(const node2&x)const{return w<x.w;}
};
priority_queue<node2>q2;
LL old,cnt,tot;
bool f[*M];
int main()
{
LL l,w,now;
n=read(); k=read(); cnt=n;
l=read(); w=read();
q1.push((node1){k,w,}),q2.push((node2){k,w,}); old=l;
for(int i=;i<=n;i++){
l=read(); w=read(); LL h=old;
if(l>k){printf("-1\n"); return ;}
while(old){
node1 x=q1.top(); now=x.h;// printf("[%d %d]\n",x.h,x.w); //system("pause");
if(f[x.pos]){q1.pop(); continue;}
if(now>old) tot=tot+old*x.w,q1.pop(),q1.push((node1){now-old,x.w,++cnt}),q2.push((node2){now-old,x.w,cnt}),f[x.pos]=,old=;
else if(now==old) tot=tot+now*x.w,f[x.pos]=,q1.pop(),old=;
else tot=tot+now*x.w,f[x.pos]=,q1.pop(),old-=now;
}
q2.push((node2){h,w,i}),q1.push((node1){h,w,i});
LL ans=k-old,sum=;
while(ans){
node2 x=q2.top(); now=x.h;
if(f[x.pos]){q2.pop(); continue;}
if(x.w<=w) break;
if(ans>=now) sum+=now,q2.pop(),f[x.pos]=,ans-=now;
else sum+=ans,ans=,q2.pop(),f[x.pos]=,q2.push((node2){now-ans,x.w,++cnt}),q2.push((node2){now-ans,x.w,cnt});
}
if(sum) q2.push((node2){sum,w,++cnt}),q1.push((node1){sum,w,cnt});
old=l;
}
while(old){
node1 x=q1.top(); now=x.h;
if(f[x.pos]){q1.pop(); continue;}
if(now>old) tot=tot+old*x.w,q1.pop(),q1.push((node1){now-old,x.w,++cnt}),q2.push((node2){now-old,x.w,cnt}),f[x.pos]=,old=;
else if(now==old) tot=tot+now*x.w,f[x.pos]=,q1.pop(),old=;
else tot=tot+now*x.w,f[x.pos]=,q1.pop(),old-=now;
}
printf("%lld\n",tot);
return ;
}
51nod K 汽油补给 大根堆+小根堆....的更多相关文章
- [多校联考2019(Round 4 T2)][51nod 1288]汽油补给(ST表+单调栈)
[51nod 1288]汽油补给(ST表+单调栈) 题面 有(N+1)个城市,0是起点N是终点,开车从0 -> 1 - > 2...... -> N,车每走1个单位距离消耗1个单位的 ...
- 洛谷 P3378 【模板】堆(小根堆)
题目描述 如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 输出该小根堆内的最小数 操作3: 3 删除该小根堆内的最小数 输入输出格式 输入格式: ...
- bzoj 1577: [Usaco2009 Feb]庙会捷运Fair Shuttle——小根堆+大根堆+贪心
Description 公交车一共经过N(1<=N<=20000)个站点,从站点1一直驶到站点N.K(1<=K<=50000)群奶牛希望搭乘这辆公交车.第i群牛一共有Mi(1& ...
- CJOJ 2482 【POI2000】促销活动(STL优先队列,大根堆,小根堆)
CJOJ 2482 [POI2000]促销活动(STL优先队列,大根堆,小根堆) Description 促销活动遵守以下规则: 一个消费者 -- 想参加促销活动的消费者,在账单下记下他自己所付的费用 ...
- 优先队列实现 大小根堆 解决top k 问题
摘于:http://my.oschina.net/leejun2005/blog/135085 目录:[ - ] 1.认识 PriorityQueue 2.应用:求 Top K 大/小 的元素 3 ...
- priority_queue()大根堆和小根堆(二叉堆)
#include<iostream> #include <queue> using namespace std; int main() { //对于基础类型 默认是大顶堆 pr ...
- scala写算法-用小根堆解决topK
topK问题是指从大量数据中获取最大(或最小)的k个数,比如从全校学生中寻找成绩最高的500名学生等等. 本问题可采用小根堆解决.思路是先把源数据中的前k个数放入堆中,然后构建堆,使其保持堆序(可以简 ...
- 让priority_queue支持小根堆的几种方法
点击这里了解什么是priority_queue 前言 priority_queue默认是大根堆,也就是大的元素会放在前面 例如 #include<iostream> #include< ...
- HDU 6003 Problem Buyer【小根堆】
任意k个都可以,也可以看做把不行的都选了,再随便选一个可以的要选的数量 把区间和m个值都排序,区间按l一序r二序排,枚举m个值,小根堆维护能帮韩当前枚举值的区间的右端点,这样方便删除区间,然后剩下的就 ...
随机推荐
- ubuntu自带的ibus输入法问题解决方法
ubuntu自带的ibus有点问题,输入字的时候不知道是个什么模式. 在网上搜到一个解决方法. 终端下执行: ibus-daemon -drx 然后切换到拼音输入法,就正常了. 写下作为记录.
- 安装mathtype出问题卸载后 office2016打开mathtype弹错误窗口
解决方法:找到 C:\Program Files (x86)\Microsoft Office\root\Office16\STARTUP目录下的MathType Commands 6 For Wor ...
- 13-Mysql数据库----权限设置
权限管理 我们知道我们的最高权限管理者是root用户,它拥有着最高的权限操作.包括select.update.delete.update.grant等操作.那么一般情况在公司之后DBA工程师会创建一个 ...
- Visual Studio 2005安装包
点击下载
- 安装VMware的一般步骤(安装过程总结)
(安装的是vmwareworkstation11以上) 0.遇到问题为没有权限打开服务“VMAuthdService”时,需要去管理->服务中打开对应的Windows Management In ...
- URAL 1932 The Secret of Identifier(容斥)
Description Davy Jones: You've been captain of the Black Pearl for 13 years. That was our agreement. ...
- DFS(3)——poj1321棋盘问题
一.题目回顾 题目链接:棋盘问题 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于 ...
- MySQL初识2
用python调用mysql的一些方法总结: 1.编码声明: # encoding: UTF-8#!/usr/bin/python 在开头处进行声明,避免出现输入中文时,提示声明错误(当然输入中文出现 ...
- php数据缓存到文件类设计
// 自定义缓存类 class Cache_Filesystem { // 缓存写保存 function set ($key, $data, $ttl) { //打开文件为读/写模式 $h = fop ...
- 基于oracle的sql(结构化查询语言)指令
创建表空间 create tablespace 表空间名 datafile '存储路径(c:\a\a.dbf)' size 200m autoextend on next 10m maxsize un ...