BOJ : https://www.acmicpc.net/problem/2605

github : https://github.com/junho0956/Algorithm/blob/master/2605/2605/%EC%86%8C%EC%8A%A4.cpp

 

다음은 직접 list 를 구현하여 해결한 코드입니다.

 

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
#include <iostream>
using namespace std;
 
struct node {
    int num;
    node* first;
    node* back;
    bool r, b;
    
    node() {
        this->first = NULLthis->back = NULL;
        this->= this->= false;
    }
 
    void insert(node * np, int cnt, int ans) {
        node* p = np;
        if (cnt == 0) {
            node* newp = new node();
            p->first->back = newp;
            newp->first = p->first;
            newp->back = p;
            p->first = newp;
            newp->num = ans;
        }
        else {
            p->insert(p->first, cnt - 1, ans);
        }
    }
 
    void print_node(node* p) {
        if (p->b) return;
        cout << p->num << " ";
        p->print_node(p->back);
    }
};
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    
    node* root = new node();
    node* broot = new node();
    root->back = broot;
    broot->first = root;
    root->= broot->= true;
 
    int N; cin >> N;
 
    for (int i = 1; i <= N; i++) {
        int cnt; cin >> cnt;
        broot->insert(broot, cnt, i);
    }
 
    root->print_node(root->back);
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

다음은 list STL 을 사용하여 해결한 코드입니다.

 

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
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <list>
 
using namespace std;
 
int main() {
 
    std::ios::sync_with_stdio(false); cin.tie(0);
 
    int n; cin >> n;
 
    list<int> arr;
    list<int>::iterator iter;
    for (int i = 1; i <= n; i++) {
        int cand; cin >> cand;
 
        for (iter = arr.begin(); iter != arr.end(); iter++) {
            if (cand == 0) {
                break;
            }
            cand--;
        }
 
        arr.insert(iter, i);
    }
 
 
    for (auto i = arr.rbegin(); i != arr.rend(); i++) {
        cout << *<< " ";
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 7570번 줄 세우기  (0) 2020.02.10
BOJ 2631번 줄 세우기  (0) 2020.02.10
BOJ 1681번 줄 세우기  (0) 2020.02.10
BOJ 4781번 사탕가게  (0) 2020.02.10
BOJ 9507번 Generations of Tribbles  (0) 2020.02.10

+ Recent posts