【暑假】[实用数据结构]UVa11997 K Smallest Sums
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的更多相关文章
- UVA-11997 K Smallest Sums
UVA - 11997 K Smallest Sums Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- uva11997 K Smallest Sums&&UVALive 3135 Argus(优先队列,多路归并)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- UVA11997 K Smallest Sums
思路 经典的k路归并问题 问题先转换为2路的有序表归并 先让A[1~k]都和B[1]相加,然后加入堆中,取出堆顶(A[x]+B[y])之后,再放入A[x]+B[y+1] 代码 #include < ...
- 题解——UVA11997 K Smallest Sums
题面 背景 输入 输出 翻译(渣自翻) 给定K个包含K个数字的表,要求将其能产生的\( k^{k} \)个值中最小的K个输出出来 题解 k路归并问题的经典问题 可以转化为二路归并问题求解 考虑A[], ...
- 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 ...
- 11997 - K Smallest Sums(优先队列)
11997 - K Smallest Sums You’re given k arrays, each array has k integers. There are kk ways to pick ...
- UVa 11997 K Smallest Sums 优先队列&&打有序表&&归并
UVA - 11997 id=18702" target="_blank" style="color:blue; text-decoration:none&qu ...
- 【优先队列】【UVa11997】K Smallest Sums
传送门 Description Input Output Translation · 给定k个长度为k的数组,把每个数组选一个元素加起来,这样共有kk种可能的答案,求最小的k个 Sample Inpu ...
- K Smallest Sums
uva11997:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
随机推荐
- 如何将word中上下两行文字对齐?
一.问题来源及描述 本科毕设的时候积累的问题,整理如下. 红头文件下面的署名,上下要对齐. 二.解决办法 经验证,第一次拉标尺要把标尺放在第一行的光标处,为了换行后,再次enter,tab后到与上一行 ...
- Windows2003/2008/2008 R2下易语言点支持库配置就退出的问题
问题: 请问一个问题,我的电脑上win2003系统的,安装了易语言后,一点支持库配置就会自动退出.这是为什么啊? 解决方法如下: 删除 lib下的wmp.npk,重新打开易语言就可以了.
- VS2005 VS2008 Manifest 配置问题总结
一.问题 编译某个遗留工程后,运行程序时报错,“由于应用程序的配置不正确,应用程序无法启动.重新安装应用程序可能会解决这个问题.” 查看生成的Manifest文件如下: <?xml versio ...
- WAF SSI
http://www.2cto.com/Article/201405/299154.html
- 217. Contains Duplicate
题目: Given an array of integers, find if the array contains any duplicates. Your function should retu ...
- Cobalt Strike
http://www.77169.com/hack/201512/222080.shtm
- A9裸机
Tiny4412裸机程序之操作ICache 一.首先普及一下什么是Cache 基于程序访问的局限性,在主存和CPU通用寄存器之前设置了一类高速的.容量较小的存储器,把正在执行的指令地址附件的一部分指令 ...
- 【HDOJ】4412 Sky Soldiers
1. 题目描述有$k$个伞兵跳伞,有$m$个汇点.当伞兵着陆后,需要走向离他最近的汇点.如何选择这$m$个结点,可以使得士兵最终行走的距离的期望最小.求这个最小的期望. 2. 基本思路假设已经选好了这 ...
- Complete The Pattern #1
Complete The Pattern #1 Task: You have to write a function pattern which creates the following patte ...
- 如何在 OS X Yosemite 中安装 Java
如果你的 Mac 纯净的安装了 OS X Yosemite 的话,其中是不会包含 Java 的,如果你的 Mac 需要安装 Java 环境的话,可以通过下面介绍的两种方法来实现.通过手动安装最新版 J ...