思路

经典的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的更多相关文章

  1. 【暑假】[实用数据结构]UVa11997 K Smallest Sums

    UVa11997 K Smallest Sums  题目: K Smallest Sums You're given k arrays, each array has k integers. Ther ...

  2. UVA-11997 K Smallest Sums

    UVA - 11997 K Smallest Sums Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & ...

  3. uva11997 K Smallest Sums&&UVALive 3135 Argus(优先队列,多路归并)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  4. 题解——UVA11997 K Smallest Sums

    题面 背景 输入 输出 翻译(渣自翻) 给定K个包含K个数字的表,要求将其能产生的\( k^{k} \)个值中最小的K个输出出来 题解 k路归并问题的经典问题 可以转化为二路归并问题求解 考虑A[], ...

  5. 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 ...

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

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

  7. UVa 11997 K Smallest Sums 优先队列&amp;&amp;打有序表&amp;&amp;归并

    UVA - 11997 id=18702" target="_blank" style="color:blue; text-decoration:none&qu ...

  8. 【优先队列】【UVa11997】K Smallest Sums

    传送门 Description Input Output Translation · 给定k个长度为k的数组,把每个数组选一个元素加起来,这样共有kk种可能的答案,求最小的k个 Sample Inpu ...

  9. K Smallest Sums

    uva11997:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...

随机推荐

  1. Excel GET.DOCUMENT说明

    GET.DOCUMENT(type_num, name_text) Type_num    指明信息类型的数.下表列出 type_num 的可能值与对应结果. Type_num        返回 1 ...

  2. Codeforces 1089E - Easy Chess - [DFS+特判][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem E]

    题目链接:https://codeforces.com/contest/1089/problem/E Elma is learning chess figures. She learned that ...

  3. mysql数据库的主从同步,实现读写分离 g

    https://blog.csdn.net/qq_15092079/article/details/81672920 前言 1 分别在两台centos 7系统上安装mysql 5.7 2 master ...

  4. 实验二:MAL——简单后门 by:赵文昊

    实验二:MAL--简单后门 一.后门是什么? 哪里有后门呢? 编译器留后门 操作系统留后门 最常见的当然还是应用程序中留后门 还有就是潜伏于操作系统中或伪装为特定应用的专用后门程序. 二.认识netc ...

  5. 【托业】【新东方托业全真模拟】TEST07~08-----P5~6

    unless ---conj:barring(除非,不包括)perp+名词短语 be capable of doing 有能力做某事 qualified commensurate with 与……相应 ...

  6. Android hdpi ldpi mdpi xhdpi xxhdpi适配详解

    设计稿计算: x/2.5=1080/3x=900y/2.5=1920/3y=1600 http://blog.csdn.net/lantiankongmo/article/details/505491 ...

  7. Python加密保护解决方案

    防止代码反编译,高强度加密保护exe或pyc文件 产品简介 Python语言写的程序无需编译成二进制文件代码,可以直接从源代码运行程序.在计算机内部,Python解释器把源代码转换成字节码的中间形式, ...

  8. Cartographer源码阅读(9):图优化的前端——闭环检测

    约束计算 闭环检测的策略:搜索闭环,通过匹配检测是否是闭环,采用了分支定界法. 前已经述及PoseGraph的内容,此处继续.位姿图类定义了pose_graph::ConstraintBuilder ...

  9. Spark mllib多层分类感知器在情感分析中的实际应用

    import org.apache.spark.ml.Pipeline import org.apache.spark.ml.classification.MultilayerPerceptronCl ...

  10. CentOS6.5安装pip

    首先重要的事情说三遍,因为可能有程序依赖目前的python2环境,比如yum: 不要动现有的python2环境! 不要动现有的python2环境! 不要动现有的python2环境! 如果你动了,yum ...