리스트(List)

백준 2605번 : 줄 세우기

cepiloth 2018. 6. 17. 11:14
반응형

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


1. 문제 요약

입력받은 정수로 임의 접근하여 원소를 삽입 하는 문제


2. 알고리즘

자료구조 List 임의 접근을 사용하여 해결.


3. 코드

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;
}
cs

반응형