[백준/BOJ] 1863. 스카이라인 쉬운거 [Gold 5]

jychan99·2022년 2월 2일
0
post-thumbnail
  1. 스카이라인 쉬운거

문제출처 : https://www.acmicpc.net/problem/1863

code

#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;

int main()
{
    int i, n, x, y, buildings = 0, temp;
    cin >> n;
    vector<int>skyline(n);
    stack<int>stk;
    for (i = 0; i < n; i++)
        cin >> x >> skyline[i];

    for (i = 0; i < n; i++)
    {
        temp = skyline[i];
        while (!stk.empty() && stk.top() > temp)
        {
            if (stk.top())
                buildings++;
            stk.pop();
        }

        if (!stk.empty() && stk.top() == temp)
            continue;

        stk.push(temp);
    }

    while (!stk.empty())
    {
        if (stk.top())
            buildings++;
        stk.pop();
    }

    cout << buildings;

    return 0;
}

스택은 큐보단 조금 더 쉬운 것 같긴한데, 아직 코드구현이 미숙하다.
알고리즘은 어떻게 해야하는지 알것같은데, 평소 안쓰던 while문을 쓰다보니 좀 헷갈릴때가 많은듯.... 좀 더 많은 문제를 접하다보면 익숙해지지 않을까...?? ㅠㅠ

profile
내가 지금 두려워 하고 있는 일이 바로 내가 지금 해야 할 일이다. 🐥

0개의 댓글