UVa11997 K Smallest Sums

 题目:

K Smallest Sums

You're given k arrays, each array has k integers. There are kk ways to pick exactly one element in each array and calculate the sum of the integers. Your task is to find the k smallest sums among them.

Input

There will be several test cases. The first line of each case contains an integer k (2<=k<=750). Each of the following k lines contains k positive integers in each array. Each of these integers does not exceed 1,000,000. The input is terminated by end-of-file (EOF). The size of input file does not exceed 5MB.

Output

For each test case, print the k smallest sums, in ascending order.

Sample Input

3
1 8 5
9 2 5
10 7 6
2
1 1
1 2

Output for the Sample Input

9 10 12
2 2 ------------------------------------------------------------------------------------------------------------------------------------------------------------------

思路:

 先思考2个有序表(sort后)AB合成的情况。组织如下

【 表】1: A1+B1 <=  A1+B2 <=  A1+B3 <=......

【表】2:A2+B1 <=  A2+B2 <=  A2+B3 <=......

......

【表】n:An+B1 <=  An+B2 <=  An+B3 <=......

转化为多路归并问题且有总数限制为k,因此将n个【表】合并为一个有序【表】C且表中数据数目为k。优先队列处理:初始化优先队列为n个表的第一元素,每次在队列中取出最小元素加入C并将该最小元素在【表】中的后一个元素入队。共取k个元素。

对于k个有序表的情况:两两合并。

于是有代码如下:

 #include<cstdio>
#include<algorithm>
#include<queue>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; const int maxn = + ; struct Node{
int s,b;
bool operator <(const Node& rhs) const{ //相反定义 <
return s>rhs.s;
}
}; void merge(int* A,int* B,int*C,int n){ //合并AB数组取其前n小的和放入C
priority_queue<Node> Q;
FOR(i,,n) Q.push(Node{A[i]+B[],});
FOR(i,,n){
Node u=Q.top();Q.pop();
C[i]=u.s;
int b=u.b;
if(b+ < n) Q.push(Node{u.s-B[b]+B[b+],b+}); //加入A[a][b+1]
}
} int main(){
int n;
int A[maxn],B[maxn]; while(scanf("%d",&n)==){
FOR(j,,n) scanf("%d",&A[j]); sort(A,A+n);
FOR(i,,n){
FOR(j,,n) scanf("%d",&B[j]);
sort(B,B+n);
merge(A,B,A,n);
}
printf("%d",A[]);
FOR(i,,n)
printf(" %d",A[i]);
printf("\n");
}
return ;
}

【暑假】[实用数据结构]UVa11997 K Smallest Sums的更多相关文章

  1. UVA-11997 K Smallest Sums

    UVA - 11997 K Smallest Sums Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & ...

  2. uva11997 K Smallest Sums&&UVALive 3135 Argus(优先队列,多路归并)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  3. UVA11997 K Smallest Sums

    思路 经典的k路归并问题 问题先转换为2路的有序表归并 先让A[1~k]都和B[1]相加,然后加入堆中,取出堆顶(A[x]+B[y])之后,再放入A[x]+B[y+1] 代码 #include < ...

  4. 题解——UVA11997 K Smallest Sums

    题面 背景 输入 输出 翻译(渣自翻) 给定K个包含K个数字的表,要求将其能产生的\( k^{k} \)个值中最小的K个输出出来 题解 k路归并问题的经典问题 可以转化为二路归并问题求解 考虑A[], ...

  5. D - K Smallest Sums(多路归并+贪心)

    Problem K K Smallest Sums You're given k arrays, each array has k integers. There are kk ways to pic ...

  6. 11997 - K Smallest Sums(优先队列)

    11997 - K Smallest Sums You’re given k arrays, each array has k integers. There are kk ways to pick ...

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

    UVA - 11997 id=18702" target="_blank" style="color:blue; text-decoration:none&qu ...

  8. 【优先队列】【UVa11997】K Smallest Sums

    传送门 Description Input Output Translation · 给定k个长度为k的数组,把每个数组选一个元素加起来,这样共有kk种可能的答案,求最小的k个 Sample Inpu ...

  9. K Smallest Sums

    uva11997:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...

随机推荐

  1. MySQL的基本命令

    MySQL的基本命令 启动:net start mySql; 进入:mysql -u root -p/mysql -h localhost -u root -p databaseName; 列出数据库 ...

  2. PAT-乙级-1030. 完美数列(25)

    1030. 完美数列(25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 给定一个正整数数列,和正整数p,设这 ...

  3. win8 优化笔记

    win8可以关掉的服务: 以下是小编搜集的可以安全更改为手动启动的服务(按名称排序): Application Experience(启动时为程序处理应用程序兼容性缓存请求) ·Computer Br ...

  4. JavaScript 踩坑心得— 为了高速(下)

    一.前言 本文的上一篇 JavaScript 踩坑心得- 为了高速(上) 主要和大家分享的是 JavaScript 使用过程中的基本原则以及编写过程中的心得分享,本文主要和大家聊聊在各个使用场景下的 ...

  5. 一个只需要点 「下一步」就完成监控 Windows

    Cloud Insight 此前已然支持 Linux 操作系统,支持20多中数据库中间件等组件,多种操作,多种搭配,服务器监控玩的其乐无穷啊!但想想还有许多 Windows 的小伙伴没有体验过,所以在 ...

  6. Android 二维码扫描与生成

    由于源代码比较多,本文不进行讲述,请下载源码. 源码来源于网络,请点击这里下载: http://files.cnblogs.com/wuyou/Android%E4%BA%8C%E7%BB%B4%E7 ...

  7. 服务器部署_linux下部署jprofiler简单备忘

    1.windows安装jprofiler 2.linux下安装jprofiler服务端,记好安装路径.假设是安装在/ex/bin/下 3. 配置tomcat的启动sh文件,在后面加入以下参数:  -a ...

  8. 【无聊放个模板系列】POJ 1274 (匈牙利)

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...

  9. JVM内存状况查看方法和分析工具

    Java本身提供了多种丰富的方法和工具来帮助开发人员查看和分析GC及JVM内存的状况,同时开源界和商业界也有一些工具可用于查看.分析GC及JVM内存的状况.通过这些分析,可以排查程序中内存泄露的问题及 ...

  10. nigix以及相关

    nginx+php的配置 php与nginx整合 http://www.thinkphp.cn/topic/13082.html [入门篇]Nginx + FastCGI 程序(C/C++) 搭建高性 ...