Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14-YIM2UL2ET #43

Merged
merged 3 commits into from
Jun 27, 2024
Merged

14-YIM2UL2ET #43

merged 3 commits into from
Jun 27, 2024

Conversation

YIM2UL2ET
Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET commented Apr 1, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

BOJ 11286 - ์ ˆ๋Œ“๊ฐ’ ํž™

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

์•ฝ 5m (์ด์ „์— ํ‘ผ ํ•˜์œ„ ๋ฌธ์ œ๊นŒ์ง€ ํ•ฉํ•˜๋ฉด 30m)

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

1. ๋ฌธ์ œ ์š”์•ฝ

n๊ณผ n๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ heap ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ทœ์น™๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๊ทœ์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • 0์„ ์ž…๋ ฅ ๋ฐ›์œผ๋ฉด heap์˜ root๋ฅผ ์ถœ๋ ฅํ•œ ํ›„ ์‚ญ์ œํ•œ๋‹ค.
  • 0์ด ์•„๋‹Œ ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ฉด heap์— ๊ทธ ์ˆซ์ž๋ฅผ ๋„ฃ๋Š”๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  heap์€ ์ ˆ๋Œ“๊ฐ’์ด ์ œ์ผ ์ž‘์€ ๊ฒƒ์„ root์— ๋„ฃ์–ด์•ผ ํ•œ๋‹ค. ์ ˆ๋Œ“๊ฐ’์ด ๊ฐ™์œผ๋ฉด ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ root์— ๋„ฃ๋Š”๋‹ค.

2. heap์ด๋ž€?

heap: ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งŒ๋“  ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ ๊ตฌ์กฐ์˜ ์ž๋ฃŒ๊ตฌ์กฐ.
๊ฐ’์„ ์ž์ฃผ ์‚ฝ์ž…ํ•˜๋ฉด์„œ, ์ผ์ • ์กฐ๊ฑด์— ๋”ฐ๋ฅธ ์ตœ์†Ÿ๊ฐ’ ๋˜๋Š” ์ตœ๋Œ“๊ฐ’ ๋งŒ์„ ํ•„์š”๋กœ ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
heap ์ž๋ฃŒ๊ตฌ์กฐ์˜ ์‚ฝ์ž… ๋ฐ ์‚ญ์ œ ์—ฐ์‚ฐ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(logN)์ด๋‹ค.

ํŠธ๋ฆฌ์— ๊ด€ํ•œ ์šฉ์–ด๋ฅผ ๋ชจ๋ฅธ๋‹ค๋ฉด ์ฐธ์กฐ: ๋งํฌ
์‹œ๊ฐ ์ž๋ฃŒ๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค๋ฉด ์ฐธ์กฐ: ๋งํฌ

heap์˜ ์‚ฝ์ž… ๋ฐฉ๋ฒ•

  1. ํŠธ๋ฆฌ์˜ ๋์— ๋…ธ๋“œ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.
  2. ์‚ฝ์ž…ํ–ˆ๋˜ ๋…ธ๋“œ์™€, ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ๊ทœ์น™์— ๋”ฐ๋ผ ๋น„๊ตํ•œ๋‹ค.
  3. ๊ทœ์น™์— ๋งž์œผ๋ฉด ์„œ๋กœ์˜ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค.
  4. ์‚ฝ์ž…ํ–ˆ๋˜ ๋…ธ๋“œ๊ฐ€ ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ๋˜๊ฑฐ๋‚˜, ๊ทœ์น™์— ๋งž์„ ๋•Œ ๊นŒ์ง€ 3๋ฒˆ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

์ตœ์†Œ ํž™ ๊ธฐ์ค€์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

void cre_heap(std::queue <int> heap, int v) {
    int k = heap.size();
    heap.push(v);
    while (k > 1) {
        if (heap[k] < heap[k/2]) {
            std::swap(heap[k], heap[k/2]);
            k /= 2;
        }
        else break;
    }
    return;
}

heap์˜ ์‚ญ์ œ ๋ฐฉ๋ฒ•

  1. ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œ ํ›„ ๋งจ ๋ ๋…ธ๋“œ๋ฅผ ๋ฃจํŠธ ๋…ธ๋“œ์— ์‚ฝ์ž…ํ•œ๋‹ค.
  2. ๋‘ ์ž์‹ ๋…ธ๋“œ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๊ทœ์น™์— ๋” ๋ถ€ํ•ฉํ•˜๋Š” ๋…ธ๋“œ์™€ ๋ฐ”๊พผ๋‹ค. ๋งŒ์•ฝ ๋‘˜ ๋‹ค ๊ทœ์น™์— ๋ถ€ํ•ฉํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ทธ๋งŒ๋‘”๋‹ค.
  3. 2๋ฒˆ์„ ๋ฆฌํ”„ ๋…ธ๋“œ์— ๋„๋‹ฌํ•  ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

์ตœ์†Œ ํž™ ๊ธฐ์ค€์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

void del_heap(std::queue <int> heap) {
    heap[1] = heap.back(), heap.pop();
    int size = heap.size(), k = 1;
    while (k * 2 <= size) {
        if (k * 2 + 1 < size && heap[k*2+1] < heap[k*2]) k = k*2+1;
        else k = k*2;

        if (heap[k/2] < heap[k]) std::swap(heap[k/2], heap[k]);
        else break;
    }
    return;
}

