前K小数 码蹄集

题目来源:码蹄集

题目描述:

在这里插入图片描述
在这里插入图片描述
最直接的思路是先将所有的和计算出来,并进行排序,然后输出前k小的数。

Python代码直接思路(会超内存):

n, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

sums = []
for i in range(n):
    for j in range(n):
        sums.append(a[i] + b[j])

sums.sort()
for i in range(k):
    print(sums[i], end=" ")

Python代码优化实现:

import heapq

class Node:
    def __init__(self, sum_, b):
        self.sum = sum_
        self.b = b
        
    def __lt__(self, other):
        return self.sum < other.sum

n, k = map(int, input().split())

a = list(map(int, input().split()))
b = list(map(int, input().split()))

a.sort()
b.sort()

min_list = []
for i in range(n):
    heapq.heappush(min_list, Node(a[i] + b[0], 0))

res = []
for i in range(k):
    node = heapq.heappop(min_list)
    res.append(node.sum)
    temp_b = node.b
    if temp_b + 1 < n:
        heapq.heappush(min_list, Node(node.sum - b[temp_b] + b[temp_b + 1], temp_b + 1))

print(" ".join(str(x) for x in res))

C++代码实现:

代码参考链接:https://betheme.net/dashuju/49396.html?action=onClick

#include<bits/stdc++.h>
using namespace std;

const int maxSize = 10100;
int a[maxSize];
int b[maxSize];

struct Node{
    int sum, b;
    Node(int sum, int b):sum(sum), b(b){}
    bool operator < (const Node &b) const{
        return sum > b.sum;
    }
};

priority_queue<Node> minList;

int main(){
    int n, k;
    cin >> n >> k;

    for(int i = 0; i < n; i++){
        cin >> a[i];
    }

    for(int i = 0; i < n; i++){
        cin >> b[i];
    }

    sort(a, a + n);
    sort(b, b + n);

    for(int i = 0; i < n; i++){
        minList.push(Node(a[i] + b[0], 0));
    }

    for(int i = 0; i < k; i++){
        Node node = minList.top();
        minList.pop();
        cout << node.sum << " ";
        int tempB = node.b;
        if(tempB + 1 < n){
            minList.push(Node(node.sum - b[tempB] + b[tempB + 1], tempB + 1));
        }
    }

    return 0;
}

Java代码实现:

import java.util.*;

public class Main {
    static class Node implements Comparable<Node> {
        int sum;
        int b;

        public Node(int sum, int b) {
            this.sum = sum;
            this.b = b;
        }

        @Override
        public int compareTo(Node other) {
            return Integer.compare(this.sum, other.sum);
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();

        int[] a = new int[n];
        int[] b = new int[n];

        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        for (int i = 0; i < n; i++) {
            b[i] = sc.nextInt();
        }

        Arrays.sort(a);
        Arrays.sort(b);

        PriorityQueue<Node> minList = new PriorityQueue<>();

        for (int i = 0; i < n; i++) {
            minList.add(new Node(a[i] + b[0], 0));
        }

        for (int i = 0; i < k; i++) {
            Node node = minList.poll();
            System.out.print(node.sum + " ");
            int tempB = node.b;
            if (tempB + 1 < n) {
                minList.add(new Node(node.sum - b[tempB] + b[tempB + 1], tempB + 1));
            }
        }
    }
}

代码提交测试结果:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Magneto_万磁王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值