UVA - 11997

Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

Description

 

Problem K

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

Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!

[Submit]   [Go Back]   [Status]

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm> using namespace std; int arr[][],k; struct Item
{
int s,b;
Item() {}
Item(int x,int y):s(x),b(y){}
bool operator<(const Item& res) const
{
return s>res.s;
}
}; void merge(int *A,int *B,int *C,int n)
{
priority_queue<Item> q;
for(int i=;i<n;i++) q.push(Item(A[i]+B[],));
for(int i=;i<n;i++)
{
Item tmp=q.top(); q.pop();
C[i]=tmp.s;
if(tmp.b+<n)
{
tmp.s=tmp.s-B[tmp.b]+B[tmp.b+];
tmp.b++;
q.push(tmp);
}
}
} int main()
{
while(scanf("%d",&k)!=EOF)
{
for(int i=;i<k;i++)
{
for(int j=;j<k;j++)
scanf("%d",&arr[i][j]);
sort(arr[i],arr[i]+k);
}
int A[],B[],C[];
memcpy(A,arr[],sizeof(A));
for(int i=;i<k;i++)
{
memcpy(B,arr[i],sizeof(B));
merge(A,B,C,k);
memcpy(A,C,sizeof(A));
}
for(int i=;i<k;i++)
{
if(i) putchar(' ');
printf("%d",C[i]);
}
putchar();
}
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. UVA 11997 K Smallest Sums 优先队列 多路合并

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

  3. UVa 11997 K Smallest Sums - 优先队列

    题目大意 有k个长度为k的数组,从每个数组中选出1个数,再把这k个数进行求和,问在所有的这些和中,最小的前k个和. 考虑将前i个数组合并,保留前k个和.然后考虑将第(i + 1)个数组和它合并,保留前 ...

  4. 【UVA 11997 K Smallest Sums】优先级队列

    来自<训练指南>优先级队列的例题. 题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18702 题意:给定 ...

  5. 优先队列 UVA 11997 K Smallest Sums

    题目传送门 题意:训练指南P189 分析:完全参考书上的思路,k^k的表弄成有序表: 表1:A1 + B1 <= A1 + B2 <= .... A1 + Bk 表2:A2 + B1 &l ...

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

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

  7. 【UVA–11997 K Smallest Sums 】

    ·哦,这题要用优先队列?那大米饼就扔一个手写堆上去吧! ·英文题,述大意:       输入n个长度为n的序列(题中是k,2<=k<=750).一种结果定义为:从每个序列中都要挑选一个数加 ...

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

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

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

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

随机推荐

  1. MMORPG大型游戏设计与开发(服务器 游戏场景 事件)

    今天第星期天,知识是永远是学习不完的,所以今天这部分算比较轻松,同时也希望大家会有一个好的周末.场景事件即场景的回调,和别的事件一样是在特定的条件下产生的,前面也介绍过场景的各种事件,今天详细的说一说 ...

  2. 【2016-11-1】【坚持学习】【Day16】【MongoDB】【复制集 分片】

    Mongodb 两种集群方式 复制集 通常是一主一从,一主多从 mongodb的复制至少需要两个节点.其中一个是主节点,负责处理客户端请求,其余的都是从节点,负责复制主节点上的数据. mongodb各 ...

  3. js实现九九乘法表

    <script type="text/javascript"> var sum=0; var wite; for (var i = 1; i < 10; i++) ...

  4. HTTPS----安全超文本传输协议

    HTTPS协议详解HTTPS以保密为目标研发,简单讲是HTTP的安全版.其安全基础是SSL协议,因此加密的详细内容请看SSL.全称Hypertext Transfer Protocol over Se ...

  5. NOIP2010pj三国游戏[博弈论]

    题目描述 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏. 在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有 N 位武将(N为偶数且不小于 4),任意两个武将之 ...

  6. [备份]破解Xamarin

    [转]试用了一阵子Mono For Android,今天到期了,,囊中羞涩,只好破解. 说是要在vs2013的英文界面下运行破解包,不知道是真是假,下载并安装了一个. 然后又下载了破解包.是个名为xa ...

  7. Java并发编程:Lock

    原文出处: 海子 在上一篇文章中我们讲到了如何使用关键字synchronized来实现同步访问.本文我们继续来探讨这个问题,从Java 5之后,在java.util.concurrent.locks包 ...

  8. Qt——自定义属性

    一.Qt中的属性 属性是指窗口或控件的属性,比如opacity属性表示“透明度”,geometry指的是“位置和大小”,pos属性代表“位置”. qt中的控件有自带的属性,我们也可以自己定义属性. Q ...

  9. Swift学习(二):自定义扩展方法(Extensions)

    扩展就是向一个已有的类.结构体或枚举类型添加新功能(functionality) 扩展可以 添加计算型属性和计算静态属性 定义实例方法和类型方法 提供新的构造器 定义下标 定义和使用新的嵌套类型 使一 ...

  10. EF6 在 SQLite中使用备忘

    == 菜鸟级选手试验在EF6中使用Sqlite,零EF基础,少量Sqlite基础.经过断断续续的很长时间 - _ -! >>连接 1. 安装 使用目前最新版本EF6.1,Sqlite1.0 ...