UVA11997 K Smallest Sums
思路
经典的k路归并问题
问题先转换为2路的有序表归并
先让A[1~k]都和B[1]相加,然后加入堆中,取出堆顶(A[x]+B[y])之后,再放入A[x]+B[y+1]
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct QNode{
int val,s;
bool operator < (const QNode &b) const{
return val>b.val;
}
};
priority_queue<QNode> q;
void merge(int *a,int *b,int *c,int n){//合并A和B两个有序表
while(!q.empty())
q.pop();
for(int i=1;i<=n;i++)
q.push((QNode){a[i]+b[1],1});
for(int i=1;i<=n;i++){
QNode x=q.top();
while(x.s>n){
q.pop();
x=q.top();
}
q.pop();
c[i]=x.val;
q.push((QNode){x.val-b[x.s]+b[x.s+1],x.s+1});
}
}
int A[2][800],k;
int main(){
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
while(scanf("%d",&k)==1){
for(int i=1;i<=k;i++)
scanf("%d",&A[0][i]);
sort(A[0]+1,A[0]+k+1);
for(int i=1;i<=k-1;i++){
for(int j=1;j<=k;j++)
scanf("%d",&A[1][j]);
sort(A[1]+1,A[1]+k+1);
merge(A[0],A[1],A[0],k);
}
for(int i=1;i<=k;i++)
printf("%d%c",A[0][i],(i==k)?'\n':' ');
}
return 0;
}
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个包含K个数字的表,要求将其能产生的\( k^{k} \)个值中最小的K个输出出来 题解 k路归并问题的经典问题 可以转化为二路归并问题求解 考虑A[], ...
- 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 ...
随机推荐
- JAVA课后作业01
一.关于枚举的问题 public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; Size t ...
- ajax-addclass
- js获取谷歌浏览器版本 和 js分辨不同浏览器
// 获取谷歌版本 function getChromeVersion() { var arr = navigator.userAgent.split(' '); var chromeVersion ...
- Centos7 修改系统时区timezone
Centos7 修改系统时区timezone 注意:修改Linux系统的时区以后,再安装jvm,jvm默认会使用系统的时区.如果系统时区设置错误,安装jvm后,再修改系统的时区,但jvm的时区仍然用不 ...
- Ubuntu 18.04 安装Docker
第一种方法从Ubuntu的仓库直接下载安装: 安装比较简单,这种安装的Docker不是最新版本,不过对于学习够用了,依次执行下面命令进行安装. $ sudo apt install docker.io ...
- 【托业】【跨栏阅读】错题集-REVIEW1
05 06 REVIEW 1
- Win10系统jdk环境变量配置方法
http://www.w10zj.com/Win10xy/Win10yh_5620.html
- 使用Chrome远程调试GenyMotion上的WebView程序
WebView让我们方便的使用熟悉的Html/JS/Css来开发APP.但是,当出现问题时,却没有PC上那么方便的排查问题.PC上,前端的问题我们可以使用Chrome的开发者工具方便的调试.Andro ...
- 使用dbeaver查mysql的表会导致锁表的问题
查询完成之后接着需要使用rollback,不然其它session没法执行语句.
- git help 机器翻译
该篇发布仅为博主个人保存并参考,内容可能不对 usage: git [--version] [--help] [-C <path>] [-c <name>=<value& ...