【暑假】[实用数据结构]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 ...
随机推荐
- Discuz云平台站点信息同步失败,An unknown error occurred. May be DNS Error.
站点信息同步失败 An unknown error occurred. May be DNS Error. (ERRCODE:1) 经过Discuz教程网(http://www.1314study.c ...
- VS2005(vs2008,vs2010)使用map文件查找程序崩溃原因
VS 2005使用map文件查找程序崩溃原因 一般程序崩溃可以通过debug,找到程序在那一行代码崩溃了,最近编一个多线程的程序,都不知道在那发生错误,多线程并发,又不好单行调试,终于找到一个比较好的 ...
- openvas
http://www.freebuf.com/articles/5474.html EPEL http://www.centoscn.com/CentOS/config/2014/0920/3793. ...
- [Ruby on Rails系列]5、专题:Talk About SaSS
This is my presentation in the Rails lecture of Hosei University. I will introduce SaSS by 5 example ...
- IText 生成简单表格(报表)doc文档 单元居中
IText生成doc文档需要三个包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jar 亲测无误,代码如下所示: import com.lowagie ...
- 深入剖析Classloader(二)--根类加载器,扩展类加载器与系统类加载器
原文地址:http://yhjhappy234.blog.163.com/blog/static/31632832201152555245584/?suggestedreading&wumii ...
- 使用JS创建表格以及隔行换色(包括隔N行换色)
<html> <head> <title></title> <style> table{ width:800px; border-colla ...
- javascript数据变量类型判断(JS变量是否是数组,是否是函数的判断)
function isArray(o) { return Object.prototype.toString.apply(o) === “[object Array]”;}function isFun ...
- winform学习日志(十九)----------真正三层架构之登录
摘要:一:三层构架的基础知识在项目开发的过程中,有时把整个项目分为三层架构,其中包括:表示层(UI).业务逻辑层(BLL)和数据访问层(DAL).三层的作用分别如下: 表示层:为用户提供交互操作界面, ...
- vs2015 打不开了 提示"CSharpPackage",未能正确加载xx包
原文:vs2015 打不开了 提示"CSharpPackage" 最近发现vs2015 在新建项目和加载现有项目的时候会报错 提示 开始我以为是系统的问题导致vs 配置除了问题,重 ...