#include <iostream>
#include <stack>
#define X first
#define Y second
using namespace std;
int N;
stack<pair<int, int>> tower;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N;
tower.push({ 100000001, 0 }); // 넘을 수 없는 최대치의 벽 미리 삽입
for (int i = 1; i <= N; i++) { // 인덱스 출력을 위해 1부터 시작
int height;
cin >> height;
while (tower.top().X < height) // 더 높은 이전타워 탐색
tower.pop();
cout << tower.top().Y << " "; // 더 높은 타워의 레이저 송수신 결과
tower.push({ height, i }); // 현재 타워 스택에 추가
}
}
바킹독님의 소스. 나중에 다시 리벤지 하는걸로
핵심은 스택에 맨 먼저 넘을수 없는 최대높이, 인덱스 0의 값을 준다
#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int n;
vector<int> towers;
vector<int> temp_towers;
vector<int> answer;
cin >> n;
int data;
for (int i = 0; i < n; ++i)
{
cin >> data;
towers.push_back(data);
}
int count = 0;
while (true)
{
int tower = towers.back();
towers.pop_back();
temp_towers.push_back(tower);
//count += 1;
if (towers.empty())
{
break;
}
while (!temp_towers.empty() && temp_towers.back() < towers.back())
{
answer.push_back(towers.size());
temp_towers.pop_back();
}
}
while (!temp_towers.empty())
{
answer.push_back(0);
temp_towers.pop_back();
}
for (int i = 0; i < n; ++i)
{
cout << answer.back() << " ";
answer.pop_back();
}
return 0;
}
내가 원래 작성하던 코드