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. sharepoint 自定义字段实现省市联动

    最后实现效果如下:设置栏如下:解决方案结构如下: fldtypes_RoyCustomField.xml 内容如下: <?xml version="1.0" encoding ...

  2. Codeigniter-实现权限认证

    两种方法 钩子函数 集成核心Controller 方法一,钩子函数: 一直没找到CI的权限认证扩展,以前好像找到过一个老外的扩展,不过不怎么好用,现在记不清了,后来仿着jsp firter的方式用CI ...

  3. BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )

    MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...

  4. [LeetCode]题解:005-Longest Palindromic Substring优化

    题目来源和题意分析: 详情请看我的博客:http://www.cnblogs.com/chruny/p/4791078.html 题目思路: 我上一篇博客解决这个问题的时间复杂度是最坏情况是(O(n^ ...

  5. 数组length属性的一些特性

    ~~·数组的length属性是可读写的 var colors = ["blue","red","green"];colors.length ...

  6. 求最大值最小值的方法 时间复杂度O(n)

    #include<iostream> #include <iostream> #include <bitset> #include <ctime> us ...

  7. C++ 用libcurl库进行http 网络通讯编程

      一.LibCurl基本编程框架libcurl是一个跨平台的网络协议库,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议.libcur ...

  8. mfc删除标题和边框

    //删除标题和边框WS_CAPTION和WS_BORDER风格 ModifyStyle(WS_CAPTION, 0);ModifyStyle(WS_BORDER, 0);

  9. 表格java代码的相关知识积累

    本文主要收集各大博客中的java表格 用JSP创建一个表格模板 . 项目中要用到一些展示信息的表格,表头不固定,表格内容是即时从后台取的:考虑到复用性,笔者用jsp编写了一个表格模板,可以从reque ...

  10. Bug驱动开发(Bug-driven development)

    说实话,作为一个Domino开发者,像測试驱动开发(Test-driven development).功能驱动开发(Feature-driven development)之类软件开发的高大上的方法论( ...