[ 백준 ] 14719 / 빗물

金弘均·2021년 9월 15일
0

Baekjoon Online Judge

목록 보기
52/228
post-thumbnail

# Appreciation

/*
 * Problem :: 14719 / 빗물
 *
 * Kind :: Simulation
 *
 * Insight
 * -     |     |
 *       |   | |
 *       | | | |
 *   i : 0 1 2 3
 *   + i=0 에 빗물이 고이는 양을
 *     어떻게 하면 쉽게 알 수 있을까?
 *     # i=0 왼편에 있는 가장 높은 블록의 길이=lmh(left max height)
 *       i=0 오른편에 있는 가장 높은 블록의 길이=rmh(right max height)
 *       i=0 블록의 길이=h
 *       -> i=0 에 빗물이 고이는 양 = max(0, min(lmh, rmh)-h)
 *          => 이렇게 i=0~(W-1) 까지 알아보면 되겠다!
 */

# Code

//
//  BOJ
//  ver.C++
//
//  Created by GGlifer
//
//  Open Source

#include <iostream>

using namespace std;

#define endl '\n'

// Set up : Global Variables
/* None */

// Set up : Functions Declaration
/* None */


int main()
{
    // Set up : I/O
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // Set up : Input
    int H, W;
    cin >> H >> W;
    int A[W]; /* 위치에 따른 블록이 쌓인 높이 */
    for (int i=0; i<W; i++)
        cin >> A[i];

    // Process
    /* LMH[i] = i 왼편에서 가장 높은 블록의 길이
     * RMH[i] = i 오른편에서 가장 높은 블록의 길이 */
    int LMH[W], RMH[W];
    LMH[0] = 0;
    for (int i=1; i<=W-1; i++) {
        LMH[i] = max(A[i-1], LMH[i-1]);
    }
    RMH[W-1] = 0;
    for (int i=W-2; i>=0; i--) {
        RMH[i] = max(A[i+1], RMH[i+1]);
    }

    int ans = 0; /* 고이는 빗물의 총량 */
    for (int i=1; i<=W-2; i++) {
        int lmh = LMH[i], rmh = RMH[i];
        ans += max(0, min(lmh, rmh)-A[i]);
    }

    // Control : Output
    cout << ans << endl;
}

// Helper Functions
/* None */
profile
이런 미친 게임을 봤나! - 옥냥이

0개의 댓글