UVA-11997 K Smallest Sums
UVA - 11997
Description ![]() Problem KK Smallest SumsYou'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. InputThere 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. OutputFor each test case, print the k smallest sums, in ascending order. Sample Input3 Output for the Sample Input9 10 12 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! |
#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的更多相关文章
- UVa 11997 K Smallest Sums 优先队列&&打有序表&&归并
UVA - 11997 id=18702" target="_blank" style="color:blue; text-decoration:none&qu ...
- UVA 11997 K Smallest Sums 优先队列 多路合并
vjudge 上题目链接:UVA 11997 题意很简单,就是从 k 个数组(每个数组均包含 k 个正整数)中各取出一个整数相加(所以可以得到 kk 个结果),输出前 k 小的和. 这时训练指南上的一 ...
- UVa 11997 K Smallest Sums - 优先队列
题目大意 有k个长度为k的数组,从每个数组中选出1个数,再把这k个数进行求和,问在所有的这些和中,最小的前k个和. 考虑将前i个数组合并,保留前k个和.然后考虑将第(i + 1)个数组和它合并,保留前 ...
- 【UVA 11997 K Smallest Sums】优先级队列
来自<训练指南>优先级队列的例题. 题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18702 题意:给定 ...
- 优先队列 UVA 11997 K Smallest Sums
题目传送门 题意:训练指南P189 分析:完全参考书上的思路,k^k的表弄成有序表: 表1:A1 + B1 <= A1 + B2 <= .... A1 + Bk 表2:A2 + B1 &l ...
- uva 11997 K Smallest Sums 优先队列处理多路归并问题
题意:K个数组每组K个值,每次从一组中选一个,共K^k种,问前K个小的. 思路:优先队列处理多路归并,每个状态含有K个元素.详见刘汝佳算法指南. #include<iostream> #i ...
- 【UVA–11997 K Smallest Sums 】
·哦,这题要用优先队列?那大米饼就扔一个手写堆上去吧! ·英文题,述大意: 输入n个长度为n的序列(题中是k,2<=k<=750).一种结果定义为:从每个序列中都要挑选一个数加 ...
- UVA 11997 K Smallest Sums (多路归并)
从包含k个整数的k个数组中各选一个求和,在所有的和中选最小的k个值. 思路是多路归并,对于两个长度为k的有序表按一定顺序选两个数字组成和,(B表已经有序)会形成n个有序表 A1+B1<=A1+B ...
- 11997 - K Smallest Sums(优先队列)
11997 - K Smallest Sums You’re given k arrays, each array has k integers. There are kk ways to pick ...
随机推荐
- AWS S3 CLI的权限bug
使用AWS CLI在S3上创建了一个bucket,上传文件的时候报以下错误: A client error (AccessDenied) occurred when calling the Creat ...
- 《InsideUE4》-8-GamePlay架构(七)GameMode和GameState
我的世界,我做主 引言 上文我们说到在Actor层次,UE用Controller来充当APawn的逻辑控制者,也有了可以接受玩家输入的PlayerController,和能自行行动的AIControl ...
- ubuntu下安装加装DNS
感觉在ubuntu下网速特别的慢,所以网上找了下解决方案,本地缓存域名解析相关信息. 首先,安装dns服务 命令: sudo apt-get install dnsmasq 编辑dnsmasq的配置文 ...
- zlib压缩一个文件为gzip格式
网上有很多针对zlib的总结,但是很零散,自己经过总结,实现了用zlib压缩一个文件为gzip格式,似的可以直接使用winr工具解压. 具体方法是使用zlib的deflate系列函数,将buffer压 ...
- [No000077]打造自己的Eclipse
下载官网的Eclipse IDE for Java EE Developers 在根目录下的eclipse.ini文件中添加"-Dfile.encoding=UTF-8", 作用: ...
- Java的泛型反射
If the superclass is a parameterized type, the {@code Type} * object returned must accurately reflec ...
- time.c 的Java实现(从timestamp计算年月日时分秒等数值)
time.c的Java实现 public class GMT { public static final int EPOCH_YEAR = 1970; public static final int[ ...
- Grumpy: Go 上运行 Python!
Google 运行数百万行Python代码用于驱动youtube.com和YouTube API的前端服务器,每秒可以提供数百万的请求! YouTube的前端运行在CPython 2.7上,所以我们投 ...
- Qt5.7.0配置选项(configure非常详细的参数)
configure是一个命令行工具,用于配置Qt编译到指定平台.configure必须运行于Qt源码根目录.当运行configure时,编译源码使用的是所选工具链中的make工具. 一.源码目录.编译 ...
- 在Windows Server 2012 R2上安装SharePoint 2013 with SP1失败,提示没有.net4.5的解决办法
现在的Server用Windows Server 2012 R2的越来越多了,在部署带Sp1的SharePoint2013的时候,走完预安装工具后,点击setup提示缺少.net4.5. 其实Wind ...