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

 

16939번: 2×2×2 큐브

첫째 줄에 2×2×2 루빅스 큐브 각 면의 각 칸 색상이 주어진다. 색상은 1부터 6까지의 자연수로 나타내며, 각 자연수는 총 4번 등장한다. i번째 수가 의미하는 칸은 아래와 같다.

www.acmicpc.net

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
using namespace std;
void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
}
/*
      01 02
      03 04
13 14 05 06 17 18 21 22
15 16 07 08 19 20 23 24
      09 10
      11 12
*/
deque<int> init_map;
// 그 면에서 보는 기준
// (0,왼)(1,오)(2,위)(3,아래)(4,정면)(5,뒷면)
 
int oper[6][8= {
    {22,24,11,9 ,7 ,5 ,3 ,1 },
    {23,21,2 ,4 ,6 ,8 ,10,12},
    {18,20,12,11,16,14,1 ,2 },
    {19,17,4 ,3 ,14,16,9 ,10},
    {24,23,20,19,8 ,7 ,16,15},
    {13,14,5 ,6 ,17,18,21,22}
};
int cube[6][2][2= {
    {{13,15},{14,16}},
    {{20,18},{19,17}},
    {{22,21},{24,23}},
    {{6,5},{8,7}},
    {{11,12},{9,10}},
    {{2,1},{4,3}}
};
int result = 0;
int check(const deque<int> &map_copy) {
    for (int i = 0; i < 6; i++) {
        int num = map_copy[cube[i][0][0]];
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                if (num != map_copy[cube[i][j][k]]) {
                    return 0;
                }
            }
        }
    }
    //cout << "111";
    return 1;
}
void turn(const int& oper_num) {
    // 시계방향
    deque<int> map_copy = init_map;
    deque<int> index;
    for (const auto& i : oper[oper_num]) {
        index.push_back(map_copy[i]);
    }
    for (int i = 0; i < 2; i++) {
        index.push_front(index.back());
        index.pop_back();
    }
    for (const auto& i : oper[oper_num]) {
        map_copy[i] = index.front();
        index.pop_front();
    }
    result = max(check(map_copy), result);
    // 시계반대 방향
    map_copy = init_map;
    for (const auto& i : oper[oper_num]) {
        index.push_back(map_copy[i]);
    }
    for (int i = 0; i < 2; i++) {
        index.push_back(index.front());
        index.pop_front();
    }
    for (const auto& i : oper[oper_num]) {
        map_copy[i] = index.front();
        index.pop_front();
    }
    result = max(check(map_copy),result);
}
void solve() {
    for (int i = 0; i < 6; i++)
        turn(i);
    cout << result << endl;
}
void input() {
    init_map.push_back(0);
    for (int i = 1; i <= 24; i++) {
        int temp;
        cin >> temp;
        init_map.push_back(temp);
    }
 
}
int main() {
    init();
    input();
    solve();
}
cs

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

 

5373번: 큐빙

각 테스트 케이스에 대해서 큐브를 모두 돌린 후의 윗 면의 색상을 출력한다. 첫 번째 줄에는 뒷 면과 접하는 칸의 색을 출력하고, 두 번째, 세 번째 줄은 순서대로 출력하면 된다. 흰색은 w, 노란

