let menu = ["Home", "Algorithms", "CodeHub", "VNOI Statistics"];

Overview

KMIN

solution.md

Bài này ta sort dãy B lại theo thứ tự không giảm. Sau đó sử dụng heap như sau:

  • Khởi tạo heap với M phần tử, mỗi phần tử lần lượt có giá trị \(A_i + B_0\).
  • Lặp K bước, ở mỗi bước lấy ra phần tử nhỏ nhất của heap, in giá trị phần tử này. Sau đó, giả sử phần tử bị lấy ra có giá trị là \(A_i + B_j\), ta thêm \(A_i + B_{j+1}\) vào heap.
main.cpp
Open in Github Download
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

struct pack { int s, i; };
bool operator > (pack a, pack b) {
    return a.s > b.s;
}
typedef priority_queue<pack, vector<pack>, greater<pack>> min_heap;

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int m, n, k; cin >> m >> n >> k;
    vector<int> a(m), b(n);
    for (int &a: a) cin >> a;
    for (int &b: b) cin >> b;

    sort(b.begin(), b.end());

    min_heap heap;
    vector<int> pair(m, 0);
    for (int i=0; i<m; i++) {
        heap.push({a[i]+b[0], i});
    }

    while (k--) {
        pack top = heap.top();
        cout << top.s << '\n';

        int i = top.i;
        heap.pop();
        pair[i] += 1;
        if (pair[i] < n) heap.push({a[i] + b[pair[i]], i});
    }

    return 0;
}
Comments