给出两个包含 nn 个整数的数组 AA,BB。分别在 AA, BB 中任意出一个数并且相加,可以得到 n^2n2个和。求这些和中最小的 nn 个。

输入格式
输入第一行一个整数 n(1 \le n \le 50000)n(1≤n≤50000)。

接下来一行输入数组 AA,用空格隔开。

接下来一行输入数组 BB,用空格隔开。

1 \le A_i, B_i \le 10^91≤Ai​,Bi​≤109

输出格式
从小到大输出最小的 nn 个和,用空格隔开。

样例输入
4
1 3 5 7
2 4 6 8
样例输出
3 5 5 7

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int n,a[],b[];
struct node{
int y,num;
bool operator <(const node &v)const
{
return num>v.num;
}
};
node k;
priority_queue<node>q;
int main()
{
cin>>n;
for(int i=;i<=n;i++)
cin>>a[i];
for(int i=;i<=n;i++)
cin>>b[i];
sort(a+,a+n+);
sort(b+,b+n+);
for(int i=;i<=n;i++)
{
k.y=;
k.num=a[i]+b[];
q.push(k);
}
int s=;
while(s<=n)
{
node now=q.top();
q.pop();
if(now.y+<=n)
{
k.y=now.y+;
k.num=now.num-b[now.y]+b[now.y+];
q.push(k);
}s++;
cout<<now.num<<" ";
}
return ;
} //AC
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int a[],b[],c[];int n;;
struct data{
int aa,bb,c;
}o[],z;
bool operator<(data x,data y)
{ return x.c>y.c;
}
priority_queue<data> q;
int main()
{
cin>>n;int l1=,l2=;
for (int i=;i<=n;++i)
{ cin>>a[i];
}
sort(a+,a++n);
for (int i=;i<=n;++i)
{ cin>>b[i];
}
sort(b+,b++n);
for (int i=;i<=n;++i)
{ o[i].aa=;
o[i].bb=i;
o[i].c=a[o[i].aa]+b[o[i].bb];
q.push(o[i]);
}
for (int i=;i<=n;++i)
{
z=q.top();
q.pop();
cout<<z.c;
if(i!=n)cout<<" ";
z.aa++;
z.c=a[z.aa]+b[z.bb];
q.push(z);
}
}

最小n个和(优先队列)的更多相关文章

  1. 剑指 Offer 40. 最小的k个数 + 优先队列 + 堆 + 快速排序

    剑指 Offer 40. 最小的k个数 Offer_40 题目描述 解法一:排序后取前k个数 /** * 题目描述:输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7. ...

  2. HDU 1285 确定比赛名次【字典序最小的拓扑排序 + 优先队列】

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  3. UVALive 7274 Canvas Painting (优先队列)

    Canvas Painting 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/C Description http://7xjo ...

  4. poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra

    http://poj.org/problem?id=3013 Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total S ...

  5. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  6. hdu1242 Rescue(BFS +优先队列 or BFS )

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意:     Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中.迷宫的长.宽不超 ...

  7. 【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)

    Catch That Cow Descriptions: Farmer John has been informed of the location of a fugitive cow and wan ...

  8. 优先队列与TopK

    一.简介 前文介绍了<最大堆>的实现,本章节在最大堆的基础上实现一个简单的优先队列.优先队列的实现本身没什么难度,所以本文我们从优先队列的场景出发介绍topK问题. 后面会持续更新数据结构 ...

  9. Priority Queue(优先队列)

    今天早上起来完成了一个完整的基于二叉堆实现的优先队列,其中包含最小优先和最大优先队列. 上篇说了优先队列的特性,通过建堆和堆排序操作,我们就已经看到了这种数据结构中的数据具有某种优先级别,要么非根节点 ...

  10. Algorithms - Priority Queue - 优先队列

    Priority queue - 优先队列 相关概念 Priority queue优先队列是一种用来维护由一组元素构成的集合S的数据结构, 其中的每一种元素都有一个相关的值,称为关键字(key). 一 ...

随机推荐

  1. Python占位符使用总结

    格式化对象为字符串:%s myName=input('Enter your name:') userAge=input('Enter your age:') userHight=input('Ente ...

  2. ASP .NET依赖注入理解

    ASP .NET依赖注入理解[转]:  https://www.cnblogs.com/wzk153/p/10892444.html

  3. Lua的栈及基本栈操作

    Lua的栈及基本栈操作 https://blog.csdn.net/mydriverc2/article/details/51134737 https://blog.csdn.net/mydriver ...

  4. JS基础_变量的声明提前、函数的声明提前

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. [NOIP10.5模拟赛]3.c题解--思维

    题目链接 这次不咕了 https://www.luogu.org/problemnew/show/AT2389 闲扯 考场20分爆搜走人 \cy 话说这几天T3都很考验思维啊 分析 我们先钦定一只鸡( ...

  6. 解决npm ERR!Unexpected end of JSON input while paring near (解析附近时JSON输入意外结束)'...."^2.0.0-rc.0","glob"'等npm install错误

    摘要 最近更新了一次node,但是更新后npm的命令总是会报 npm WARN deprecated fsevents@2.0.6: Please update: there are crash fi ...

  7. css 对div用hover设置border,出现抖动和div走位问题,解决方法

    样式设置 : div:hover { border:1px solid red;} 当鼠标移动到div时,产生抖动和偏移. 产生的原因: 是因为设置border时设置了1px边框,多出的这1px,与其 ...

  8. C# 填充无效,无法被移除

    1.本文采用微软的 RijndaelManaged 命名空间: System.Security.Cryptography Assemblies: mscorlib.dll, netstandard.d ...

  9. 【Distributed】限流技巧

    一.概述 1.1 高并发服务限流特技 1.2 为什么要互联网项目要限流 1.3 高并发限流解决方案 二.限流算法 2.1 计数器 2.2 滑动窗口计数 2.3 令牌桶算法 使用RateLimiter实 ...

  10. 小程序Flex布局

    容器属性 容器支持的属性有:display:通过设置display属性,指定元素是否为Flex布局.flex-direction:指定主轴方向,决定了项目的排列方式.flex-wrap:排列换行设置. ...