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. Foundation 框架 归档

    一.使用XML属性列表归档 此方法适用于NSString.NSDictionary.NSarray.NSDate.NSnumber,其中atomically参数表示先将字典写入临时备份文件,成功之后, ...

  2. 【原创】移除RX filters在C118上面

    » 作者:LSX » 原创文章版权归作者所有,未经作者同意请保留以下声明. » 本文链接:http://blog.lishixin.net/?p=1318 » 转载请注明来源:LSX·Blog » & ...

  3. UVa1225 Digit Counting

    #include <stdio.h>#include <string.h> int main(){    int T, N, i, j;    int a[10];    sc ...

  4. 在单链表和双链表中删除倒数第K个节点

    [说明]: 本文是左程云老师所著的<程序员面试代码指南>第二章中“在单链表和双链表中删除倒数第K个节点”这一题目的C++复现. 本文只包含问题描述.C++代码的实现以及简单的思路,不包含解 ...

  5. Trie implementation

    在学习 Aho - Corasick Automation Algorithm 之前,先学习一下,Trie 的实现过程: The name trie comes from its use for re ...

  6. Hibernate学习之缓存机制

    转自:http://www.cnblogs.com/xiaoluo501395377/p/3377604.html 一.N+1问题 首先我们来探讨一下N+1的问题,我们先通过一个例子来看一下,什么是N ...

  7. removeCss

    (function ($) { //删除元素行内style中单个style和多个style //示例: //$(".b").removeCss("color") ...

  8. PHP自练项目之数字分页效果

    学习要点:1.LIMIT 用法2.各种参数3.超链接调用 第一:先在文件中设置数字分页模块:我的文件是(blog.php) //分页模块 $_page = $_GET['page']; $_pages ...

  9. 弹出窗口内嵌iframe 框口自适应

    说一下,弹出窗口还有内嵌一个iframe 这种模式应该是不科学的,但是公司项目里面就偏偏用到了,它这高低还不能只适应,所以我痛苦的日子来了 分析一下: 首先window.showDialog 方法的时 ...

  10. oracle10g安装图解(win7)

    一.Oracle10g 安装预备步骤取得 Oracle 10g 安装程序,或从 Oracle 技术网(OTN)下载光盘映像.在评估阶段您可以免费下载和使用无技术限制的全功能 Oracle,但在正式的商 ...