序列合并

题目描述

有两个长度都是N的序列A和B,在A和B中各取一个数相加可以得到N^2个和,求这N^2个和中最小的N个。

 

输入

第一行一个正整数N;

第二行N个整数Ai,满足Ai<=Ai+1且Ai<=10^9;

第三行N个整数Bi, 满足Bi<=Bi+1且Bi<=10^9.

 

输出

仅一行,包含N个整数,从小到大输出这N个最小的和,相邻数字之间用空格隔开。

 

样例输入

3
2 6 6
1 4 8

 

样例输出

3 6 7 

 

提示

 

对于50%的数据中,满足1<=N<=1000;
对于100%的数据中,满足1<=N<=100000。

思维题,运用优先队列可以轻松做出

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int a[100050],b[100050];
struct node
{
    int val;
    int pos;
    friend bool operator < (const node &x,const node &y)
    {
        return x.val>y.val;
    }
};
priority_queue<node>qu;
int main()
{
    //freopen("in.txt","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&b[i]);
    }
    node op;
    for(int i=1;i<=n;i++)
    {
        op.val=a[1]+b[i];
        op.pos=1;
        qu.push(op);
    }
    for(int i=1;i<n;i++)
    {
        op=qu.top();
        qu.pop();
        printf("%d ",op.val);
        op.val+=(a[op.pos+1]-a[op.pos]);
        op.pos++;
        qu.push(op);
    }
    op=qu.top();
    printf("%d\n",op.val);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值