题面

  背景

  输入

  

  输出

    

翻译(渣自翻)

  给定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的更多相关文章

  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路归并问题 问题先转换为2路的有序表归并 先让A[1~k]都和B[1]相加,然后加入堆中,取出堆顶(A[x]+B[y])之后,再放入A[x]+B[y+1] 代码 #include < ...

  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. Marlin 擠出頭溫度控制PID值校正

    Marlin 擠出頭溫度控制PID值校正 擠出頭加熱器.溫度感應器安裝好後,先別急著直接指定工作溫度並且加熱.因為控制板上的溫度控制PID參數尚未校正.如果加熱速度過快,有可能會加熱過度並且導致零件燒 ...

  2. Widget Factory (高斯消元解线性方程组)

    The widget factory produces several different kinds of widgets. Each widget is carefully built by a ...

  3. jQuery筛选--hasClass(class)和eq(index|-index)

    hasClass(class) 概述 检查当前的元素是否含有某个特定的类,如果有,则返回true 参数 class  用于匹配的类名 <!DOCTYPE html> <html> ...

  4. Block 循环引用(中)

    不会造成循环引用的block 大部分GCD方法 dispatch_async(dispatch_get_main_queue(), ^{ [self doSomething]; }); 因为self并 ...

  5. win7 64位 python启动报错:无法启动此程序,因为计算机中丢失api-ms-win-crt-process-l1-1-0.dll

    安装python3.7,安装成功后,在cmd窗口输入python检查是否安装成功,报错:无法启动此程序,因为计算机中丢失api-ms-win-crt-process-l1-1-0.dll 在网上查询了 ...

  6. Java解析Json字符串--复杂对象

    { "name": "三班", "students": [ { "age": 25, "gender" ...

  7. Linux环境变量和本地变量

    每一种编程语言中,我们都会碰到变量的作用域的问题.(比如在函数中定义的变量在函数外不能使用的) BASH 中也有类似的问题,局部变量和环境变量(全局变量). 局部变量是普通的变量,仅在创建它的Shel ...

  8. Firefox 功能笔记

    1.复制标签 说明:复制标签功能即新开一个与当前页一样的标签页,这个功能在Chrome中点击标签右键复制即可,但是在firefox中没有 Firefox中实现:Ctrl+拖动标签页

  9. 区块链公链分片技术(sharding)方案,配思维导图

    区块链公链分片技术(sharding)方案,配思维导图 分片技术(sharding)方案 以太坊分片思路 其基本思想是,将网络中的节点分成不同的碎片,各分片可以并行处理不同交易,这样可以并行处理相互之 ...

  10. 性能测试工具Locust,一个开源性能测试工具

    性能测试工具Locust,一个开源性能测试工具使用Python代码来定义用户行为.用它可以模拟百万计的并发用户访问你的系统.1.它与目前主流的LoadRunner和Jmeter玩法都不一样.2.它完全 ...