본문 바로가기

USACO/Bronze

백준 18788 Swapity Swap (USACO February 2020 Bronze 3번)

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

 

18788번: Swapity Swap

Initially, the order of the cows is $[1,2,3,4,5,6,7]$ from left to right. After the first step of the process, the order is $[1,5,4,3,2,6,7].$ After the second step of the process, the order is $[1,5,7,6,2,3,4]$. Repeating both steps a second time yields t

www.acmicpc.net

배열의 처음 상태로 몇번만에 돌아오는지 period를 먼저 계산하고, 실제로 k번을 반복하는 대신 k % period 만큼만 반복한후의 배열을 출력하면 됨.

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
#include <bits/stdc++.h>
using namespace std;
 
int n, k, a, b, c, d, arr[101];
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    cin >> n >> k >> a >> b >> c >> d;
    for(int i = 1; i <= n; i++)
        arr[i] = i;
 
    int period = 1;
    while(1) {
        reverse(arr + a, arr + b + 1);
        reverse(arr + c, arr + d + 1);
        bool ok = true;
        for(int i = 1; i <= n; i++) {
            if(i != arr[i])
                ok = false;
        }
        if(ok) break;
        period++;
    }
 
    k %= period;
    while(k--) {
        reverse(arr + a, arr + b + 1);
        reverse(arr + c, arr + d + 1);
    }
 
    for(int i = 1; i <= n; i++)
        cout << arr[i] << '\n';
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter