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

8-wnsmir #32

Merged
merged 1 commit into from
Dec 10, 2024
Merged

8-wnsmir #32

merged 1 commit into from
Dec 10, 2024

Conversation

wnsmir
Copy link
Collaborator

@wnsmir wnsmir commented Nov 25, 2024

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

https://www.acmicpc.net/problem/1654

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

45min

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

์ตœ์†Œ๊ธธ์ด๊ฐ€ 1cm, ์ตœ๋Œ€๊ธธ์ด๋Š” ๋žœ์„ ๋“ค ์ค‘ ๊ฐ€์žฅ ๊ธด ๋žœ์„ ์ด๋ฏ€๋กœ ๋จผ์ € ์ž๋ฅผ์ˆ˜ ์žˆ๋Š” ๋ฒ”์œ„๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค.

์ดํ›„ ์ค‘๊ฐ„๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋ชจ๋“  ๋žœ์„ ์„ ์ž๋ฅธ ํ›„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋žœ์„ ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฐœ์‚ฐํ•œ๋‹ค.

N๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด, ๋” ๊ธด ๊ธธ์ด๋ฅผ ์‹œ๋„ํ•˜๊ธฐ ์œ„ํ•ด ์ค‘์•™๊ฐ’๋ณด๋‹ค ๋” ํฐ ๋ฒ”์œ„๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
N๋ณด๋‹ค ์ž‘์œผ๋ฉด ์ค‘์•™๊ฐ’๋ณด๋‹ค ์ž‘์€ ๋ฒ”์œ„๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.

์ดํ›„ ์ค‘์•™๊ฐ’์„ ์ €์žฅํ•œ๋‹ค.

์ฆ‰ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ €์žฅํ•œ ๊ฐ’์ด ์ตœ์ ์˜ ๊ธธ์ด๊ฐ€ ๋œ๋‹ค.

์ฆ‰ ๋ฒ”์œ„์ง€์ • ์ฆ‰ 1๊ณผ ๋์ ์„ ์ง€์ •ํ•ด์ฃผ๋Š”๊ฒƒ๋งŒ ์‹œ์ž‘ํ•ด๋„ ์ ˆ๋ฐ˜์ด์ƒ์˜ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ์‰ฌ์šด ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

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

ํ•œ์‹œ๊ฐ„ ๋„˜๊ฒŒ ๊ณ ๋ฏผ๋งŒ ํ•˜๋‹ค๊ฐ€... ๋ ˆํผ๋Ÿฐ์Šค ๋ดค๋„ค์š”..
์ด๋ถ„ํƒ์ƒ‰ ๋ฌธ์ œ๋ฅผ ์•ˆํ’€์–ด๋ด์„œ ๊ทธ๋Ÿฐ๊ฐ€ ์ „ํ˜€ ๊ฐ์ด ์•ˆ์žกํžˆ๋”๊ตฐ์œ 

์ฒ˜์Œ์—๋Š” max heap์—์„œ top์„ ๊บผ๋‚ด๋ฉฐ
๋ฐ˜์„ ๊ฐˆ๋ผ์„œ max heap์— ๋‹ค์‹œ ๋„ฃ๊ณ  N++ํ•˜๋Š” ...
๊ทธ๋ฆฌ๊ณ  ๊ทธ๊ฑธ while(N != K) ์—์„œ ์ง„ํ–‰ํ•˜๋Š”... ๊ทธ๋Ÿฐ๊ฑธ ์ƒ์ƒํ–ˆ๋Š”๋ฐ ์˜ค์ ์ด ๋„ˆ๋ฌด ๋งŽ๋”๊ตฐ์š”

๊ทธ๋ž˜๋„ ๋ฌธ์ œ๋ฅผ ์˜ค๋ž˜ ๊ณ ๋ฏผํ•ด์„œ์ธ์ง€,
์ •๋‹ต ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์ดํ•ดํ•˜๋Š” ๊ฑด ๋น ๋ฅด๊ธดํ–ˆ์Šต๋‹ˆ๋‹ค.

