λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

Algorithm/BOJ

[BOJ] νžˆμŠ€ν† κ·Έλž¨(1725)

[λ°±μ€€(BOJ)] νžˆμŠ€ν† κ·Έλž¨(1725) C++

문제 : BOJ_1725번 νžˆμŠ€ν† κ·Έλž¨

문제 μ„€λͺ…

뢄할정볡

유λͺ…ν•œ 뢄할정볡 λ¬Έμ œμž…λ‹ˆλ‹€.

μŠ€ν…μ„ ν™œμš©ν•˜μ—¬ O(N) μ•ˆμ— ν‘ΈλŠ” 방법도 μ‘΄μž¬ν•œλ‹€κ³  λ“€μ—ˆμœΌλ‚˜, 뢄할정볡을 μ΄μš©ν•΄ ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€.

μž„μ˜μ˜ 크기λ₯Ό 가진 간격이 1인 λ§‰λŒ€ 그리프듀이 λŠ˜μ–΄μ Έμžˆκ³ , λ‚΄λΆ€μ—μ„œ κ°€μž₯ 큰 넓이λ₯Ό ꡬ해야 ν•˜λŠ”λ°

μ΄λ•Œ, λ§‰λŒ€κ·Έλž˜ν”„λŠ” μ—°μ†ν•œ λ§‰λŒ€κ·Έλž˜ν”„λ₯Ό 선택해야 ν•˜κ³ , 높이 값은 μ„ νƒλœ λ§‰λŒ€κ·Έλž˜ν”„λ“€ 쀑 μ΅œμ†Œ 높이, 간격은 λ§‰λŒ€κ·Έλž˜ν”„ ν•˜λ‚˜μ˜ 간격이 1μ΄λ―€λ‘œ, λ§‰λŒ€κ·Έλž˜ν”„μ˜ κ°œμˆ˜μž…λ‹ˆλ‹€.


Solution

λ§‰λŒ€κ·Έλž˜ν”„λ“€μ„ 계속 μ ˆλ°˜μ”© λ‚˜λˆ μ„œ 각각 λ‚˜λˆ μ§„ μ ˆλ°˜λ§ˆλ‹€μ˜ κ²½μš°μ—μ„œ μ΅œλŒ€ 크기λ₯Ό ꡬ해야 ν•©λ‹ˆλ‹€.

μ΄λ•Œ, μ΅œλŒ€ ν¬κΈ°λŠ” μ²˜μŒμ— λ§‰λŒ€κ·Έλž˜ν”„λ₯Ό 쀑앙을 κΈ°μ€€μœΌλ‘œ 작고, 높이가 큰 μͺ½(같은 μͺ½μ΄λ©΄ 아무 λ°λ‚˜)으둜 ν™•μž₯ν•΄λ‚˜κ°€λ©°,

ν™•μž₯ν•œ λͺ¨λ“  경우의 μˆ˜λ§ˆλ‹€ λ„“μ΄μ˜ μ΅œλŒ“κ°’μ„ κ°±μ‹ ν•΄ μ£Όλ©° λͺ¨λ“  λ§‰λŒ€κ·Έλž˜ν”„ 전체λ₯Ό ν™•μž₯ν•˜λ©΄ λΆ„ν• λœ κ΅¬μ—­μ—μ„œ μ΅œλŒ“κ°’μ΄ 보μž₯λ©λ‹ˆλ‹€.

μ΄λ•Œ, λ§‰λŒ€κ·Έλž˜ν”„λ₯Ό ν•œ λ²ˆμ”©λ§Œ ν™•μž₯ν•΄ μ£Όλ―€λ‘œ λΆ„ν• λœ μƒνƒœμ—μ„œ 전체 λ§‰λŒ€κ·Έλž˜ν”„λ₯Ό νƒμƒ‰ν•˜λŠ” 것은 N번이고,

전체 크기λ₯Ό 계속 절반 μ”© λ‚˜λˆ„κΈ° λ•Œλ¬Έμ— 전체 κ°œμˆ˜κ°€ N이라면, N이 1이 될 λ•ŒκΉŒμ§€ 계속 2둜 λ‚˜λˆ„μ–΄ 진행해야 ν•˜λ―€λ‘œ,

μ‹œκ°„ λ³΅μž‘λ„λŠ” O(logN * N) μž„μ„ μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.


Description

κ΅¬ν˜„μ΄ λ³΅μž‘ν•˜λ―€λ‘œ, 두 개의 ν•¨μˆ˜λ‘œ λ‚˜λˆ μ„œ ν’€μ–΄μ€¬μŠ΅λ‹ˆλ‹€.


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int N, value[100002], area;
int printArea(int funcLeft, int funcRight);
void recursion(int le, int ri);
int main(void) {
    memset(value, -1, sizeof(value));
    cin >> N;
    for (int i = 1; i <= N; i++) {
        cin >> value[i];
    }
    recursion(1, N);
    cout << area << '\n';
    return 0;
}
void recursion(int le, int ri) {
    // 절반 μ”© λΆ„ν• ν•˜λŠ” μž¬κ·€ ν•¨μˆ˜
    if (le == ri) {
        area = max(area, value[le]);
        return;
    }
    area = max(area, printArea(le, ri));
    int mid = (le+ri)/2;
    if ((ri - le) == 1) {
        area = max(area, value[ri]);
        return;
    }
    recursion(le, mid);
    recursion(mid, ri);
}
int printArea(int funcLeft, int funcRight) {
    // λΆ„ν• λœ λ§‰λŒ€ κ·Έλž˜ν”„λ“€ 쀑 μ΅œλŒ€ 넓이λ₯Ό κ΅¬ν•΄μ£ΌλŠ” ν•¨μˆ˜
    int returnArea = 0;
    int cnt = 1;
    int mid = (funcRight + funcLeft) / 2;
    int le = mid;
    int ri = mid;
    int nowHeight = value[mid];
    bool endLe = false;
    bool endRi = false;
    while (true) {
        if (le <= funcLeft && ri >= funcRight) break;
        if (le == funcLeft) endLe = true;
        if (ri >= funcRight) endRi = true;
        cnt++;
        int nowArea;
        if (endLe) {
            ri++;
            nowHeight = min(value[ri], nowHeight);
        }
        else if (endRi) {
            le--;
            nowHeight = min(value[le], nowHeight);
        }
        else if ((value[le - 1] >= value[ri + 1]) && !endLe) {
            le--;
            nowHeight = min(value[le], nowHeight);
        }
        else {
            ri++;
            nowHeight = min(value[ri], nowHeight);
        }
        nowArea = cnt * nowHeight;
        returnArea = max(returnArea, nowArea);
    }
    return returnArea;
}

'Algorithm > BOJ' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[BOJ] νœ΄λŒ€ν° 자판(5070)  (0) 2022.03.07
[BOJ] μ „ν™”λ²ˆν˜Έ λͺ©λ‘(5052)  (0) 2022.03.07
[BOJ] κ³±μ…ˆ(1629)  (0) 2022.03.06
[BOJ] νšŒμ˜μ‹€λ°°μ •(1931)  (0) 2022.03.06
[BOJ] 숫자 야ꡬ(2503)  (0) 2022.03.06