탑
#include <stdio.h>
int stack[500001] = {};
int arr[500001] = {};
int top = -1;
void push(int a, int b)
{
top++;
stack[top] = a;
arr[top] = b;
}
void pop()
{
if (top > -1)
{
top--;
}
}
int main()
{
int n, h, i;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
scanf("%d", &h);
while (top > -1 && stack[top] < h)
{
pop();
}
if (top == -1)
{
printf("0 ");
}
else
{
printf("%d ", arr[top]);
}
push(h, i);
}
return 0;
}
4회 조회




