题解——UVA11997 K Smallest Sums

题面

  背景

  输入

  

  输出

    

翻译(渣自翻)

  给定K个包含K个数字的表,要求将其能产生的\( k^{k} \)个值中最小的K个输出出来

题解

k路归并问题的经典问题

可以转化为二路归并问题求解

考虑A[],B[]两个有序数组

使用堆,记录一些二元组\( (x,y) \),x表示值,y表示对应的b的下标,因为我们是把b合并到a上,所以我们能够根据记录的下标推出后面的值

然后两两合并

所以就很简单

放代码

#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
struct asshole{
    int w,t;
    bool operator < (const asshole b) const{
        return w>b.w;
    }
};
priority_queue<asshole> q;
int kz[10000],kk[10000];
int n;
void merge(int *a,int *b,int *c){
    while(!q.empty())
        q.pop();
    for(int i=1;i<=n;i++)
        q.push((asshole){a[i]+b[1],1});
    for(int i=1;i<=n;i++){
        asshole in = q.top();
        q.pop();
        a[i]=in.w;
        int pos=in.t;
        if(pos+1<=n)
            q.push((asshole){in.w-b[pos]+b[pos+1],pos+1});
    }
}
int main(){
    while(scanf("%d",&n)==1){
        for(int i=1;i<=n;i++)
            scanf("%d",&kz[i]);
        sort(kz+1,kz+n+1);
        for(int j=1;j<=n-1;j++){
        for(int i=1;i<=n;i++)
            scanf("%d",&kk[i]);
        sort(kk+1,kk+n+1);
        merge(kz,kk,kz);
        }
        for(int i=1;i<=n-1;i++)
            printf("%d ",kz[i]);
        printf("%d",kz[n]);
        printf("\n");
    }
}

原文地址:https://www.cnblogs.com/dreagonm/p/9484130.html

时间: 2024-11-08 19:33:31

题解——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. 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

uva11997 K Smallest Sums&amp;&amp;UVALive 3135 Argus(优先队列,多路归并)

#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<algorithm> #include<stack> #in

(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

传送门 Description Input Output Translation · 给定k个长度为k的数组,把每个数组选一个元素加起来,这样共有kk种可能的答案,求最小的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 Hint k<=750 Solution 显然可以一行一行做,同时如果S+now[j]最小,需要S最小.即我们只需要记录前i行的最小的k个ans,分别与当前行的k个相加,从k2个an