Description
在宇宙中存在着一种神秘的暗物质魔法球。这种魔法球分为阴球和阳球,当两种球合并时就会释放出巨大的能量。 每个球都有一个魔法值。现在给定你n对魔法球,请你找出所有的组合中魔法值最大的n种。 ( 组合意思是指一个阴球和一个阳球结合,魔法值为两球之和,每个魔法球可以使用多次。)
Input
单组输入。 第一行为两个整数n(1<=n<=1e5) 第二行为n个整数,代表n个阳球的魔法值,每个整数值范围(1-1e7)。 第三行为n个整数,代表n个阴球的魔法值,每个整数值范围(1-1e7)。
Output
输出1行,为n个由空格分割的整数,即为最后所求答案,最后一个整数后无空格。
Sample
Input
5
1 2 5 3 4
1 2 3 4 5
Output
10 9 9 8 8
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
const int N = 1e6 + 10;
using namespace std;
struct node
{
int a,b,num;
bool operator<(const node other)const
{
return num<other.num;
}
};
int cmp(int x,int y)
{
return x>y;
}
priority_queue<node>ans;
int a[N];
int b[N];
int main()
{
ios::sync_with_stdio(false);
int t;
node temp;
cin>>t;
for(int i=0; i<t; i++)
cin>>a[i];
for(int i=0; i<t; i++)
cin>>b[i];
sort(b,b+t,cmp);
sort(a,a+t,cmp);
for(int i=0; i<t; i++)
{
temp.a=i;
temp.b=0;
temp.num=a[i]+b[0];
ans.push(temp);
}
for(int i=0; i<t; i++)
{
temp=ans.top();
ans.pop();
if(i!=t-1)
cout<<temp.num<<" ";
else
cout<<temp.num<<endl;
if(temp.b!=t-1)
{
temp.b++;
temp.num=a[temp.a]+b[temp.b];
ans.push(temp);
}
}
return 0;
}