UVa 11997 (优先队列 多路归并) K Smallest Sums
考虑一个简单的问题,两个长度为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的更多相关文章
- UVa 11997 K Smallest Sums 优先队列&&打有序表&&归并
UVA - 11997 id=18702" target="_blank" style="color:blue; text-decoration:none&qu ...
- 11997 - K Smallest Sums(优先队列)
11997 - K Smallest Sums You’re given k arrays, each array has k integers. There are kk ways to pick ...
- 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 ...
- 【暑假】[实用数据结构]UVa11997 K Smallest Sums
UVa11997 K Smallest Sums 题目: K Smallest Sums You're given k arrays, each array has k integers. Ther ...
- UVA-11997 K Smallest Sums
UVA - 11997 K Smallest Sums Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- uva 11997 优先队列
K Smallest Sums You're given k arrays, each array has k integers. There are kk ways to pick exactly ...
- uva 11997 K Smallest Sums 优先队列处理多路归并问题
题意:K个数组每组K个值,每次从一组中选一个,共K^k种,问前K个小的. 思路:优先队列处理多路归并,每个状态含有K个元素.详见刘汝佳算法指南. #include<iostream> #i ...
- UVA 11997 K Smallest Sums 优先队列 多路合并
vjudge 上题目链接:UVA 11997 题意很简单,就是从 k 个数组(每个数组均包含 k 个正整数)中各取出一个整数相加(所以可以得到 kk 个结果),输出前 k 小的和. 这时训练指南上的一 ...
- UVA 11997 K Smallest Sums (多路归并)
从包含k个整数的k个数组中各选一个求和,在所有的和中选最小的k个值. 思路是多路归并,对于两个长度为k的有序表按一定顺序选两个数字组成和,(B表已经有序)会形成n个有序表 A1+B1<=A1+B ...
随机推荐
- Careercup - Facebook面试题 - 5179916190482432
2014-05-01 00:45 题目链接 原题: input [,,,] output [,,,] Multiply all fields except it's own position. Res ...
- 合并2个dll成一个,好处你懂的
步骤一:先下载微软的工具 ilmerge.exe 地址:http://www.microsoft.com/en-us/download/details.aspx?id=17630 步骤二:安装好之后 ...
- 我的第一个python爬虫程序
程序用来爬取糗事百科上的图片的,程序设有超时功能,具有异常处理能力 下面直接上源码: #-*-coding:utf-8-*- ''' Created on 2016年10月20日 @author: a ...
- pos机套现是怎么回事
POS机是商家为了促进消费,向银行申请的刷卡机它的主要功能是转账就是通过客户的刷卡,把相对的金额转入商户的帐户银行会根据笔数或金额向商户收取手续费非法套现就是客户并未和商户产生贸易往来,单纯通过pos ...
- 【转】2-SAT题集
转自:http://blog.csdn.net/shahdza/article/details/7779369 [HDU]3062 Party1824 Let's go home3622 Bomb G ...
- unity3d结合轮廓显示,实现完整的框选目标(附Demo代码)
原地址:http://dong2008hong.blog.163.com/blog/static/469688272013111554511948/ 在unity里实现,其实很简单,因为有两个前提:1 ...
- 在UWSGI和NGINX配合的情况下,配置个别目录上传及超时
笨办法就是多加一个LOCATION. 然后,自定义以下类似参数 client_max_body_size uwsgi_connect_timeout uwsgi_read_timeout server ...
- [Ruby on Rails系列]6、一个简单的暗语生成器与解释器(上)
[0]Ruby on Rails 系列回顾 [Ruby on Rails系列]1.开发环境准备:Vmware和Linux的安装 [Ruby on Rails系列]2.开发环境准备:Ruby on Ra ...
- 解决virtualbox 虚拟机不能ping通win7
凭经验猜测是由于防火墙引起的,关闭防火墙再ping,果然可行.google说这是由于“win7 防火墙默认的禁ping策略”引起的.但是关闭防火墙很不安全,可以按照以下步骤为防火墙添加入站规则来解决问 ...
- lintcode:四个数之和
题目 四数之和 给一个包含n个数的整数数组S,在S中找到所有使得和为给定整数target的四元组(a, b, c, d). 样例 例如,对于给定的整数数组S=. 满足要求的四元组集合为: (-1, 0 ...