• UVA 11997 K Smallest Sums


    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3148

    虽然是水题,需要用到优先队列...但还是考一点思维

    题意是找出n*n的矩阵中,每行中取一个数求sum的前k小值...

    可以很容易想到是求两行中的前k小然后合并...但是求两行中前k小却卡了很久...

    一开始暴力把两行n^2和全部加到优先队列...直接T了...想一想O(n^3logn)确实有点过分...

    实际上优先队列中只需要维护k个值就可以了...

    假设两个有序数列分别为A,B ...我们先将sum{Ai,B0}入队列...

    则任意一个sum{Ai,Bi+1} >= sum{Ai,Bi} 且 sum{Ai,Bi+1}不属于优先队列的集合...

    对于每一个最小匹配对的sum{Ai,Bi},每次加入sum{Ai,Bi+1},就行了...

    这样总的复杂度是O(n^2logn)

    /********************* Template ************************/
    #include <set>
    #include <map>
    #include <list>
    #include <cmath>
    #include <ctime>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <bitset>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cassert>
    #include <cstdlib>
    #inclue <cstring>
    #include <sstream>
    #include <fstream>
    #include <numeric>
    #include <iomanip>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    #define EPS         1e-8
    #define DINF        1e15
    #define MAXN        805
    #define LINF        1LL << 60
    #define MOD         1000000007
    #define INF         0x7fffffff
    #define PI          3.14159265358979323846
    #define lson            l,m,rt<<1
    #define rson            m+1,r,rt<<1|1
    #define BUG             cout<<" BUG! "<<endl;
    #define LINE            cout<<" ------------------ "<<endl;
    #define FIN             freopen("in.txt","r",stdin);
    #define FOUT            freopen("out.txt","w",stdout);
    #define mem(a,b)        memset(a,b,sizeof(a))
    #define FOR(i,a,b)      for(int i = a ; i < b ; i++)
    #define read(a)         scanf("%d",&a)
    #define read2(a,b)      scanf("%d%d",&a,&b)
    #define read3(a,b,c)    scanf("%d%d%d",&a,&b,&c)
    #define write(a)        printf("%d
    ",a)
    #define write2(a,b)     printf("%d %d
    ",a,b)
    #define write3(a,b,c)   printf("%d %d %d
    ",a,b,c)
    #pragma comment         (linker,"/STACK:102400000,102400000")
    template<class T> inline T L(T a)       {return (a << 1);}
    template<class T> inline T R(T a)       {return (a << 1 | 1);}
    template<class T> inline T lowbit(T a)  {return (a & -a);}
    template<class T> inline T Mid(T a,T b) {return ((a + b) >> 1);}
    template<class T> inline T gcd(T a,T b) {return b ? gcd(b,a%b) : a;}
    template<class T> inline T lcm(T a,T b) {return a / gcd(a,b) * b;}
    template<class T> inline T Min(T a,T b) {return a < b ? a : b;}
    template<class T> inline T Max(T a,T b) {return a > b ? a : b;}
    template<class T> inline T Min(T a,T b,T c)     {return min(min(a,b),c);}
    template<class T> inline T Max(T a,T b,T c)     {return max(max(a,b),c);}
    template<class T> inline T Min(T a,T b,T c,T d) {return min(min(a,b),min(c,d));}
    template<class T> inline T Max(T a,T b,T c,T d) {return max(max(a,b),max(c,d));}
    template<class T> inline T exGCD(T a, T b, T &x, T &y){
        if(!b) return x = 1,y = 0,a;
        T res = exGCD(b,a%b,x,y),tmp = x;
        x = y,y = tmp - (a / b) * y;
        return res;
    }
    template<class T> inline T reverse_bits(T x){
        x = (x >> 1 & 0x55555555) | ((x << 1) & 0xaaaaaaaa); x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
        x = (x >> 4 & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0); x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
        x = (x >>16 & 0x0000ffff) | ((x <<16) & 0xffff0000); return x;
    }
    //typedef long long LL;    typedef unsigned long long ULL;
    //typedef __int64 LL;      typedef unsigned __int64 ULL;
    
    /*********************   By  F   *********************/
    int a[MAXN][MAXN];
    int n;
    struct node{
        int val;
        int pos;
        node(int _val = 0,int _pos = 0):val(_val),pos(_pos){};
        bool operator < (const node & a) const{
            return val > a.val;
        }
    };
    int main(){
        //FIN;
        //FOUT;
        while(~scanf("%d",&n)){
            for(int i = 0 ; i < n ; i++){
                for(int j = 0 ; j < n ; j++)
                    scanf("%d",&a[i][j]);
                sort(a[i],a[i]+n);
            }
            for(int i = 1 ; i < n ; i++){
                priority_queue<node> q;
                int cnt = 0;
                for(int j = 0 ; j < n ; j++) q.push(node{a[0][j] + a[i][0],0});
                while(cnt != n){
                    node t = q.top();
                    q.pop();
                    a[0][cnt++] = t.val;
                    q.push(node{t.val + a[i][t.pos+1] - a[i][t.pos],t.pos+1});
                }
            }
            for(int i = 0 ; i < n ; i++)
                printf("%d%c",a[0][i], i != n-1 ? ' ' : '
    ');
        }
        return 0;
    }
  • 相关阅读:
    默认值设置
    关于设置 存储 内部存储空间只显示图片不显示视频的解决方法
    sd卡的监听
    android 设置时间12/24小时制
    详解BMP木马
    C#中类和接口的设计思想(本人认为比较好的思想,欢迎大家讨论指点)
    从XML中读取数据到内存的实例
    如何在代码中通过命令行创建SQL SERVER 数据库
    Visual Studio 2005 新特性 之 可空类型
    install shield11.5 如何制作卸载程序
  • 原文地址:https://www.cnblogs.com/Felix-F/p/3352541.html
Copyright © 2020-2023  润新知