Problem C 풀이 (C++)

 문제 링크 : https://codeforces.com/contest/1547/problem/C

배열 a와 b를 loop마다 확인한다.

a와 b에서 각각 할 수 있는 일까지 모두 처리한다.

a와 b에서 둘 다 하나도 일을 처리하지 못한 경우 -1 출력.

a와 b 둘 다 모든 일이 끝났다면 일 처리 순서 결과 출력.


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
86
#include <iostream>
#include <vector>
 
using namespace std;
 
int main() {
 
    int t;
    cin >> t;
    
    while (t--) {
 
        int k, n, m;
        cin >> k >> n >> m;
 
        vector<int> answer;
        vector<int> a(n), b(m);
 
        for (int i = 0; i < n; i++)
            cin >> a[i];
        for (int i = 0; i < m; i++)
            cin >> b[i];
 
        int aidx = 0, bidx = 0;
 
        while (true) {
 
            bool isA = false, isB = false;
 
            // process a
            for (int i = aidx; i < a.size(); i++) {
                if (a[i] == 0) {
                    k++;
                    answer.push_back(a[i]);
                    isA = true;
                }
                else {
                    if (k >= a[i]) {
                        answer.push_back(a[i]);
                        isA = true;
                    }
                    else {
                        break;
                    }
                }
 
                aidx = i + 1;
            }
 
            // process b
            for (int i = bidx; i < b.size(); i++) {
                if (b[i] == 0) {
                    k++;
                    answer.push_back(b[i]);
                    isB = true;
                }
                else {
                    if (k >= b[i]) {
                        answer.push_back(b[i]);
                        isB = true;
                    }
                    else {
                        break;
                    }
                }
 
                bidx = i + 1;
            }
 
            if (aidx == a.size() && bidx == b.size()) {
                for (int ans : answer)
                    cout << ans << " ";
                cout << endl;
                break;
            }
            
            if (isA == false && isB == false) {
                cout << -1 << endl;
                break;
            }
 
        }
    }
 
    return 0;
}
cs

댓글