반응형
import java.util.Scanner;

public class Main {

    // USACO Swapity Swap JAVA
    // 백준 18788

    static int a1;
    static int a2;
    static int b1;
    static int b2;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int k = sc.nextInt();
        a1 = sc.nextInt();
        a2 = sc.nextInt();
        b1 = sc.nextInt();
        b2 = sc.nextInt();

        int[] cows = new int[n + 1];    //0번 인덱스 사용 X
        int[] result = new int[n + 1];

        for (int i = 1; i < n + 1; i++) {
            int reverseCnt = 0;
            int tempIdx = i;

            while (true) {              //몇번 리버스 하면 제자리 오는지 확인
                tempIdx = getReverseIdx(tempIdx);
                ++reverseCnt;
                if (tempIdx == i) {
                    break;
                }
            }

            tempIdx = i;    //리버스 횟수 카운트 하느라 사용해서 다시 초기화
            for (int j = 0; j < k % reverseCnt; j++) {
                tempIdx = getReverseIdx(tempIdx);
            }

            result[tempIdx] = i;
        }

        //0 제외 출력
        for (int i = 1; i < n + 1; i++) {
            System.out.println(result[i]);
        }
    }//main

    //소의 입장에서 a,b 리버스 이후 자신의 위치를 찾아가자
    public static int getReverseIdx(int idx) {
        if (a1 <= idx && idx <= a2) idx = a1 + a2 - idx;
        if (b1 <= idx && idx <= b2) idx = b1 + b2 - idx;
        return idx;
    }


}//class


/**
 * 1 2 3 4 5 6 7 // 2~5 , 3~7
 * <p>
 * 1 5 4 3 2 6 7
 * 1 5 7 6 2 3 4    //ab 1번
 * <p>
 * 1 2 6 7 5 3 4
 * 1 2 4 3 5 7 6    //ab 2번
 * <p>
 * 1 5 3 4 2 7 6
 * 1 5 6 7 2 4 3    //ab 3번
 * <p>
 * 1 2 7 6 5 4 3
 * 1 2 3 4 5 6 7    //ab 4번
 */


[USACO] Swapity Swap JAVA

문제

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

코드

 

반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기