๊ณจ๋“œ4 : stack ๋ฌธ์ œ์ด๋‹ค.

ํ’€์ด

์•„, ์ •๋ง ์•„์‰ฌ์šด ๋ฌธ์ œ์˜€๋‹ค. ํ•˜์ง€๋งŒ ๋ฐฐ์šด ๊ฒƒ์ด ์žˆ์—ˆ๋‹ค. Stack์„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ์€ ์–ด๋–ค ๋ณต์žก๋„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. ์ด ๋ถ€๋ถ„์€ ๋’ค์— ๋‹ค์‹œ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. ์ผ๋‹จ ์ด ๋ฌธ์ œ๋ฅผ ๋‹จ์ˆœํ•˜๊ฒŒ ์ˆœํšŒํ•ด์„œ ํ‘ผ๋‹ค๋ฉด ์ตœ์•…์˜ ๊ฒฝ์šฐ ๊ฐ€ ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— n=1,000,000์ธ ์ƒํ™ฉ์—์„œ ๋ฐ”๋กœ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค. ๊ทธ๋ž˜์„œ ์ด๋ฅผ n์— ๊ฐ€๊นŒ์šด ์†๋„๋กœ ํ’€์–ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ๋ฌธ์ œ๋ฅผ ๋ถ„์„ํ•ด๋ณด์ž. ์˜คํฐ์ˆ˜๋Š” ํ˜„์žฌ ๊ฐ’์—์„œ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ํฐ๊ฐ’๋“ค ์ค‘ ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ๋กœ ๋‚˜์˜ค๋Š” ๋…€์„์ด๋ผ๊ณ  ์ •j์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์—, ๋งŒ์•ฝ ํ˜„์žฌ๊ฐ’์ด 4์ธ๋ฐ, ๋‹ค์Œ ๊ฐ’์ด 10์ด๋ฉด ๊ทธ 10์ด ์˜คํฐ์ˆ˜๊ฐ€ ๋  ์ˆ˜ ์žˆ๊ฒ ๋‹ค. ์—ฌ๊ธฐ์„œ ์•Œ ์ˆ˜ ์žˆ๋Š” ์ ์€ ์•„, ํฐ๋†ˆ์ด ๋‚˜์˜ค๋ฉด ๊ทธ ์ „๊ฐ’์€ ์ด ํฐ๋†ˆ์ด ์˜คํฐ์ˆ˜๊ฐ€ ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๊ตฌ๋‚˜. ์ •๋„์ด๋‹ค. ๊ทธ๋Ÿผ ์ด๋Ÿฐ ์ƒํ™ฉ์€ ์–ด๋–จ๋ผ?

4 3 9

์ด๋Ÿฐ ์ƒํ™ฉ์—์„œ 4์˜ ์ž…์žฅ์—์„œ 3์€ ์˜คํฐ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ 3์˜ ์ž…์žฅ์—์„œ 9๋Š” ์˜คํฐ์ˆ˜์ด๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด 4์˜ ์˜คํฐ์ˆ˜๋Š”? 9์ด๋‹ค. ์—ฌ๊ธฐ์„œ, ํ˜„์žฌ ๊ฐ’๋ณด๋‹ค ๋ฏธ๋ž˜ ๊ฐ’์ด ํฐ ๊ฒฝ์šฐ ๊ณผ๊ฑฐ๊ฐ’๋“ค ์ค‘์— ๋ฏธ๋ž˜ ๊ฐ’์„ ์˜คํฐ์ˆ˜๋กœ ๊ฐ€์ง€๋Š” ๋…€์„์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒฐ๋ก ์„ ๋‚ด๋ฆด ์ˆ˜ ์žˆ๋‹ค.

1 7  9 7 8  3 1  4 10  3 10
7 9 10 8 10 4 4 10 -1 10 -1

์ž ๊ทธ๋Ÿผ, ์–ด๋–ค ๋ถ„๊ธฐ์—์„œ ์ด ๊ฐ€์ •์ด ํ‹€๋ฆด ์ˆ˜ ์žˆ์„๊นŒ? ์ผ๋‹จ ๋ฏธ๋ž˜ ๊ฐ’์ด ํ˜„์žฌ๊ฐ’๊ณผ ๊ฐ™๋‹ค๋ฉด, ํ˜„์žฌ ๊ฐ’์˜ ์˜คํฐ์ˆ˜๋Š” ๊ทธ ๋ฏธ๋ž˜๊ฐ’์ด ์•„๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ stack์„ ์‚ฌ์šฉํ•ด๋ณด์ž. stack์—๋Š” ๋‹ค์Œ์— ๋‚˜์˜ฌ ๋ฏธ๋ž˜๊ฐ’์— ์˜ํ•ด ์˜คํฐ์ˆ˜๊ฐ€ ๊ฒฐ์ •๋  ํ›„๋ณด ์œ„์น˜๋ฅผ ๋งํ•œ๋‹ค. ์ข€ ์–ด๋ ค์šฐ๋‹ˆ ์ ์–ด๋ณด์ž.