์ƒ์†Œํ•œ ์ด๋ถ„ํƒ์ƒ‰ ๋ฌธ์ œ๋ฅผ ์†Œ๊ฐœํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ท!

CPP CODE
#include <algorithm>
#include <iostream>

using namespace std;

unsigned int list[10000];

int main() {
    unsigned int N, K;
    cin >> K >> N;

    unsigned int maxRange = 0;
    for (int i = 0; i < K; i++) {
        cin >> list[i];
        maxRange = max(maxRange, list[i]);
    }

    unsigned int result = 0;
    unsigned int left = 1;
    unsigned int right = maxRange;
    unsigned int mid;

    while (left <= right) {
        unsigned int temp = 0;
        mid = (left + right) / 2;

        for (int i = 0; i < K; i++) {
            temp += list[i] / mid;
        }

        if (temp >= N) {
            left = mid + 1;
            result = max(result, mid);
        } 
        else right = mid - 1;
    }
    cout << result << '\n';

    return 0;
}

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

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

๋“œ๋””์–ด ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.!! ์ด์ง„ํƒ์ƒ‰์ด๋ž€ ๊ฒƒ์„ ์•Œ์•„๋„ ์ข€ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋„ค์š”

ํŒŒ์ด์ฌ์€ ์–ด๋–ค์ง€ ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ cpp ์—์„œ๋Š” int๋งŒ ํ•˜๋‹ˆ๊ฐ€ ๋ฌธ์ œ๊ฐ€ ํ‹€๋ฆฌ๋”๊ตฐ์š”.. ์ฒ˜์Œ์— ๋ณ€์ˆ˜๋ฅผ int๋กœ ์žก๊ณ  INT_MIN์œผ๋กœ ์„ค์ •ํ–ˆ๋Š”๋ฐ ๋‚˜๋ˆ„๊ธฐ๋ฅผ ํ•˜๋Š” ๊ณผ์ •์—์„œ (s + e)๊ฐ€ ์Œ์ˆ˜๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ด ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค. (์•„๋‹ˆ๋ฉด int ๊ฐ’์„ ๋„˜์–ด๊ฐ”๋˜๊ฐ€..)

ํ•˜๋‚˜ ๊ถ๊ธˆํ•œ ์ ์€ answer = max(answer, mid);๋กœ ์„ค์ •ํ•˜๋˜๊ฐ€ answer = mid;๋กœ ์„ค์ •ํ•˜๋˜๊ฐ€ ๋‹ต์€ ๊ฐ™์€ ๊ฒƒ ๊ฐ™๋„ค์š”. ์™œ ์ด๋Ÿฐ ๊ฑธ๊ฐ€์š”..? ์ตœ์ ์˜ ๊ฐ’์„ ์ฐพ์•„๊ฐ€๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ ๋Š” ํ•˜๋‚˜ ์ดํ•ด๊ฐ€ ๊ฐ€์งˆ ์•Š๋„ค์š” ๐Ÿฅฒ

code
int main() {
    unsigned int n, k;
    unsigned int maxLine = 0;
    unsigned int answer = 0;
    unsigned int s = 1, e = maxLine;
    unsigned int mid, cnt;

    // init data ..

    while (s <= e) {
        mid = (s + e) / 2;
        cnt = 0;

        for (int i=0; i<k; i++) {
            cnt += lines[i] / mid;
        }

        if (cnt >= n) {
            s = mid + 1;
            answer = max(answer, mid);
        } else {
            e = mid - 1;
        }
    }
    cout << answer;
}

@g0rnn g0rnn merged commit 2c03b01 into main Dec 10, 2024
1 check passed
@g0rnn g0rnn deleted the 8-wnsmir branch December 10, 2024 05:51
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