3. ๋ฌธ์ œ ํ’€์ด

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๊ตฌํ˜„ํ–ˆ๋˜ heap์˜ ๊ทœ์น™์„ ์กฐ๊ธˆ๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋œ๋‹ค.
๋ฌธ์ œ์—์„œ๋Š” "์ ˆ๋Œ“๊ฐ’์ด ์ž‘๊ณ , ๊ฐ™์œผ๋ฉด ์ž‘์€ ๊ฐ’์„ root์— ์‚ฝ์ž…"์„ ํ•˜๋ฉด ๋œ๋‹ค.
์ด๋ฅผ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

// ์‚ฝ์ž… ๋…ผ๋ฆฌ ์—ฐ์‚ฐ
if (abs(heap[k/2]) > abs(heap[k]) || (abs(heap[k/2]) == abs(heap[k]) && heap[k/2] > heap[k]));
//๋ถ€๋ชจ ๋…ธ๋“œ์™€ ์ž์‹ ๋…ธ๋“œ์˜ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ, true์ด๋ฉด swap ํ›„ k*=2;, false์ด๋ฉด break;

// ์‚ญ์ œ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ
if (k * 2 + 1 < heap.size() && (abs(heap[k*2+1]) < abs(heap[k*2]) || (abs(heap[k*2+1]) == abs(heap[k*2]) && heap[k*2+1] < heap[k*2]))) 
// ์ž์‹ ๋…ธ๋“œ๋ผ๋ฆฌ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ, true์ด๋ฉด k = k*2+1, false์ด๋ฉด k = k*2

if (abs(heap[k/2]) > abs(heap[k]) || (abs(heap[k/2]) == abs(heap[k]) && heap[k/2] > heap[k]))
// ๋ถ€๋ชจ ๋…ธ๋“œ์™€ ์ž์‹ ๋…ธ๋“œ์˜ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ, true์ด๋ฉด swap, false์ด๋ฉด break;

4. ์ตœ์ข… ์ฝ”๋“œ

#include <iostream>
#include <vector>

void cre_heap(std::vector <int> &heap, int key) {
    int k = heap.size();
    heap.push_back(key);
    while (k > 1) {
        if (abs(heap[k/2]) > abs(heap[k]) || (abs(heap[k/2]) == abs(heap[k]) && heap[k/2] > heap[k])) {
            std::swap(heap[k], heap[k/2]);
            k /= 2;
        }
        else break;
    }
    return;
}

void del_heap(std::vector <int> &heap) {
    int k = 1;
    heap[1] = heap.back(), heap.pop_back();
    while (k * 2 <= heap.size()) {
        if (k * 2 + 1 < heap.size() && (abs(heap[k*2+1]) < abs(heap[k*2]) || (abs(heap[k*2+1]) == abs(heap[k*2]) && heap[k*2+1] < heap[k*2])))
            k = k*2+1;
        else k = k*2;

        if (abs(heap[k/2]) > abs(heap[k]) || (abs(heap[k/2]) == abs(heap[k]) && heap[k/2] > heap[k])) 
            std::swap(heap[k/2], heap[k]);
        else break;
    }
    return;
}

int main(void) {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    int n, k, x;
    std::vector <int> abs_heap = {0};

    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> x;
        if (abs(x) > 0) cre_heap(abs_heap, x);
        else if (abs_heap.size() > 1) {
            std::cout << abs_heap[1] << '\n';
            del_heap(abs_heap);
        }
        else std::cout << 0 << '\n';
    }
    
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์šฐ์„ ์ˆœ์œ„ ํ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋ฐฐ์šฐ๊ณ , ํž™ ํŠธ๋ฆฌ ๊ตฌํ˜„ํ•ด๋ด„. ์žฌ๋ฐŒ์—ˆ๋‹ค.
์ฐธ๊ณ  ์ž๋ฃŒ: ๋‚˜๋ฌด์œ„ํ‚ค - ํž™ ํŠธ๋ฆฌ
ํ•˜์œ„ ๋ฌธ์ œ: ์ตœ์†Œ ํž™, ์ตœ๋Œ€ ํž™
ํ•ด์•ผ ํ• ๊บผ: ๋ถ€๋ชจ ๋…ธ๋“œ๋ž‘ ์ž์‹ ๋…ธ๋“œ ์ž๊พธ ํ—ท๊น”๋ฆฌ๋Š”๋ฐ ์—ฐ์Šต ๋งŽ์ด ํ•˜๊ธฐ. ์ด์ œ ํž™ STL ๊ณต๋ถ€ํ•˜๊ธฐ.

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํž™์˜ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ ๊ธฐ๋Šฅ์„ ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณด์‹ ๊ฒŒ ์ธ์ƒ์ ์œผ๋กœ ๋‚จ์•˜์Šต๋‹ˆ๋‹ค.
์ด๋ฏธ ๊ตฌํ˜„๋˜์–ด์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๋‹ค์‹œ ์งœ๋ณด๋Š” ๋ฐฉ๋ฒ•์ด ๊ฐ€์žฅ ๊ธฐ์–ต์— ์ž˜ ๋‚จ๊ธด ํ•˜๋”๋ผ๊ณ ์š”

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜

@tgyuuAn tgyuuAn merged commit eee8b46 into main Jun 27, 2024
1 check passed
@tgyuuAn tgyuuAn deleted the 14-YIM2UL2ET branch June 27, 2024 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants