zoukankan      html  css  js  c++  java
  • UVA

    多路归并

    #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("
    ");
    }
    int main()
    {
        while(~scanf("%d",&n)){
            solve();
        }
        return 0;
    }
  • 相关阅读:
    CodeForces
    hdu4003 树形dp
    hdu2196
    poj2486
    hdu1502 树形dp入门题
    cf 686D
    bzoj2763 分层图
    hdu4424 并查集+贪心+思维
    poj1734 最小环+输出路径
    集训题解1
  • 原文地址:https://www.cnblogs.com/w-h-h/p/7894713.html
Copyright © 2011-2022 走看看