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. protobuf 向前兼容向后兼容

    http://blog.163.com/jiang_tao_2010/blog/static/12112689020114305013458/ 不错的protobuf.. protobuf的编码方式: ...

  2. SQL 聚集函数使用

    SQL 聚集函数使用 (2009-04-14 15:50:36) 转载▼   总结: 在SQL语句中同时包含where子句,groupby子句,having子句及聚集函数时的执行顺序: 1.按WHER ...

  3. js的原型链

    js中的原型链是一个很重要的概念,理解了原型链,对js程序的开发有很大的好处,废话不说,先上图: javascript是基于原型的语言,所以一个对象可以另一个对象继承.不过javascript实现的时 ...

  4. Scrum中的User Story

    我们通常用User Story来描述Backlog里的各个Backlog项,User Story是从用户的角度对系统的某个功能模块所作的简短描述.一个User Story描述了项目中的一个小功能,以及 ...

  5. [笨木头FireFly 02]入门篇2_客户端发送请求,服务器处理请求

    原地址:http://www.9miao.com/question-15-53940.html 好,经过上一篇不权威的讲解,大家已经能轻易地让客户端和服务端连接起来了. 但是,仅仅是连接了,可它们俩不 ...

  6. pthread_create用法

    linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread. #include <pthread.h> int pthread_create(pth ...

  7. HDU4602+推导公式

    手动列出前5项 可发现规律 /* 推导公式 a[n] = 2^(n-1) + (n-2)*2^(n-3) */ #include<stdio.h> #include<math.h&g ...

  8. Zookeeper + Hadoop + Hbase部署备忘

    网上类似的文章很多,本文只是记录下来备忘.本文分四大步骤: 准备工作.安装zookeeper.安装hadoop.安装hbase,下面分别详细介绍: 一 准备工作 1. 下载 zookeeper.had ...

  9. Servlet课程0424(三) 通过继承HttpServlet来开发Servlet

    //这是第三种开发servlet的方法,通过继承httpservlet package com.tsinghua; import javax.servlet.http.*; import java.i ...

  10. Android安全问题 抢先拦截短信

    同上篇文章一样,这里只陈述结果,代码分析稍后给出 导读:本文叙述如何先于某些伪杀毒软件.病毒.常规软件获取到短信 众所周知,android系统在收到短信息的时候会发送广播,但是此广播是有序广播,也就是 ...