UVA - 11997:K Smallest Sums

时间:2022-04-26 21:02:47

多路归并

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<queue>
#define MAXN 755
using namespace std;
int n;
int ans[MAXN];
int a[MAXN];
struct Node{
int Val,y;
Node(int p1=,int p2=){
Val=p1,y=p2;
}
friend bool operator < (const Node &p1,const Node &p2){
return (p1.Val>p2.Val);
}
};
void Merge(){
priority_queue<Node> q;
for(int i=;i<=n;i++){
q.push(Node(ans[i]+a[],));
}
for(int i=;i<=n;i++){
Node x=q.top();q.pop();
ans[i]=x.Val;
if(x.y<n){
q.push(Node(x.Val-a[x.y]+a[x.y+],x.y+));
}
}
}
void solve(){
for(int i=;i<=n;i++){
scanf("%d",&ans[i]);
}
sort(ans+,ans+n+);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&a[j]);
}
sort(a+,a+n+);
Merge();
}
for(int i=;i<=n;i++){
printf("%d",ans[i]);
if(i<n)printf(" ");
}
printf("\n");
}
int main()
{
while(~scanf("%d",&n)){
solve();
}
return ;
}