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

15-YIM2UL2ET #48

Merged
merged 3 commits into from
Jun 27, 2024
Merged

15-YIM2UL2ET #48

merged 3 commits into from
Jun 27, 2024

Conversation

YIM2UL2ET
Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET commented Apr 9, 2024

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

BOJ 14719 - ๋น—๋ฌผ

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

๊ธฐ์–ต์ด ์•ˆ๋‚จ..

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

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

1 2

๊ณ ์ธ ๋น—๋ฌผ์˜ ์–‘์„ ์ถœ๋ ฅํ•œ๋‹ค. ๊ฐ€์žฅ ๋†’์€ ๋†’์ด 'h'์™€, n์ด ์ฃผ์–ด์ง€๊ณ , ์ขŒ์ธก๋ถ€ํ„ฐ ๊ฐ๊ฐ์˜ ๋†’์ด๊ฐ€ n๋ฒˆ ์ฃผ์–ด์ง„๋‹ค.

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

๋จธ๋ฆฟ์†์œผ๋กœ ์ƒ๊ฐํ•˜์ž.

์›๋ž˜๋Š” ์ขŒ, ์šฐ์ธก์˜ ๋†’์ด ์ฐจ์ด๋ฅผ ์ด์šฉํ•ด์„œ ํ’€๋ ค๊ณ  ํ–ˆ์—ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ๋†’์ด๊ฐ€ ์™œ ์ฃผ์–ด์ฃผ์—ˆ์„๊นŒ..? ์ƒ๊ฐํ–ˆ๋‹ค.
๋†’์ด๋ฅผ ์ด์šฉํ•ด์„œ ํ’€ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ?

๋†’์ด๋ฅผ ํ•œ ์นธ์”ฉ ๋น„๊ตํ•˜์ž.
ํ•ด๋‹น ๋†’์ด๋ณด๋‹ค ํฐ ๋†’์ด๊ฐ€ 2๊ฐœ ์ด์ƒ ์žˆ๋‹ค๋ฉด, ์•ž ๋’ค๋กœ ์œ„์น˜๋ฅผ ์ฐพ๊ณ  ๊ทธ ์‚ฌ์ด์— ๋‚ฎ์€ ๋†’์ด๊ฐ€ ์žˆ๋Š” ๊ณณ์„ count ํ•ด์„œ result++
KakaoTalk_20240410_144337604

3. ์ˆ˜๋„ ์ฝ”๋“œ

์ž…๋ ฅ ๋ถ€๋ถ„์€ ์ œ์™ธ.

for i = 0 ~ ์ตœ๊ณ  ๋†’์ด {
    cnt = i๋ณด๋‹ค ํฐ ๋†’์ด์˜ ์ˆ˜
    if (cnt > 1) {
        front = i๋ณด๋‹ค ํฐ ๋†’์ด ์ค‘ ๊ฐ€์žฅ ์ขŒ์ธก์— ์žˆ๋Š” ๊ฒƒ์˜ ์œ„์น˜
        back = i๋ณด๋‹ค ํฐ ๋†’์ด ์ค‘ ๊ฐ€์žฅ ์šฐ์ธก์— ์žˆ๋Š” ๊ฒƒ์˜ ์œ„์น˜
        result += ์ด ๊ธธ์ด - back - front - cnt
    }
}

result ์ถœ๋ ฅ

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

#include <iostream>
#include <algorithm>
#include <vector>

int main(void) {
    int height, width, res = 0;

    std::cin >> height >> width;
    std::vector <int> vec(width);
    for (int i = 0; i < width; i++) std::cin >> vec[i];
    
    for (int i = height; i >= 0; i--) {
        int cnt = std::count_if(vec.begin(), vec.end(), [&i](int v) -> bool {return v>=i;});
        if (cnt > 1) {
            int front = std::find_if(vec.begin(), vec.end(), [&i](int v) -> bool {return v>=i;}) - vec.begin();
            int back = std::find_if(vec.rbegin(), vec.rend(), [&i](int v) -> bool {return v>=i;}) - vec.rbegin();
            res += width - back - front - cnt;
        }
    }
    
    std::cout << res;
    return 0;
}

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

์˜ค๋žœ๋งŒ์— ์ˆœ์ˆ˜ํ•˜๊ฒŒ ์•„๋ฌด ์ง€์‹ ์—†์ด ๋จธ๋ฆฌ๋งŒ ์จ์„œ ํ•ด๊ฒฐํ–ˆ์—ˆ๋˜ ๋ฌธ์ œ.
์ด๋Ÿฐ ๋‘๋‡Œ๋„ ํ™œ์„ฑํ™” ํ•ด์ฃผ๋Š” ๊ฒƒ๋„ ๋‚˜์˜์ง€ ์•Š๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

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.

์ฒ˜์Œ ๋ณด๋ฉด ์‰ฝ๊ฒŒ ํ’€๋ฆฌ์ง€๋Š” ์•Š์„ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.
vector๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž˜ ์ž‘์„ฑํ•˜์‹  ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์˜€์Šต๋‹ˆ๋‹ค.

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

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