2024-05-20 18:59:53

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _2493_boj boj = new _2493_boj();
 
            boj.boj_2493();
 
        }
    }
    internal class _2493_boj
    {
        public void boj_2493()
        {
            var reader = new StreamReader(Console.OpenStandardInput());
            var print = new StreamWriter(Console.OpenStandardOutput());
            var sb = new StringBuilder();
            Stack tower = new Stack();
            tower.size = int.Parse(reader.ReadLine());
            tower.stack = new string[tower.size];
 
            string[] input = reader.ReadLine().Split();
            string[] result = new string[tower.size];
 
            for (int i = 0; i < tower.size; i++)
            {
                while (tower.top != -1 && int.Parse(input[i]) > int.Parse(input[int.Parse(tower.peek())]))
                {
                    tower.pop();
                }
                sb.Append(tower.top == -1 ? "0" : (int.Parse(tower.peek()) + 1).ToString());
                if (i != tower.size - 1)
                    sb.Append(' ');
                tower.push(i.ToString());
            }
 
            print.Write(sb);
            print.Flush();
 
        }
    }
    public class Stack
    {
        public int top = -1;
        public string[] stack;
        public int size;
 
        public void push(string data)
        {
            stack[++top] = data;
        }
        public string pop()
        {
            string res = null;
            if (top != -1)
            {
                res = stack[top];
                stack[top--= null;
            }
 
            return res;
        }
        public string peek()
        {
            string res = null;
            if (top != -1)
                res = stack[top];
            return res;
        }
 
    }
}
 
 
cs

 

 

 

'하루 한 접시' 카테고리의 다른 글

[백준] 10845번 : 큐 [C#]  (0) 2024.05.21
[백준] 28279번: 덱 2 [C#]  (0) 2024.05.20
[백준] 1874번 : 스택 수열 [C#]  (0) 2024.05.16
[백준] 요세푸스 문제 0 [C#]  (0) 2024.05.14
[백준] 2164번: 카드2 [C#]  (0) 2024.05.14