11
1 7  9 7 8  3 1  4 10  3 10

index : 0
value : 1
stack ํ˜„์žฌ ์ƒํƒœ
0 

index : 1
value : 7
stack top (position): 0
value : 1
    ์˜คํฐ์ˆ˜ ํ›„๋ณด ๋ฐœ๊ฒฌ, ๋‹ต์•ˆ ์—…๋ฐ์ดํŠธ
    stack before
    0 
    answer before
    0 0 0 0 0 0 0 0 0 0 0 
    stack after
    
    answer after
    7 0 0 0 0 0 0 0 0 0 0 
stack ํ˜„์žฌ ์ƒํƒœ
1 

index : 2
value : 9
stack top (position): 1
value : 7
    ์˜คํฐ์ˆ˜ ํ›„๋ณด ๋ฐœ๊ฒฌ, ๋‹ต์•ˆ ์—…๋ฐ์ดํŠธ
    stack before
    1 
    answer before
    7 0 0 0 0 0 0 0 0 0 0 
    stack after
    
    answer after
    7 9 0 0 0 0 0 0 0 0 0 
stack ํ˜„์žฌ ์ƒํƒœ
2 

index : 3
value : 7
stack top (position): 2
value : 9
stack ํ˜„์žฌ ์ƒํƒœ
2 3 

index : 4
value : 8
stack top (position): 3
value : 7
    ์˜คํฐ์ˆ˜ ํ›„๋ณด ๋ฐœ๊ฒฌ, ๋‹ต์•ˆ ์—…๋ฐ์ดํŠธ
    stack before
    2 3 
    answer before
    7 9 0 0 0 0 0 0 0 0 0 
    stack after
    2 
    answer after
    7 9 0 8 0 0 0 0 0 0 0 
stack ํ˜„์žฌ ์ƒํƒœ
2 4 

index : 5
value : 3
stack top (position): 4
value : 8
stack ํ˜„์žฌ ์ƒํƒœ
2 4 5 

index : 6
value : 1
stack top (position): 5
value : 3
stack ํ˜„์žฌ ์ƒํƒœ
2 4 5 6 

index : 7
value : 4
stack top (position): 6
value : 1
    ์˜คํฐ์ˆ˜ ํ›„๋ณด ๋ฐœ๊ฒฌ, ๋‹ต์•ˆ ์—…๋ฐ์ดํŠธ
    stack before
    2 4 5 6 
    answer before
    7 9 0 8 0 0 0 0 0 0 0 
    stack after
    2 4 5 
    answer after
    7 9 0 8 0 0 4 0 0 0 0 
    ์˜คํฐ์ˆ˜ ํ›„๋ณด ๋ฐœ๊ฒฌ, ๋‹ต์•ˆ ์—…๋ฐ์ดํŠธ
    stack before
    2 4 5 
    answer before
    7 9 0 8 0 0 4 0 0 0 0 
    stack after
    2 4 
    answer after
    7 9 0 8 0 4 4 0 0 0 0 
stack ํ˜„์žฌ ์ƒํƒœ
2 4 7 

index : 8
value : 10
stack top (position): 7
value : 4
    ์˜คํฐ์ˆ˜ ํ›„๋ณด ๋ฐœ๊ฒฌ, ๋‹ต์•ˆ ์—…๋ฐ์ดํŠธ
    stack before
    2 4 7 
    answer before
    7 9 0 8 0 4 4 0 0 0 0 
    stack after
    2 4 
    answer after
    7 9 0 8 0 4 4 10 0 0 0 
    ์˜คํฐ์ˆ˜ ํ›„๋ณด ๋ฐœ๊ฒฌ, ๋‹ต์•ˆ ์—…๋ฐ์ดํŠธ
    stack before
    2 4 
    answer before
    7 9 0 8 0 4 4 10 0 0 0 
    stack after
    2 
    answer after
    7 9 0 8 10 4 4 10 0 0 0 
    ์˜คํฐ์ˆ˜ ํ›„๋ณด ๋ฐœ๊ฒฌ, ๋‹ต์•ˆ ์—…๋ฐ์ดํŠธ
    stack before
    2 
    answer before
    7 9 0 8 10 4 4 10 0 0 0 
    stack after
    
    answer after
    7 9 10 8 10 4 4 10 0 0 0 
