zoukankan      html  css  js  c++  java
  • uva 11997 K Smallest Sums

    解题思路:STL —— priority_queue

      1 ///////////////////////////////////////////////////////////////////////////
      2 //problem_id: uva 11997
      3 //user_id: SCNU20102200088
      4 ///////////////////////////////////////////////////////////////////////////
      5 
      6 #include <algorithm>
      7 #include <iostream>
      8 #include <iterator>
      9 #include <iomanip>
     10 #include <cstring>
     11 #include <cstdlib>
     12 #include <string>
     13 #include <vector>
     14 #include <cstdio>
     15 #include <cctype>
     16 #include <cmath>
     17 #include <queue>
     18 #include <stack>
     19 #include <list>
     20 #include <set>
     21 #include <map>
     22 using namespace std;
     23 
     24 ///////////////////////////////////////////////////////////////////////////
     25 #pragma comment(linker,"/STACK:1024000000,1024000000")
     26 
     27 #define lson l,m,rt<<1
     28 #define rson m+1,r,rt<<1|1
     29 ///////////////////////////////////////////////////////////////////////////
     30 
     31 ///////////////////////////////////////////////////////////////////////////
     32 const double EPS=1e-9;
     33 const double PI=acos(-1.0);
     34 const double E=2.7182818284590452353602874713526;
     35 
     36 const int x4[]={-1,0,1,0};
     37 const int y4[]={0,1,0,-1};
     38 const int x8[]={-1,-1,0,1,1,1,0,-1};
     39 const int y8[]={0,1,1,1,0,-1,-1,-1};
     40 ///////////////////////////////////////////////////////////////////////////
     41 
     42 ///////////////////////////////////////////////////////////////////////////
     43 typedef long long LL;
     44 
     45 typedef int T;
     46 T max(T a,T b){ return a>b? a:b; }
     47 T min(T a,T b){ return a<b? a:b; }
     48 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
     49 T lcm(T a,T b){ return a/gcd(a,b)*b; }
     50 ///////////////////////////////////////////////////////////////////////////
     51 
     52 ///////////////////////////////////////////////////////////////////////////
     53 //Add Code:
     54 int k,a[755][755];
     55 
     56 struct Node{
     57     int x,y;
     58     bool operator <(const Node &a) const{
     59         return x>a.x;
     60     }
     61 };
     62 
     63 void merge(int *x,int *y,int *z){
     64     priority_queue<Node> pq;
     65     int i,j;
     66     for(i=0;i<k;i++){
     67         Node res=Node{y[i]+z[0],0};
     68         pq.push(res);
     69     }
     70     for(i=0;i<k;i++){
     71         Node now=pq.top();
     72         pq.pop();
     73         x[i]=now.x;
     74         j=now.y;
     75         if(j+1<k){
     76             Node res=Node{x[i]-z[j]+z[j+1],j+1};
     77             pq.push(res);
     78         }
     79     }
     80 }
     81 ///////////////////////////////////////////////////////////////////////////
     82 
     83 int main(){
     84     ///////////////////////////////////////////////////////////////////////
     85     //Add Code:
     86     int i,j,ans[755];
     87     while(scanf("%d",&k)!=EOF){
     88         for(i=0;i<k;i++){
     89             for(j=0;j<k;j++) scanf("%d",&a[i][j]);
     90             sort(a[i],a[i]+k);
     91         }
     92         merge(ans,a[0],a[1]);
     93         for(i=2;i<k;i++) merge(ans,ans,a[i]);
     94         printf("%d",ans[0]);
     95         for(i=1;i<k;i++) printf(" %d",ans[i]);
     96         printf("
    ");
     97     }
     98     ///////////////////////////////////////////////////////////////////////
     99     return 0;
    100 }
    101 
    102 ///////////////////////////////////////////////////////////////////////////
    103 /*
    104 Testcase:
    105 Input:
    106 3
    107 1 8 5
    108 9 2 5
    109 10 7 6
    110 2
    111 1 1
    112 1 2
    113 Output:
    114 9 10 12
    115 2 2
    116 */
    117 ///////////////////////////////////////////////////////////////////////////
  • 相关阅读:
    左孩子右兄弟的字典树
    UVA 1401 Remember the Word
    HDOJ 4770 Lights Against Dudely
    UvaLA 3938 "Ray, Pass me the dishes!"
    UVA
    Codeforces 215A A.Sereja and Coat Rack
    Codeforces 215B B.Sereja and Suffixes
    HDU 4788 Hard Disk Drive
    HDU 2095 find your present (2)
    图的连通性问题—学习笔记
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3326637.html
Copyright © 2011-2022 走看看