• 문제 링크
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
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
static int a1, a2, b1, b2;
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int n = read(), k = read();
a1 = read();
a2 = read();
b1 = read();
b2 = read();
int[] arr = new int[n + 2];
for (int i = 1; i <= n; i++) {
int nxt = i, cnt = 1;
while ((nxt = swap(nxt)) != i) cnt++;
int rtn = k % cnt;
while (rtn-- > 0) nxt = swap(nxt);
arr[nxt] = i;
}
for (int i = 1; i <= n; i++) sb.append(arr[i]).append("\n");
bw.write(sb.toString());
bw.flush();
}
private static int swap(int i) {
if (a1 <= i && i <= a2) i = a1 + a2 - i;
if (b1 <= i && i <= b2) i = b1 + b2 - i;
return i;
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 15242 Knight - Graph Theory / Java (0) | 2023.12.16 |
---|---|
[백준] 6207 Cow Picnic - Graph Theory / Java (0) | 2023.12.15 |
[백준] 6189 Munching - Graph Theory / Java (0) | 2023.12.13 |
[백준] 6080 Bad Grass - Graph Theory / Java (0) | 2023.12.12 |
[백준] 10104 Party Invitation - Data Structure / Java (0) | 2023.12.11 |
댓글