题目传送门

用优先队列瞎搞... 想着在每个地方 先算上一个点到这一个点要花费多少钱 这个用小根堆算就好 然后在这个地方加油 把油钱比自己多的替代掉 这个用大根堆维护一下 然后两个堆之间信息要保持互通 这个有点麻烦 我用的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 ;
}

最新文章

  1. 统计Go, Go, Go
  2. UIscrollView和UIPageControl的循环滚动
  3. C# 文件和文件夹操作
  4. 关于路由器自定义 3322.org 的DDNS
  5. C++实现简单的内存池
  6. 3243: [Noi2013]向量内积 - BZOJ
  7. UVa 1645 (递推) Count
  8. ###STL学习--vector
  9. Mac下持久化路由配置
  10. linux 管理权限
  11. mvc4.0 @Styles.Render(转)
  12. css3新属性box-orient
  13. 解决mysql中文乱码问题?
  14. D3D9 effect (hlsl)(转)
  15. 彻底搞懂字符编码(unicode,mbcs,utf-8,utf-16,utf-32,big endian,little endian...)[转]
  16. selenium学习网址
  17. WampServer的配置
  18. Mysql自动设置时间(自动获取时间,填充时间)
  19. Angular 2 升级到 Angular 5
  20. 使用nginx对spring boot项目进行代理

热门文章

  1. luogu4238 【模板】多项式求逆
  2. 网页设计简史看设计&amp;代码“隔膜”
  3. Django笔记 —— 模板高级进阶
  4. 接口测试工具postman(二)创建新项目
  5. BZOJ 3569 DZY Loves Chinese II 树上差分+线性基
  6. 修改freemarker的ftl时,不重启tomcat的办法(使用了springMVC)
  7. Alpha冲刺(7/10
  8. elasticsearch 6.2.3安装ik分词
  9. ui-grid下拉过滤
  10. java生成和解析二维码