www.acmicpc.net

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
using namespace std;
void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
}
int T; // TEST_CASE
int n; // 돌린 횟수
/*
W 위 G 왼쪽 R 앞면 Y 아래면 B 오른쪽면 왼쪽 초록색
      O O O
      O O O
      O O O
G G G Y Y Y B B B W W W
G G G Y Y Y B B B W W W
G G G Y Y Y B B B W W W
      R R R
      R R R
      R R R
 
         1  2  3
         4  5  6
         7  8  9
10 11 12 19 20 21 28 29 30 37 38 39
13 14 15 22 23 24 31 32 33 40 41 42
16 17 18 25 26 27 34 35 36 43 44 45
         46 47 48
         49 50 51
         52 53 54
*/
// 0 L, 1 R, 2 U, 3 D, 4 F, 5 B
int oper[6][12= {
    {39,42,45,52,49,46,25,22,19,7 ,4 ,1 },
    {43,40,37,3 ,6 ,9 ,21,24,27,48,51,54},
    {30,33,36,54,53,52,16,13,10,1 ,2 ,3 },
    {34,31,28,9 ,8 ,7 ,12,15,18,46,47,48},
    {45,44,43,36,35,34,27,26,25,18,17,16},
    {10,11,12,19,20,21,28,29,30,37,38,39}
};
int cube[6][3][3= {
    {{10,13,16},{11,14,17},{12,15,18}},
    {{36,33,30},{35,32,29},{34,31,28}},
    {{39,38,37},{42,41,40},{45,44,43}},
    {{21,20,19},{24,23,22},{27,26,25}},
    {{52,53,54},{49,50,51},{46,47,48}},
    {{3 ,2 ,1 },{6 ,5 ,4 },{9 ,8 ,7}}
};
char MAP_init[55= { 0,
'o','o','o','o','o','o','o','o','o',
'g','g','g','g','g','g','g','g','g',
'y','y','y','y','y','y','y','y','y',
'b','b','b','b','b','b','b','b','b',
'w','w','w','w','w','w','w','w','w',
'r','r','r','r','r','r','r','r','r'
};
char MAP_COPY[55];
void turn(const string& op) {
    int num_oper;
    if (op[0== 'L')
        num_oper = 0;
    else if (op[0== 'R')
        num_oper = 1;
    else if (op[0== 'U')
        num_oper = 2;
    else if (op[0== 'D')
        num_oper = 3;
    else if (op[0== 'F')
        num_oper = 4;
    else if (op[0== 'B')
        num_oper = 5;
    deque<char> index;
    deque<char> cube_front[3];
    for (const auto& i : oper[num_oper]) 
        index.push_back(MAP_COPY[i]);
    if (op[1== '+') {
        for (int i = 0; i < 3; i++) {
            index.push_front(index.back());
            index.pop_back();
        }
        for (int j = 0; j <= 2; j++) {
            for (int i = 2; i >= 0; i--) {
                cube_front[j].push_back(MAP_COPY[cube[num_oper][i][j]]);
            }
        }
    }
    else {
        for (int i = 0; i < 3; i++) {
            index.push_back(index.front());
            index.pop_front();
        }
        int count = 0;
        for (int j = 2; j >= 0; j--) {
            for (int i = 0; i <=2; i++) {
                cube_front[count].push_back(MAP_COPY[cube[num_oper][i][j]]);
            }
            count++;
        }
    }
    for (const auto& i : oper[num_oper]) {
        MAP_COPY[i] = index.front();
        index.pop_front();
    }
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            MAP_COPY[cube[num_oper][i][j]] = cube_front[i][j];
            
        }
    }
}
void print() {
    cout << MAP_COPY[39<< MAP_COPY[38<< MAP_COPY[37<< "\n";
    cout << MAP_COPY[42<< MAP_COPY[41<< MAP_COPY[40<< "\n";
    cout << MAP_COPY[45<< MAP_COPY[44<< MAP_COPY[43<< "\n";
}
void solve() {
    cin >> T;
    while (T--) {
        memcpy(MAP_COPY, MAP_init, sizeof(MAP_init));
        cin >> n;
        while (n--) {
            string oper;
            cin >> oper;
            turn(oper);
        }
        print();
    }
}
int main() {
    init();
    solve();
}
 
cs

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

 

17406번: 배열 돌리기 4

크기가 N×M 크기인 배열 A가 있을때, 배열 A의 값은 각 행에 있는 모든 수의 합 중 최솟값을 의미한다. 배열 A가 아래와 같은 경우 1행의 합은 6, 2행의 합은 4, 3행의 합은 15이다. 따라서, 배열 A의

www.acmicpc.net

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
using namespace std;
void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
}
struct operation{
    int r, c, s;
};
int array_map_original[51][51];
int array_map_copy[51][51];
int N, M, K;
vector<operation> operation_list;
bool operation_check[6];
operation choose_oper[6];
// 오른쪽 아래 왼쪽 위쪽
int dx[4= { 0,1,0,-1 };
int dy[4= { 1,0,-1,0 };
int min_result = 987654321;
void cal_array_value() {
    for (int i = 1; i <= N; i++) {
        int total = 0;
        for (int j = 1; j <= M; j++) {
            total += array_map_copy[i][j];
        }
        min_result = min(min_result, total);
    }
}
void turn(const operation& oper) {
    for (int i = 1; i <= oper.s; i++) {
        deque<int> temp;
        int x = oper.r - i;
        int y = oper.c - i;
        temp.push_back(array_map_copy[x][y]);
        for (int j = 0; j < 4; j++) {
            for (int k = 0; k < i * 2; k++) {
                x += dx[j];
                y += dy[j];
                temp.push_back(array_map_copy[x][y]);
            }
        }    
        temp.pop_back();
        array_map_copy[x][y] = temp.back();
        temp.pop_back();
        for (int j = 0; j < 4; j++) {
            for (int k = 0; k < i * 2; k++) {
                if (temp.empty()) {
                    continue;
                }
                x += dx[j];
                y += dy[j];
                array_map_copy[x][y] = temp.front();
                temp.pop_front();
            }
        }
    }
 
}
void choose(int count) {
    if (count == K) {
        memcpy(array_map_copy, array_map_original, sizeof(array_map_copy));
        for (int i = 0; i < K; i++) {
            //cout << choose_oper[i].r << " " << choose_oper[i].c << " " << choose_oper[i].s;
            turn(choose_oper[i]);
        }
        cal_array_value();
        return;
    }
    for (int i = 0; i < K; i++) {
        if (!operation_check[i]) {
            operation_check[i] = true;
            choose_oper[count] = operation_list[i];
            choose(count + 1);
            operation_check[i] = false;
        }
    }
}
void input() {
    cin >> N >> M >> K;
    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= M; j++)
            cin >> array_map_original[i][j];
    for (int i = 1; i <= K; i++) {
        int r, c, s;
        cin >> r >> c >> s;
        operation_list.push_back({ r, c, s });
    }
}
int main() {
    init();
    input();
    choose(0);
    cout << min_result << endl;
}
cs

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

 

17779번: 게리맨더링 2

재현시의 시장 구재현은 지난 몇 년간 게리맨더링을 통해서 자신의 당에게 유리하게 선거구를 획정했다. 견제할 권력이 없어진 구재현은 권력을 매우 부당하게 행사했고, 심지어는 시의 이름

www.acmicpc.net

 

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
using namespace std;
// 게리맨더링 2  백준 17779 브루트포스
int N;
int MAP[21][21];
int result = 987654321;
void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);    
}
 
bool one(int &x,int &y, int &d1, int &d2, int &i,int &j) {
    if (i >= 1 && i < x + d1 && j >= 1 && j <= y) {
        if (i >= x && j >= y - i + x) {
            return false;
        }
        return true;
    }
    return false;
}
bool two(int& x, int& y, int& d1, int& d2, int& i, int& j) {
    if (i >= 1 && i <= x + d2 && j > y && j <= N) {
        if (i >= x && j <= y + i - x) {
            return false;
        }
        return true;
    }
    return false;
}
bool three(int& x, int& y, int& d1, int& d2, int& i, int& j) {
    if (i >= x + d1 && i <= N && j >= 1 && j < y - d1 + d2) {
        if (i <= x + d1 + d2 && j >= y + d2 - d1 - (x + d1 + d2 - i)) {
            return false;
        }
        return true;
    }
    return false;
}
bool four(int& x, int& y, int& d1, int& d2, int& i, int& j) {
    if (i > x + d2 && i <= N && j >= y-d1+d2 && j <= N) {
        if (i <= x + d1 + d2 && j <= y + d2 - d1 + (x + d1 + d2 - i)) {
            return false;
        }
        return true;
    }
    return false;
}
void Divide_Areas(int& x, int& y, int& d1, int& d2) {
    int num_area[6= { 0 };
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j++) {
            if (one(x, y, d1, d2, i, j)) {
                num_area[1+= MAP[i][j];
            }
            else if (two(x, y, d1, d2, i, j)) {
                num_area[2+= MAP[i][j];
            }
            else if (three(x, y, d1, d2, i, j)) {
                num_area[3+= MAP[i][j];
            }
            else if (four(x, y, d1, d2, i, j)) {
                num_area[4+= MAP[i][j];
            }
            else {
                num_area[5+= MAP[i][j];
            }
        }
    }
    sort(num_area + 1, num_area + 6);
    result = min(result, num_area[5- num_area[1]);
}
void choose_d1_d2(int &x, int &y) {
    int d1 = 1;
    int d2 = 1;
    bool flag = false;
    while (1) {
        if (x + d1 + d2 <= N && y - d1 >= 1 && y + d2 <= N) {
 
            Divide_Areas(x, y, d1, d2);
            d2++;
            flag = true;
        }
        else if(flag == true){
            d1++;
            d2 = 1;
            flag = false;
        }
        else if (flag == false)
            break;
    }
}
void solve() {
    for (int x = 1; x <= N; x++) {
        for (int y = 2; y <= N - 1; y++) {
            choose_d1_d2(x, y);
        }
    }
}
void input() {
    cin >> N;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j++) {
            cin >> MAP[i][j];
        }
    }
}
int main() {
    init();
    input();
    solve();
    cout << result << endl;
}
cs

'BAEKJOON ONLINE JUDGE' 카테고리의 다른 글

[백준 5373] 큐빙 (C++)  (0) 2021.08.31
[백준 17406] 배열 돌리기 4 (C++)  (0) 2021.08.30
[백준 1946] 신입 사원 (C++)  (0) 2021.08.28
[백준 1715] 카드 정렬하기 (C++)  (0) 2021.08.27
[백준 1781] 컵라면 (C++)  (0) 2021.08.27

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

 

1946번: 신입 사원

첫째 줄에는 테스트 케이스의 개수 T(1 ≤ T ≤ 20)가 주어진다. 각 테스트 케이스의 첫째 줄에 지원자의 숫자 N(1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개 줄에는 각각의 지원자의 서류심사 성

www.acmicpc.net

 

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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    int N;
    int document, interview;
    int result = 0;
    vector<pair<intint>> grade;
    cin >> T;
    while (T--) {
        cin >> N;
        for (int i = 0; i < N; i++) {
            cin >> document >> interview;
            grade.push_back({ document,interview });
        }
        sort(grade.begin(), grade.end());
        int max_score = grade.front().second;
        for (int i = 0; i < N; i++) {
            if (grade[i].second <= max_score) {
                result++;
                max_score = grade[i].second;
            }
        }
        cout << result << "\n";
        result = 0;
        grade.clear();
    }
}
cs

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

 

1715번: 카드 정렬하기

정렬된 두 묶음의 숫자 카드가 있다고 하자. 각 묶음의 카드의 수를 A, B라 하면 보통 두 묶음을 합쳐서 하나로 만드는 데에는 A+B 번의 비교를 해야 한다. 이를테면, 20장의 숫자 카드 묶음과 30장

www.acmicpc.net

 

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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
using namespace std;
// 백준 1715 카드 정렬하기
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    priority_queue<intvector<int>, greater<int>> pq;
    for (int i = 0; i < N; i++) {
        int num;
        cin >> num;
        pq.push(num);
    }
    /*
    while (!pq.empty()) {
        cout << pq.top() << endl;
        pq.pop();
    }
    */
    if (N == 1) {
        cout << 0;
        return 0;
    }
    int num1 = 0;
    int num2 = 0;
    int result = 0;
    while (1) {
        num1 = pq.top(); pq.pop();
        num2 = pq.top(); pq.pop();
        result += num1 + num2;
        if (pq.empty()) break;
        pq.push(num1 + num2);
    }
    cout << result;
}
cs

'BAEKJOON ONLINE JUDGE' 카테고리의 다른 글

[백준 17779] 게리맨더링 2 (C++)  (0) 2021.08.29
[백준 1946] 신입 사원 (C++)  (0) 2021.08.28
[백준 1781] 컵라면 (C++)  (0) 2021.08.27
[백준 13904] 과제 (C++)  (0) 2021.08.27
[백준 1374] 강의실 (C++)  (0) 2021.08.27

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

 

1781번: 컵라면

상욱 조교는 동호에게 N개의 문제를 주고서, 각각의 문제를 풀었을 때 컵라면을 몇 개 줄 것인지 제시 하였다. 하지만 동호의 찌를듯한 자신감에 소심한 상욱 조교는 각각의 문제에 대해 데드라

www.acmicpc.net

 

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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
using namespace std;
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    priority_queue<pair<intint>vector<pair<intint>>,greater<pair<int,int>>> pq;
    priority_queue<intvector<int>, greater<int>> answer;
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        int day, cost;
        cin >> day >> cost;
        pq.push(make_pair(day, cost));
    }
    int result = 0;
    while (!pq.empty()) {
        answer.push(pq.top().second);
        result += pq.top().second;
        if (answer.size() > pq.top().first) {
            result -= answer.top();
            answer.pop();
        }
        pq.pop();
    }
    cout << result << endl;
}
cs

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

 

13904번: 과제

예제에서 다섯 번째, 네 번째, 두 번째, 첫 번째, 일곱 번째 과제 순으로 수행하고, 세 번째, 여섯 번째 과제를 포기하면 185점을 얻을 수 있다.

www.acmicpc.net

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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
using namespace std;
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    priority_queue<pair<intint>vector<pair<intint>>,greater<pair<int,int>>> pq;
    priority_queue<intvector<int>, greater<int>> answer;
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        int day, cost;
        cin >> day >> cost;
        pq.push(make_pair(day, cost));
    }
    int result = 0;
    while (!pq.empty()) {
        answer.push(pq.top().second);
        result += pq.top().second;
        if (answer.size() > pq.top().first) {
            result -= answer.top();
            answer.pop();
        }
        pq.pop();
    }
    cout << result << endl;
}
cs

+ Recent posts