考虑一个简单的问题,两个长度为n的有序数组A和B,从每个数组中各选出一个数相加,共n2中情况,求最小的n个数。

将这n2个数拆成n个有序表:

A1+B1≤A1+B2≤...

A2+B1≤A2+B2≤...

...

An+B1≤An+B2≤...

然后用优先队列合并成一个有序表即可。队列中需要记录两个数的和s,以及在B中的下标b,

比如Aa+Bb出队以后,应该将Aa+B(b+1) = Aa + Bb - Bb + Bb+1 = s - Bb + bb+1入队

对于书上最后的一个思考问题,merge函数中对A[0]又读又写,会不会出错。答案当然是不会的,因为进入到函数内部你会发现,对A数组其实是先读后写的。

 #include <cstdio>
#include <queue>
#include <algorithm>
using namespace std; const int maxn = + ;
int a[][maxn]; void scan(int& x)
{
char c;
while(c = getchar(), c < '' || c > '');
x = c - '';
while(c = getchar(), c >= '' && c <= '') x = x* + c - '';
} struct Node
{
int sum, b;
Node(int s, int b):sum(s), b(b) {}
bool operator < (const Node& rhs) const
{ return sum > rhs.sum; }
}; void merge(int n, int* A, int* B, int* C)
{
priority_queue<Node> Q;
for(int i = ; i < n; i++) Q.push(Node(A[i]+B[], )); for(int i = ; i < n; i++)
{
Node t = Q.top(); Q.pop();
int b = t.b, sum = t.sum;
C[i] = sum;
if(b + < n) Q.push(Node(sum-B[b]+B[b+], b+));
}
} int main()
{
//freopen("in.txt", "r", stdin); int k;
while(scanf("%d", &k) == )
{
for(int i = ; i < k; i++) scan(a[][i]);
sort(a[], a[] + k);
for(int i = ; i < k; i++)
{
for(int j = ; j < k; j++) scan(a[][j]);
sort(a[], a[] + k);
merge(k, a[], a[], a[]);
} for(int i = ; i < k; i++)
{
if(i) printf(" ");
printf("%d", a[][i]);
}
puts("");
} return ;
}

代码君

UVa 11997 (优先队列 多路归并) K Smallest Sums的更多相关文章

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

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

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

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

  3. 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 ...

  4. 【暑假】[实用数据结构]UVa11997 K Smallest Sums

    UVa11997 K Smallest Sums  题目: K Smallest Sums You're given k arrays, each array has k integers. Ther ...

  5. UVA-11997 K Smallest Sums

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

  6. uva 11997 优先队列

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

  7. uva 11997 K Smallest Sums 优先队列处理多路归并问题

    题意:K个数组每组K个值,每次从一组中选一个,共K^k种,问前K个小的. 思路:优先队列处理多路归并,每个状态含有K个元素.详见刘汝佳算法指南. #include<iostream> #i ...

  8. UVA 11997 K Smallest Sums 优先队列 多路合并

    vjudge 上题目链接:UVA 11997 题意很简单,就是从 k 个数组(每个数组均包含 k 个正整数)中各取出一个整数相加(所以可以得到 kk 个结果),输出前 k 小的和. 这时训练指南上的一 ...

  9. UVA 11997 K Smallest Sums (多路归并)

    从包含k个整数的k个数组中各选一个求和,在所有的和中选最小的k个值. 思路是多路归并,对于两个长度为k的有序表按一定顺序选两个数字组成和,(B表已经有序)会形成n个有序表 A1+B1<=A1+B ...

随机推荐

  1. Django 学习笔记之二 基本命令

    1.新建一个 django project 在Django安装路径下找到django-admin.py文件,我的路径是在C:\Python27\Lib\site-packages\Django-1.1 ...

  2. Makedown常用符号整理

    整理自:http://www.jianshu.com/p/1e402922ee32 不过这里发现博客园的makedown语法支持还不完善,代码语法显示挺有问题的,比较遗憾. 标题 # 一级标题## 二 ...

  3. 文本编辑器 markdown

    http://www.cnblogs.com/youxia/p/linux014.html markdown对数学公式的支持http://www.linuxidc.com/Linux/2014-08/ ...

  4. jquery中判断是否按下回车enter键

    <script>   function sendsubmit()   {   $("#userLoginForm").submit();   return false; ...

  5. 终于明白公测的beta 源自何处了

    A very early version of a software product that may not contain all of the features that are planned ...

  6. Robot framework+python安装使用图解版

    一.安装包 1.Python2.7(一切的基础,切记安装目录不能有中文不能有空格) 1)python2.7:(python环境):python-2.7.msi 2)setuptools(python包 ...

  7. jQuery动画效果实现

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. Nhibernate

    Nhibernate入门与demo 学习和使用Nhibernate已经很久了,一直想写点东西和大家一起学习使用Nhibernate.博客园里也有很多大牛写了很多关于Nhibernate入门的文章.其中 ...

  9. HTML5入门3---视频播放器

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...

  10. 汇编debug 截图2