LUOGUP1631---序列合并
来源:互联网 发布:js解析json数据object 编辑:程序博客网 时间:2024/06/04 19:13
#include<cstdio>#include<iostream>#include<cstring>using namespace std;int n;int hp[1000005];int z[1000000];int cnt,acnt=1,bcnt=1;int a[100005],b[100005];int mx;void pus(int x) {//建立大根堆(一直想的建个小根堆后来发现大根堆好用。。。) int now=++cnt; hp[now]=x; while(now>1) { int tp=now/2; if(hp[now]>hp[tp]) { swap(hp[now],hp[tp]); now=tp; } else break; }}int del() {//维护堆 int res=hp[1]; hp[1]=hp[cnt]; cnt--; int now=1; while(now*2<=cnt) { int tp=now*2; if(hp[tp+1]>hp[tp]&&tp<cnt)tp++; if(hp[tp]>hp[now])swap(hp[tp],hp[now]); else break; now=tp; } return res;}int main() { cin>>n; for( int i=1; i<=n; i++) { scanf("%d",&a[i]); } for(int i=1; i<=n; i++) scanf("%d",&b[i]);// memset(hp,0x7f,sizeof hp);//没有必要memset hp[1]=214512312; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { int r=a[i]+b[j]; if(r<hp[1]||cnt<n) { pus(r); if(cnt>n)del(); if(cnt==n&&a[i]+b[j+1]>=hp[1])break;//如果b的下一个+a现在的仍大于大根堆的第一个,就没必要继续进行下去了 } }if(cnt==n&&a[i+1]+b[1]>=hp[1])break;//同理 } int ans[100005]; for(int i=1; i<=n; i++) ans[i]=del(); for(int i=n;i>=1;i--)printf("%d ",ans[i]);}
一道堆排,然后主要是加了个好像是剪枝的东西。
然后先建起来一个大根堆,来判断是否能入堆。就这样。
阅读全文
0 0
- LUOGUP1631---序列合并
- 刷题记录-luoguP1631 序列合并
- 序列合并
- 合并序列
- 合并两个有序序列
- 合并两个有序序列
- 合并两个有序序列
- 合并两个排序序列
- 整数序列合并问题
- 合并(序列)流
- 洛谷 P1631 序列合并
- 洛谷P1631 序列合并
- 洛谷P1631 序列合并
- 洛谷 P1631 序列合并
- 合并两个有序序列
- 洛谷 P1631 序列合并
- 洛谷P1631 序列合并
- 洛谷 P1631 序列合并
- HTTP方法
- Long类型转String JS丢失
- netty获取ip
- Maven学习-目录结构
- MD5加密
- LUOGUP1631---序列合并
- 如何从Linux命令行截屏
- plc里面的常用符号,流程,pid计算基本都搞清楚了。
- 数据在内存中的存储,原,反,补,内存方式
- MVP+登陆注册RecyclerView显示数据
- 【一句话解释一个技术点】ManualResetEvent
- webrtc中rtcp反馈与码率控制模块分析
- over partition by与group by 的区别
- 454. 4Sum II