11997 - K Smallest Sums

You’re given k arrays, each array has k integers. There are k
k ways to pick exactly one element in each
array and calculate the sum of the integers. Your task is to find the k smallest sums among them.
Input
There will be several test cases. The first line of each case contains an integer k (2 ≤ k ≤ 750). Each of
the following k lines contains k positive integers in each array. Each of these integers does not exceed
1,000,000. The input is terminated by end-of-file (EOF).
Output
For each test case, print the k smallest sums, in ascending order.
Sample Input
3
1 8 5
9 2 5
10 7 6
2
1 1
1 2
Sample Output
9 10 12
2 2

题意:

给你k个数组,从每个数组里面取个数字,求和,让找k个最小值;

大神的优先队列,自己用好友的思路写了一遍,没考虑周全,wa了,然后借助大神的思路写了一遍就对了,大神是把复杂问题简单化,把k维转化为二维;

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=800;
typedef long long LL;
int mp[MAXN][MAXN];
struct Node{
int s,p;
Node(int s,int p):s(s),p(p){}
friend bool operator <(Node a,Node b){
return a.s>b.s;
}
};
void merge(int *a,int *b,int *c,int n){
sort(a,a+n);sort(b,b+n);
priority_queue<Node>dl;
for(int i=0;i<n;i++)dl.push(Node(a[i]+b[0],0));
for(int i=0;i<n;i++){
Node d=dl.top();dl.pop();
c[i]=d.s;
dl.push(Node(d.s-b[d.p]+b[d.p+1],d.p+1));
}
}
int main(){
int k;
while(~scanf("%d",&k)){
for(int i=0;i<k;i++)for(int j=0;j<k;j++)scanf("%d",&mp[i][j]);
for(int i=1;i<k;i++){
merge(mp[0],mp[i],mp[0],k);
}
for(int i=0;i<k;i++){
if(i)printf(" ");
printf("%d",mp[0][i]);
}
puts("");
}
return 0;}

 刚开始没考虑周全的代码:

4

1 2 5 100

1 2 8 100

1 8 9 100

1 10 15 100

这组数据就没过。。

wa代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=800;
typedef long long LL;
priority_queue<int,vector<int>,greater<int> >dl,q;
int main(){
int k;
while(~scanf("%d",&k)){
while(!dl.empty())dl.pop();
while(!q.empty())q.pop();
int ans=0;
int x,a,b;
for(int i=0;i<k;i++){
for(int j=0;j<k;j++){
scanf("%d",&x);dl.push(x);
}
a=dl.top();
ans+=a;
dl.pop();
b=dl.top();
q.push(b-a);
// printf("%d %d\n",a,b);
while(!dl.empty())dl.pop();
}
for(int i=0;i<k;i++){
if(i)printf(" ");
if(!i)printf("%d",ans);
else printf("%d",ans+q.top()),q.pop();
}
puts("");
}
return 0;
}

  

11997 - K Smallest Sums(优先队列)的更多相关文章

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

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

  2. UVa 11997 K Smallest Sums - 优先队列

    题目大意 有k个长度为k的数组,从每个数组中选出1个数,再把这k个数进行求和,问在所有的这些和中,最小的前k个和. 考虑将前i个数组合并,保留前k个和.然后考虑将第(i + 1)个数组和它合并,保留前 ...

  3. UVA 11997 K Smallest Sums 优先队列 多路合并

    vjudge 上题目链接:UVA 11997 题意很简单,就是从 k 个数组(每个数组均包含 k 个正整数)中各取出一个整数相加(所以可以得到 kk 个结果),输出前 k 小的和. 这时训练指南上的一 ...

  4. uva 11997 K Smallest Sums 优先队列处理多路归并问题

    题意:K个数组每组K个值,每次从一组中选一个,共K^k种,问前K个小的. 思路:优先队列处理多路归并,每个状态含有K个元素.详见刘汝佳算法指南. #include<iostream> #i ...

  5. 优先队列 UVA 11997 K Smallest Sums

    题目传送门 题意:训练指南P189 分析:完全参考书上的思路,k^k的表弄成有序表: 表1:A1 + B1 <= A1 + B2 <= .... A1 + Bk 表2:A2 + B1 &l ...

  6. 【UVA 11997 K Smallest Sums】优先级队列

    来自<训练指南>优先级队列的例题. 题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18702 题意:给定 ...

  7. 【UVA–11997 K Smallest Sums 】

    ·哦,这题要用优先队列?那大米饼就扔一个手写堆上去吧! ·英文题,述大意:       输入n个长度为n的序列(题中是k,2<=k<=750).一种结果定义为:从每个序列中都要挑选一个数加 ...

  8. UVA 11997 K Smallest Sums (多路归并)

    从包含k个整数的k个数组中各选一个求和,在所有的和中选最小的k个值. 思路是多路归并,对于两个长度为k的有序表按一定顺序选两个数字组成和,(B表已经有序)会形成n个有序表 A1+B1<=A1+B ...

  9. uva_11997,K Smallest Sums优先队列

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> #inclu ...

随机推荐

  1. js 时间戳转换成时间格式,可自定义格式

    由于 c# 通过ajax获取的时间 传到前台 格式为:/Date(1354116249000)/ 所以需要转换一下,想要什么格式 更改 format() 里的 返回语句 就可以了 formatDate ...

  2. 雪碧图(sprite)

    雪碧图 是一种将网页上常用且不经常变动的小图标集中在一张大图中,根据网页需求来显示图片的技术. 可以提高网页加载速度,增加用户体验. 其原理是通过html块状元素建立一个满足需求的视图窗口,然后在窗口 ...

  3. ajax 上传图片 并预览

     <img {if !$article[pic]}style="display: none"{/if} width="82" height="6 ...

  4. 【转】 一个fork的面试题

    转自:一个fork的面试题 前两天有人问了个关于Unix的fork()系统调用的面试题,这个题正好是我大约十年前找工作时某公司问我的一个题,我觉得比较有趣,写篇文章与大家分享一下.这个题是这样的: 题 ...

  5. mysql server5.6.28 修改数据目录

    1.查看配置文件 mysql --help | grep my.cnf 列出使用哪个配置文件(顺序推) 2.service mysql stop 3.创建新目录 mkdir /data 4.迁移之前的 ...

  6. jvm 调优 工具

    1.用于打开运行中的jvm进程的gc 监控日志以及查看相关参数设置:jinfo 2.其它工具如:jps.jstack.jstat.jmap

  7. python 中文异常问题记录

    头上加入以下内容试试: # -*- coding:utf-8import sysimport osreload(sys)sys.setdefaultencoding( "utf-8" ...

  8. css布局: 两栏 自适应高度

    只使用css实现 有两种方式, 一种是通过相对定位,再绝对定位获取父亲元素的高度, 一种是通过margin-bottom:-999em;padding-bottom: 999em; 父亲元素超出隐藏 ...

  9. python中3个帮助函数help、dir、type的使用

    1.help函数:查看模块.函数.变量的详细说明: 查看模块 help("modules") 查看包  help("json") 查看类 help(json.J ...

  10. 关于android源码中的APP编译时引用隐藏的API出现的问题

    今天在编译android源码中的计算器APP时发现,竟然无法使用系统隐藏的API,比如android.os.ServiceManager中的API,引用这个类时提示错误,记忆中在android源码中的 ...