code vs 1245 最小的N个和

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 钻石 Diamond
 
 
 
题目描述 Description

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

输入描述 Input Description

第一行输入一个正整数N;第二行N个整数Ai 且Ai≤10^9;第三行N个整数Bi,
且Bi≤10^9

输出描述 Output Description

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

样例输入 Sample Input

5

1 3 2 4 5 
6 3 4 1 7

样例输出 Sample Output

2 3 4 4 5

数据范围及提示 Data Size & Hint

【数据规模】 对于 100%的数据,满足 1≤N≤100000。

思路:优先队列。

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 200001
using namespace std;
int n,num;
int a[MAXN],b[MAXN];
struct nond{
    int x,y;
    bool operator < (nond b) const{
        return x>b.x;
    }
}v;
priority_queue<nond>que;
int main(){
    //freopen("hahaha.in","r",stdin);
    //freopen("hahaha.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i]);
        v.x=b[i]+a[1];v.y=1;
        que.push(v);
    }
    while(num<n){
        nond now=que.top();
        cout<<now.x<<endl;
        que.pop();
        now.x=now.x-a[now.y]+a[now.y+1];
        now.y+=1;
        que.push(now);
        num++;
    }
}

 

posted @ 2017-09-28 11:18  一蓑烟雨任生平  阅读(151)  评论(0编辑  收藏  举报