stack ํ˜„์žฌ ์ƒํƒœ
8 

index : 9
value : 3
stack top (position): 8
value : 10
stack ํ˜„์žฌ ์ƒํƒœ
8 9 

index : 10
value : 10
stack top (position): 9
value : 3
    ์˜คํฐ์ˆ˜ ํ›„๋ณด ๋ฐœ๊ฒฌ, ๋‹ต์•ˆ ์—…๋ฐ์ดํŠธ
    stack before
    8 9 
    answer before
    7 9 10 8 10 4 4 10 0 0 0 
    stack after
    8 
    answer after
    7 9 10 8 10 4 4 10 0 10 0 
stack ํ˜„์žฌ ์ƒํƒœ
8 10 

7 9 10 8 10 4 4 10 -1 10 -1

์ˆœ์„œ๋Œ€๋กœ ๋”ฐ๋ผ๊ฐ€๋‹ค ๋ณด๋ฉด ์ดํ•ด๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. ์ตœ์ข…์ ์œผ๋กœ stack์— ๋‚จ๋Š” ๊ฐ’์€, ๋งจ ๋งˆ์ง€๋ง‰ ๊ฐ’๊นŒ์ง€ ๋ณด์•˜๋Š”๋Œ€๋„ ์˜คํฐ์ˆ˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์—ˆ๋˜, ์ธ๋ฑ์Šค ๋“ค์ด๋‹ค. ์˜คํฐ์ˆ˜๋ฅผ ์ฐพ์ง€ ๋ชปํ•œ ๋…€์„๋“ค์„ ๋งˆ์ง€๋ง‰์œผ๋กœ -1๋กœ ์—…๋ฐ์ดํŠธํ•˜๋ฉด ๋๋‚œ๋‹ค.

Code

//
//  main.cpp
//  algorithm_prac
//
//  Created by ์ตœ์™„์‹ on 2021/04/05.
//
 
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// stack์— ์œ„์น˜ ์ •๋ณด๋ฅผ ๋„ฃ์œผ๋ฉด ์ค‘๋ณต ์—ฐ์‚ฐ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค..!!!!
int N;
int a[1000001] = {0,};
int v[1000001] = {0,};
vector<int> s;
 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> a[i];
    }
    
    for (int i = 0; i < N; i++) {
        cout << "index : " << i << '\n';
        cout << "value : " << a[i] << '\n';
        
        if (!s.empty()) {
            cout << "stack top (position): " << s.back() << '\n';
            cout << "value : " << a[s.back()] << '\n';
        }
        
        while (!s.empty() && a[s.back()] < a[i]) {
            cout << "    ์˜คํฐ์ˆ˜ ํ›„๋ณด ๋ฐœ๊ฒฌ, ๋‹ต์•ˆ ์—…๋ฐ์ดํŠธ" << '\n';
            cout << "    stack before" << '\n' << "    ";
            for (int j = 0; j < s.size(); j++) {
                cout << s[j] << " ";
            }cout << '\n';
            cout << "    answer before" << '\n' << "    ";
            for (int j = 0; j < N; j++) {
                cout << v[j] << " ";
            }cout << '\n';
            
            v[s.back()] = a[i];
            s.pop_back();
            
            cout << "    stack after" << '\n' << "    ";
            for (int j = 0; j < s.size(); j++) {
                cout << s[j] << " ";
            }cout << '\n';
            cout << "    answer after" << '\n' << "    ";
            for (int j = 0; j < N; j++) {
                cout << v[j] << " ";
            }cout << '\n';
        }
        s.push_back(i);
        cout << "stack ํ˜„์žฌ ์ƒํƒœ" << '\n';
        for (int j = 0; j < s.size(); j++) {
            cout << s[j] << " ";
        }cout << '\n' << '\n';
        
    }
    
    while (!s.empty()) {
        v[s.back()] = -1;
        s.pop_back();
    }
    
    for (int i = 0; i < N; i++) {
        cout << v[i] << " ";
    }
 
    return 0;
}
 

Reference