• 문제 링크
18323번: Photoshoot
Farmer John is lining up his $N$ cows ($2\le N\le 10^3$), numbered $1\ldots N$, for a photoshoot. FJ initially planned for the $i$-th cow from the left to be the cow numbered $a_i,$ and wrote down the permutation $a_1,a_2,\ldots,a_N$ on a sheet of paper. U
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
static int n;
static int[] arr;
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb;
arr = new int[(n = read()) - 1];
for (int i = 0; i < arr.length; i++) arr[i] = read();
int i = 1;
while (!generate(i, sb = new StringBuilder(), new boolean[n + 1]) && i < arr[0]) i++;
bw.write(sb.toString());
bw.flush();
}
private static boolean generate(int a, StringBuilder sb, boolean[] visit) {
sb.append(a).append(" ");
visit[a] = true;
for (int b : arr) {
if ((a = b - a) <= 0 || a > n || visit[a]) return false;
sb.append(a).append(" ");
visit[a] = true;
}
return true;
}
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' 카테고리의 다른 글
[백준] 10164 격자상의 경로 - Dynamic Programming / Java (0) | 2023.12.01 |
---|---|
[백준] 22342 계산 로봇 - Dynamic Programming / Java (0) | 2023.11.30 |
[백준] 27162 Yacht Dice - Brute Force / Java (0) | 2023.11.28 |
[백준] 18269 Where Am I? - Brute Force / Java (0) | 2023.11.27 |
[백준] 3711 학번 - Brute Force / Java (0) | 2023.11.26 |
댓글