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 ...
随机推荐
- php中的性能挖掘
搞php以后,感觉总是很别扭,因为我觉得php会很慢,因为array普遍,在Key的循环查找不是很浪费性能么!因为我以前搞.net和java,他们是用的大多是寻址和索引方式,而php中太多是使用Key ...
- jquery常用函数与方法汇总
1.delay(duration,[queueName]) 设置一个延时来推迟执行队列中之后的项目. jQuery1.4新增.用于将队列中的函数延时执行.他既可以推迟动画队列的执行,也可以用于自定义队 ...
- WCF 之 OperationContract
这里主要说的是同名异常: [ServiceContract] public interface IUserInfo { [OperationContract] string ShowName(stri ...
- hlsl 的tex函数
texCUBE http://msdn.microsoft.com/en-us/library/windows/desktop/bb509687(v=vs.85).aspx
- 不安装Oracle客户端远程连接Orcale数据库
本方法是通过使用ORACLE官方提供的精简版客户端,即绿色免安装的客户端. 下载地址(此处提供的是官方各版本下载地址): Windows 32位系统中使用的客户端下载地址其他系统环境中使用的客户端下载 ...
- hdu 1596 find the safest road(最短路,模版题)
题目 这是用Dijsktra做的,稍加改动就好,1000ms..好水.. #define _CRT_SECURE_NO_WARNINGS #include<string.h> #inclu ...
- My_Plan
离清北夏令营不远了,今天时间也不太多了,貌似并不够我写一道题 所以就先挖个坑制定个计划啦 从5.10到5.20 先制定十天的计划吧 1.考试题的每道题目都要改出来并做总结 2.数位DP练习(十道+总结 ...
- Project Euler 75:Singular integer right triangles
题目链接 原题: It turns out that 12 cm is the smallest length of wire that can be bent to form an integer ...
- 字符编解码的故事–ASCII,ANSI,Unicode,Utf-8区别(转)
很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们认为8个开关状态作为原子单位很好,于是他们把这称为”字节”. 再后来,他们又做了一些可以处理这些字节的 ...
- 78. Subsets
题目: Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset mus ...