본문 바로가기

USACO/Bronze

백준 15464 The Bovine Shuffle (USACO December 2017 Bronze 2번)

문제 링크: https://www.acmicpc.net/problem/15464

 

15464번: The Bovine Shuffle

The first line of input contains $N$, the number of cows. The next line contains the $N$ integers $a_1 \ldots a_N$. The final line contains the order of the $N$ cows after three shuffles, with each cow specified by its ID number.

www.acmicpc.net

예제에 나와있듯이 1 3 4 5 2 라고 shuffle하는 순서가 주어져 있다면, 1번은 그대로, 3번은 2번으로, 4번은 3번으로, 5번은 4번으로, 2번은 5번으로 자리를 옮기는것을 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
#include <bits/stdc++.h>
using namespace std;
 
int N, order[101];
string id[101], ans[101];
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    cin >> N;
    for(int i = 1; i <= N; i++cin >> order[i];
    for(int i = 1; i <= N; i++cin >> id[i];
 
    for(int t = 0; t < 3; t++) {
        for(int i = 1; i <= N; i++)
            ans[i] = id[order[i]];
        for(int i = 1; i <= N; i++)
            id[i] = ans[i];
    }
 
    for(int i = 1; i <= N; i++)
        cout << ans[i] << '\n';
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

두번째 코드 역시 내용은 같으나, 메모리도 덜 사용하고 for loop으로 shuffle을 3번 했던것을 한줄로 표현함.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;
 
int N, order[101];
string id[101];
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    cin >> N;
    for(int i = 1; i <= N; i++cin >> order[i];
    for(int i = 1; i <= N; i++cin >> id[i];
 
    for(int i = 1; i <= N; i++)
        cout << id[order[order[order[i]]]] << '\n';
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter