Luogu P1631 序列合并
题目
开一个堆,先把所有\(a[i]+b[1]\)压进优先队列。
然后每次把最小的取出来,把对应的\(a[i]\)的下一个\(b[j]\)拿出来加进去。
#include<bits/stdc++.h>
using namespace std;
namespace IO
{
char ibuf[(1<<21)+1],obuf[(1<<21)+1],st[15],*iS,*iT,*oS=obuf,*oT=obuf+(1<<21);
inline char Get() { return (iS==iT? (iT=(iS=ibuf)+fread(ibuf,1,(1<<21)+1,stdin),(iS==iT? EOF:*iS++)):*iS++); }
inline void Flush() { fwrite(obuf,1,oS-obuf,stdout),oS=obuf; }
inline void Put(register char x) { *oS++=x; if(oS==oT) Flush(); }
inline int read(){register int x=0,f=0;register char ch=Get();while((ch>57||ch<48)&&ch^'-')ch=Get();if(ch=='-')ch=Get(),f=1;while(ch>=48&&ch<=57)x=x*10+(ch^48),ch=Get();return f? -x:x;}
inline void write(register int x) { register int top=0; if(x<0)Put('-'),x=-x; if(!x) Put('0'); while(x) st[++top]=(x%10)+48,x/=10; while(top) Put(st[top--]); Put(' '); }
}
using namespace IO;
const int N=100007;
int a[N],b[N],c[N];
priority_queue<pair<int,int>,vector<pair<int,int> >, greater<pair<int,int> > >q;
int main()
{
register int i,n=read();
for(i=1;i<=n;++i) a[i]=read();
for(i=1;i<=n;++i) b[i]=read(),c[i]=1,q.push(pair<int,int>(a[1]+b[i],i));
while(n--) write(q.top().first),i=q.top().second,q.pop(),q.push(pair<int,int>(a[++c[i]]+b[i],i));
return Flush(),0;
}
Luogu P1631 序列合并的更多相关文章
- 洛谷P1631 序列合并
P1631 序列合并 236通过 657提交 题目提供者xmyzwls 标签堆 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 为什么不行? 题目描述 有两个长度都是N的序列A和B,在A和B中 ...
- P1631 序列合并
P1631 序列合并 有两个长度都是N的序列A和B,在A和B中各取一个数相加可以得到N^2N2个和,求这N^2N2个和中最小的N个. 对于100%的数据中,满足1<=N<=100000. ...
- 洛谷 - P1631 - 序列合并 - 堆
https://www.luogu.org/problemnew/show/P1631 序列a中每个数首先都和序列b中的最小元素配对(虽然好像不是很必要这么早插进来?) 每次从堆顶取出最小的和输出答案 ...
- P1631 序列合并 洛谷
https://www.luogu.org/problem/show?pid=1631 题目描述 有两个长度都是N的序列A和B,在A和B中各取一个数相加可以得到N^2个和,求这N^2个和中最小的N个. ...
- 洛谷 P1631 序列合并
题意简述 有两个长度都是N的序列A和B,在A和B中各取一个数相加可以得到N^2个和,求这N^2个和中最小的N个. 题解思路 大根堆,先存入n个和,再比较大小,改变堆中元素. 代码 #include & ...
- luogu 1631 序列合并
priority_queue的使用,注意 a[1]+b[1],a[1]+b[2],a[1]+b[3],a[1]+b[4].......a[1]+b[n] a[2]+b[1]......... .. a ...
- 洛谷 P1631 序列合并(优先队列)
传送门 解题思路 首先读入a.b数组后,sort一遍(从小到大),然后把a[1]+b[1],a[2]+b[1],a[3]+b[1]……a[n]+b[1]全部加入一个优先队列q(小根堆). 然后从一到n ...
- 【洛谷】P1631: 序列合并
P1631 序列合并 题目描述 有两个长度都是N的序列A和B,在A和B中各取一个数相加可以得到N2个和,求这N2个和中最小的N个. 输入输出格式 输入格式: 第一行一个正整数N: 第二行N个整数Ai ...
- 【Luogu P1631】序列合并
Luogu P1631 题意很好懂,不作分析 很容易想出一个解法是求出每一个和,排序后取前n个. 当然这种做法妥妥的会MLE+TLE 我们会发现实质上这种做法的缺点在于存入了大量不需要的数据. 那么该 ...
随机推荐
- Linux Expect自动交互脚本
https://likegeeks.com/expect-command/ In the previous post, we talked about writing practical shell ...
- ORACLE/SQL用函数进行每年,每月,每周,每日的数据汇总
15/03/21 用函数进行每年,每月,每周,每日的数据汇总 假设一个销售明细表 sale_detail 含有 国家(country),销售时间(sale_time),销售额(sale_money) ...
- BZOJ 3173: [Tjoi2013]最长上升子序列 Splay
一眼切~ 重点是按照 $1$~$n$ 的顺序插入每一个数,这样的话就简单了. #include <cstdio> #include <algorithm> #define N ...
- Fiona简介
Fiona是一个python地理空间处理库,类似于OGR
- H5 video全屏与取消全屏兼容
H5 video全屏与取消全屏各浏览器兼容, requestFullscreen()全屏方法,exitFullscreen()退出全屏方法.兼容各个浏览器与css3兼容一样加个前缀即可. // 全屏 ...
- akka 的集群访问方式
akka 中采用startProxy分区代理 访问 ,跟使用shardRegion 来访问的区别 这两种访问方式是不是重了呢. 而另外这是一个单例代理 private fun startUniver ...
- typescript 第二部分
ts 中不允许变量同名 函数定义的两种方式 第一种:命名函数 = 声明式定义 function fn(){} 第二种:匿名函数 var fn = function(){} function fn(x: ...
- Junit : how to add listener, and how to extends RunListener to override behaviors while failed
http://junit.sourceforge.net/javadoc/org/junit/runner/notification/RunListener.html org.junit.runner ...
- 算法 - 剪枝游戏 - Green Hackenbush
场景:给颗树,轮流剪掉一条枝,没枝可剪的人输. 题目:Deforestation | HackerRank 讲解:Games!: Green Hackenbush 哎,差点自己想出来答案,最后还是看了 ...
- chrome浏览器爬虫WebDriverException解决采用python + selenium + chrome + headless模式
WebDriverException: Message: unknown error: Chrome failed to start: crashed 第一种:如果出现下面情况: chrome浏览器有 ...