문제) 백준 - 자료 구조 - 탑
https://www.acmicpc.net/problem/2493
2493번: 탑
첫째 줄에 탑의 수를 나타내는 정수 N이 주어진다. N은 1 이상 500,000 이하이다. 둘째 줄에는 N개의 탑들의 높이가 직선상에 놓인 순서대로 하나의 빈칸을 사이에 두고 주어진다. 탑들의 높이는 1
www.acmicpc.net
스택을 이용해 해결했습니다. 먼저 N부터 1번째까지 스택에 넣어주면서 만약 신호를 받을 수 있는 탑이 들어올 경우 answer에 번호를 적습니다. 반복문이 끝난 후에도 스택에 남아 있는 경우는 신호를 받지 못한 경우이므로, answer에 0으로 저장합니다.
C++ 소스코드)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (int i = N; i >= 1; --i) { | |
while (!s.empty()&& numbers[s.top()] <= numbers[i]) { | |
answer[s.top()] = i; | |
s.pop(); | |
} | |
s.push(i); | |
} | |
while (!s.empty()) { | |
answer[s.top()] = 0; | |
s.pop(); | |
} |
Full Code)
https://github.com/Chocochip101/BOJ_Solution/blob/main/Problem%201000~4999/2493_%ED%83%91.cpp
GitHub - Chocochip101/BOJ_Solution: BOJ Solutions
BOJ Solutions. Contribute to Chocochip101/BOJ_Solution development by creating an account on GitHub.
github.com
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 2467번 - 용액 (C++) 문제 및 풀이 (0) | 2021.12.08 |
---|---|
[백준] 17298번 - 오큰수 (C++) 문제 및 풀이 (0) | 2021.12.06 |
[백준] 16194번 - 카드 구매하기 2 (C++) 문제 및 풀이 (0) | 2021.12.03 |
[백준] 5397번 - 키로거 (C++) 문제 및 풀이 (0) | 2021.11.29 |
[백준] 1325번 - 효율적인 해킹 (C++) 문제 및 풀이 (0) | 2021.11.29 |
댓글