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\n",a)
#define write2(a,b)     printf("%d %d\n",a,b)
#define write3(a,b,c)   printf("%d %d %d\n",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 ? ' ' : '\n');
    }
    return 0;
}

 

posted @ 2013-10-05 16:23  Felix_F  阅读(388)  评论(0编辑  收藏  举报