본문 바로가기
PS/BOJ

[백준] 6198. 옥상정원 꾸미기(c++)

by backend 개발자 지망생 2025. 1. 31.

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

애먹어서 바로 리뷰한다..

입력값의 범위를 제대로 보지 않고 987654321를 최댓값으로 넣었다가 혼쭐이 났다. (경계값 분석이 역시나 중요하구나)

추가적으로 이전 코테 문제(https://www.acmicpc.net/problem/2493)에서 +된 부분은 마지막에 flush 해줘야 하는 부분이 달랐다.

 


#include<iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <climits>
#include <queue>
#include <set>
#include <cmath>
#include <stack>
#include <list>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;

    // 입력값 받을 배열
    int arr[80002];
    long long int answer = 0;
    // 스택
    stack<pair<int, int> > stk;

    cin >> n;

    stk.push(make_pair(1087654321, 0));

    for (int i = 1; i <= n; i++) {
        cin >> arr[i];
        while (stk.top().first <= arr[i]) {
            answer += i - stk.top().second - 1;
            stk.pop();
        }
        stk.push(make_pair(arr[i], i));
    }

    while (stk.top().first < 1087654321) {
        answer += n - stk.top().second;
        stk.pop();
    }

    cout << answer << " ";

    return 0;
}

 

 

'PS > BOJ' 카테고리의 다른 글

[백준] 2493. 탑(c++)  (0) 2025.02.03
[백준] 3015. 오아시스 재결합(c++)  (0) 2025.02.02
[백준] 1874. 스택 수열(c++)  (0) 2025.01.26
[백준] 10773. 제로(c++)  (0) 2025.01.26
[백준] 10828. 스택(c++)  (0) 2025.01.26