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 ...
随机推荐
- react中改变echart图表的形状
首先说明一点constructor中的只会渲染一次. 父组建是两个点击按钮,点击一个传过来bar,和一个line,子组件也就是当前组建通过this.props.type接收. 渲染是通过::::::t ...
- react学习笔记01
被项目拖了半年的我终于有时间学习react 了 下面是我最近学习的笔记 支持jsx语法 ReactDOM.render( <div> <h1>hello, word</h ...
- Python学习之旅(二十四)
Python基础知识(23):进程和线程(Ⅱ) 一.threadlocal 在多线程环境下,每个线程都有自己的数据 一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响 ...
- Nginx之使用nginx搭建简单的文件服务器
使用nginx可以搭建简单文件服务器 安装nginx(不详述) 修改配置文件 /usr/local/nginx/conf/nginx.conf user root; /usr/local/nginx/ ...
- Linux命令 printf
定长: $ printf '%s\t%s\t%s\t%s\t%s\t\n' $(cat printf.txt) # %s 表示一个不固定长度的字符串:printf不是管道命令,因此需要通过类似cat的 ...
- PAT甲级1141 Ranking of Institutions
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805344222429184 题意: 给定几个学生的PAT分数和学校 ...
- ASP.NET MVC Routing Debugger路由调试工具
官网地址:http://blog.csdn.net/sgear/article/details/6789882 To use this, simply download the following ...
- Compile caffe on unbutu 16.0.4
1. apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhd ...
- Appium下载安装及环境配置
下载地址:https://bitbucket.org/appium/appium.app/downloads/ windows安装: 下载 AppiumForWindows.zip 解压 Appium ...
- jquery实现一个标签图标hover到上面的时候显示tooltip
设计图: 解决思路:1.在thumbnailbox.js这个插件中加入tags弹出框显示的内容,一开始让这些内容display:none; 然后再用css画出来一个三角形 实现方法: 知识点:Jque ...