• 문제 링크
25287번: 순열 정렬
$1$부터 $N$까지의 정수를 임의로 배열한 순열은 총 $N! = N\times(N-1)\times(N-2)\times\cdots\times1$가지가 있다. 예를 들어 $1$부터 $3$까지의 수를 임의로 배열한 순열은 $\lbrace1,2,3\rbrace, \lbrace1,3,2\rbrace, \lbra
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 = new StringBuilder();
int t = read();
while (t-- > 0) {
arr = new int[n = read()];
for (int i = 0; i < n; i++) arr[i] = read();
sb.append(isIncreasing() ? "YES\n" : "NO\n");
}
bw.write(sb.toString());
bw.flush();
}
private static boolean isIncreasing() {
arr[0] = Math.min(arr[0], n - arr[0] + 1);
for (int i = 1; i < n; i++) {
int min = Math.min(arr[i], n - arr[i] + 1), max = Math.max(arr[i], n - arr[i] + 1);
if (arr[i - 1] <= min) arr[i] = min;
else if (arr[i - 1] <= max) arr[i] = max;
else return false;
}
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' 카테고리의 다른 글
[백준] 25632 소수 부르기 게임 - Greedy / Java (0) | 2024.01.16 |
---|---|
[백준] 31066 비 오는 날 - Greedy / Java (0) | 2024.01.15 |
[백준] 2864 5와 6의 차이 - Greedy / Java (0) | 2024.01.13 |
[백준] 10263 Opening Ceremony - Greedy / Java (0) | 2024.01.12 |
[백준] 10774 저지 - Greedy / Java (0) | 2024.01.11 |
댓글