题解——UVA11997 K Smallest Sums
题面
背景

输入

输出

翻译(渣自翻)
给定K个包含K个数字的表,要求将其能产生的\( k^{k} \)个值中最小的K个输出出来
题解
k路归并问题的经典问题
可以转化为二路归并问题求解
考虑A[],B[]两个有序数组
使用堆,记录一些二元组\( (x,y) \),x表示值,y表示对应的b的下标,因为我们是把b合并到a上,所以我们能够根据记录的下标推出后面的值
然后两两合并
所以就很简单
放代码
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
struct asshole{
int w,t;
bool operator < (const asshole b) const{
return w>b.w;
}
};
priority_queue<asshole> q;
int kz[],kk[];
int n;
void merge(int *a,int *b,int *c){
while(!q.empty())
q.pop();
for(int i=;i<=n;i++)
q.push((asshole){a[i]+b[],});
for(int i=;i<=n;i++){
asshole in = q.top();
q.pop();
a[i]=in.w;
int pos=in.t;
if(pos+<=n)
q.push((asshole){in.w-b[pos]+b[pos+],pos+});
}
}
int main(){
while(scanf("%d",&n)==){
for(int i=;i<=n;i++)
scanf("%d",&kz[i]);
sort(kz+,kz+n+);
for(int j=;j<=n-;j++){
for(int i=;i<=n;i++)
scanf("%d",&kk[i]);
sort(kk+,kk+n+);
merge(kz,kk,kz);
}
for(int i=;i<=n-;i++)
printf("%d ",kz[i]);
printf("%d",kz[n]);
printf("\n");
}
}
题解——UVA11997 K Smallest Sums的更多相关文章
- 【暑假】[实用数据结构]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 & ...
- uva11997 K Smallest Sums&&UVALive 3135 Argus(优先队列,多路归并)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- UVA11997 K Smallest Sums
思路 经典的k路归并问题 问题先转换为2路的有序表归并 先让A[1~k]都和B[1]相加,然后加入堆中,取出堆顶(A[x]+B[y])之后,再放入A[x]+B[y+1] 代码 #include < ...
- 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 ...
- 11997 - K Smallest Sums(优先队列)
11997 - K Smallest Sums You’re given k arrays, each array has k integers. There are kk ways to pick ...
- UVa 11997 K Smallest Sums 优先队列&&打有序表&&归并
UVA - 11997 id=18702" target="_blank" style="color:blue; text-decoration:none&qu ...
- 【优先队列】【UVa11997】K Smallest Sums
传送门 Description Input Output Translation · 给定k个长度为k的数组,把每个数组选一个元素加起来,这样共有kk种可能的答案,求最小的k个 Sample Inpu ...
- K Smallest Sums
uva11997:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
随机推荐
- 使用.NET向webService传double、int、DateTime 服务器得到的数据时null的问题(转http://blog.csdn.net/slimboy123/article/details/4366701)
用C#.NET调用Java开发的WebService时,先在客户端封装的带有int属性的对象,当将该对象传到服务器端时,服务器端可以得到string类型的属性值,却不能得到int类型.double和D ...
- ASP.NET JSON(转http://www.360doc.com/content/14/0615/21/18155648_386887590.shtml)
概念介绍还是先简单说说Json的一些例子吧.注意,以下概念是我自己定义的,可以参考.net里面的TYPE的模型设计如果有争议,欢迎提出来探讨!1.最简单:{"total":0} t ...
- Robot Framework 遇到过的错误 1. Chrome打开无法数据网址,地址栏只显示data:,
问题描述:用RF打开网页时未跳转到指定网址,而是显示data:, *** Settings ***Library SeleniumLibrary *** Test Cases ***Login_Tes ...
- Linux服务器---邮件服务openwebmail安装
安装openwebmail openwebmail提供了可视化的邮件管理系统,它运行在Apache环境下. 1.安装必备软件 [root@localhost ~]# yum install –y p ...
- POJ 1182 食物链 (种类并查集)
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说 ...
- 018.07 New BMW ICOM A3+B+C+D Plus EVG7 Controller Tablet PC with WIFI Function
2018.07 New BMW ICOM A3+B+C+D Plus EVG7 Controller Tablet PC with WIFI Function Software Version : ...
- MyEclipse如何修改XML文件默认行宽
1.MyEclipse如何修改XML文件默认行宽 Windows--->Preferences--->搜索xml--->XML--->XML Source--->Form ...
- right spindle supply short to gnd-- compact version
hardware guy found that the R1004 lead to this error, but this error should not be checked, because ...
- 算法笔记 #007# Backtracking
留着备用. 题目描述和代码参考:https://www.geeksforgeeks.org/8-queen-problem/ NQueenProblem(js代码): class NQueenProb ...
- Linux centos7下php安装cphalcon扩展的方法
说明: 操作系统:CentOS7 php安装目录:/usr/local/php php.ini配置文件路径:/usr/local/php/etc/php.ini 运行环境:LNMP ,PHP7 .安装 ...