UVA - 11997:K Smallest Sums

Posted white_hat_hacker

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA - 11997:K Smallest Sums相关的知识,希望对你有一定的参考价值。

多路归并

#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=0,int p2=0){
        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=1;i<=n;i++){
        q.push(Node(ans[i]+a[1],1));
    }
    for(int i=1;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+1],x.y+1));
        }
    }
}
void solve(){
    for(int i=1;i<=n;i++){
        scanf("%d",&ans[i]);
    }
    sort(ans+1,ans+n+1);
    for(int i=2;i<=n;i++){
        for(int j=1;j<=n;j++){
            scanf("%d",&a[j]);
        }    
        sort(a+1,a+n+1);
        Merge();
    }    
    for(int i=1;i<=n;i++){
        printf("%d",ans[i]);
        if(i<n)printf(" ");
    }
    printf("\n");
}
int main()
{
    while(~scanf("%d",&n)){
        solve();
    }
    return 0;
}

 

以上是关于UVA - 11997:K Smallest Sums的主要内容,如果未能解决你的问题,请参考以下文章

UVA 11997 K Smallest Sums 优先队列 多路合并

UVA11997 K Smallest Sums

UVA 11997 K Smallest Sums 优先队列+归并 STL

优先队列UVa11997K Smallest Sums

UVa 11997 K Smallest Sums 优先队列&&打有序表&&归并

UVA.11997- K Smallest Sums, OJ4TH.368 - Magry's Sum I