K Smallest Sums

uva11997:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=3148&mosmsg=Submission+received+with+ID+13942033

题意:给你k个数组,每个数组里有k个数,然后每个数组里面取出一个数相加会得到一个数,让你求出最小的看的数。

题解:白书的分析:

分析:这题有简化版本的,即2个整数数组A,B,包含K个元素,在每个数组中取一个元素加起来,可以得到k^2个和,求这些和中最小的K个值。

我们需要把这k^2个和组织成如下k个有序表.

表1:A1+B1<=A1+B2<=......<=A1+Bk

表2: A2+B1<=A2+B2<=......<=A2+Bk

表k:Ak+B1<=AK+B2<=......<=Ak+Bk

我们可以用二元组(s,b)来表示一个元素即s=Aa+Bb;为什么不保存A的下标a呢?因为我们用不到a的值。如果我们需要元素(s,b)在表a的下一个元素(s‘,b+1).只需要计算s‘=s+B[b+1]-B[b];

思想感觉就是DP。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<queue>
 6 using namespace std;
 7 int a[800][800];
 8 int k;
 9 int ans[800];
10 struct Node{
11    int s;
12    int id;
13    bool operator<(const Node a)const {
14      return s>a.s;
15    }
16 };
17 void merge(int a[],int b[]){
18    priority_queue<Node>Q;
19    for(int i=1;i<=k;i++){
20     Node tt;
21     tt.s=a[i]+b[1];
22     tt.id=1;
23     Q.push(tt);
24    }
25    for(int i=1;i<=k;i++){
26       Node temp=Q.top();
27       Q.pop();
28       ans[i]=temp.s;
29       Node t2;
30       int num=temp.id;
31       if(num<k){
32         t2.s=temp.s-b[num]+b[num+1];
33         t2.id=num+1;
34       Q.push(t2);
35       }
36    }
37 }
38 int main(){
39   while(~scanf("%d",&k)){
40     memset(a,0,sizeof(a));
41     memset(ans,0,sizeof(ans));
42     for(int i=1;i<=k;i++){
43         for(int j=1;j<=k;j++)
44             scanf("%d",&a[i][j]);
45         sort(a[i]+1,a[i]+k+1);
46     }
47      merge(a[1],a[2]);
48     for(int i=3;i<=k;i++)
49      merge(ans,a[i]);
50      for(int i=1;i<k;i++)
51         printf("%d ",ans[i]);
52       printf("%d\n",ans[k]);
53   }
54 }

时间: 2024-08-01 15:10:04

K Smallest Sums的相关文章

(DS 《算法竞赛入门经典》)UVA 11997 K Smallest Sums

题目大意:有k个数组,每个数组选取一个数,组成k^k个数.在这k^k个数中选择最小的前k个数 解题思路: 1.如果只有k个数组,那么最后得到的最小的前k个数应该可以由前两个数组得到的最小k个数与第三个数组 按规则运算后得到. 2.如果每个数组只有3个数.那么前两个数组(a:(a0,a1,a2)    b:(b0,b1,b2,a与b数组都已经有序)运算后有的结果矩阵如下: a0+b0,a0+b1,a0+b2 a1+b0,a1+b1,a1+b2 a2+b0,a2+b1,a2+b2 在这个矩阵中,a0

UVA 11997 The K smallest Sums

给出K*K的矩阵,每一行取一个数,构成K个数的和,总共有 k^k种可能,从中取出前k个最小的. 一开始犯了错,因为只要对每行排序,最小的必定是第一列的和,然后我当时就想着,逐步推进,每次将某行的那个数变成其下一列那个数,当然间距要最小.我这样明显是不对的,这样的话每个数只用了一次,而题目的意思明显是可以重复多次. 然后大白上说的是把他看成两行,即每次处理两行,留下最小的k个 再与下一次读入的那一行继续处理.这个方法相当给力,不过有个难点是,怎么在两行的时候,比较快的得到前k小的,我试过全部算一遍

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

UVA - 11997 K Smallest Sums Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status 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 o

11997 - K Smallest Sums(优先队列)

11997 - K Smallest Sums You’re given k arrays, each array has k integers. There are kk ways to pick exactly one element in eacharray and calculate the sum of the integers. Your task is to find the k smallest sums among them.InputThere will be several

【优先队列之多路合并】UVA - 11997 K Smallest Sums

Source : UVA - 11997 K Smallest Sums http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18702 题意 有k个整数数组,各包含k个元素,从每个数组中选取一个元素加起来,可以得到k^k个和,求这些和中最小的k个值. 示例 Sample Input 3 1 8 5 9 2 5 10 7 6 2 1 1 1 2 Sample Output 9 10 12 2 2 思路 二路归并构建二维平面表:

D - K Smallest Sums(多路归并+贪心)

Problem K K Smallest Sums You're given k arrays, each array has k integers. There are kk 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 se

【暑假】[实用数据结构]UVa11997 K Smallest Sums

UVa11997 K Smallest Sums  题目: K Smallest Sums You're given k arrays, each array has k integers. There are kk 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. In

uva 11997 K Smallest Sums

首先对每行进行排序,并对与前两行有$A = a_1 \leq a_2 \leq \cdots \leq a_k$和$B = b_1 \leq b_2 \leq \cdots \leq b_k$.首先把所有的$b_i , i\in [1,k]$与$a_1$进行求和,并加入优先队列中.其中最小的必然是$a_1+b_1$,在优先队列队首.然后只需在优先队列中压入$a_1+b_2$,没有压入$a_2+b_1$原因是该和已经在队列中存在了,没有压入$a_2+b_2$的原因是$a_2+b_2 \leq a_

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

vjudge 上题目链接:UVA 11997 题意很简单,就是从 k 个数组(每个数组均包含 k 个正整数)中各取出一个整数相加(所以可以得到 kk 个结果),输出前 k 小的和. 这时训练指南上的一道题,这道题的简化版其实在 15 年的广东省省赛出现过,当时是以送分题的形式出现的,可我还是没能做出来,归根到底还是看书不够,接触的题型不够多. *************************************************************大白书上的讲解开始*******