题解——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 ...
随机推荐
- html5-button元素
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 1113: No mapping for the Unicode character exists in the target multi-byte code page
windows版本nginx启动 报错. 启动方式:到nginx所在目录执行:nginx.exe -c conf\nginx.conf 原因:所在路径中含有中文字符. 解决:换个没有中文的路径.
- localStorage过期策略
localStorage过期策略 由于html5没有给本地存储设置过期策略,那么在处理数据的过期策略的时候可以编写自己过期策略程序,如下: <!DOCTYPE> <head> ...
- 常用linux命令行
1.ls命令 ls -a 列出目录所有文件,包含以.开始的隐藏文件 ls -A 列出除.及..的其它文件 ls -r 反序排列 ls -t 以文件修改时间排序 ls -S 以文件大小排序 ls -h ...
- docker 常用操作
1,安装 .检查内核版本,必须是3.10及以上 uname ‐r .安装docker yum install docker .输入y确认安装 .启动docker [root@localhost ~]# ...
- maven war工程重命名
1,按f2对项目进行改名 2,改变其web.xml 的项目名 3,org.eclipse.wst.common.component 改变其项目名
- Java中this和super关键字
今天练习到Java中的this和super关键字,我有如下总结: 1.子类继承父类,子类初始化对象,必须先调用父类构造方法,因为随时有可能要使用父类的成员变量. 2.get和set方法只是对成员变量进 ...
- Tomcat配置Manager管理员
修改文件: D:\MyDev\Tomcat\apache-tomcat-7.0.68\conf\tomcat-users.xml 配置内容: <role rolename="mana ...
- Linux 安装本地 yum源
放入Centos6.4的镜像光盘或找到镜像文件 [root]#mount /dev/cdrom /media/cdrom #挂载本地镜像 [root]#rm -rf /etc/yum.repo.d ...
- springboot 项目pom.xml文件基本配置
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...