• 문제 링크
17074번: 정렬
정렬이란, 배열의 모든 원소가 비내림차순이 되도록 순서를 바꾸는 것을 말한다. 예를 들어 배열 [2, 1, 2, 3, 1]을 정렬하면 [1, 1, 2, 2, 3]이 된다. 남규는 정수 N개로 이루어진 배열 하나를 갖고 있다
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read(), cnt = 0, idx = 0, res = 0;
int[] arr = new int[n + 2];
arr[0] = -1000000000;
arr[n + 1] = 1000000000;
arr[1] = read();
for (int i = 2; i <= n; i++) {
if (arr[i - 1] > (arr[i] = read())) {
cnt++;
idx = i;
}
}
if (cnt == 0) res = n;
else if (cnt == 1) {
if (arr[idx - 1] <= arr[idx + 1]) res++;
if (arr[idx - 2] <= arr[idx]) res++;
}
bw.write(String.valueOf(res));
bw.flush();
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
boolean flag = n == 13;
if (flag) n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return flag ? ~n + 1 : n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 1351 무한 수열 - Data Structure / Java (0) | 2023.12.07 |
---|---|
[백준] 5052 전화번호 목록 - Data Structure / Java (0) | 2023.12.06 |
[백준] 3372 보드 점프 - Dynamic Programming / Java (0) | 2023.12.04 |
[백준] 5705 Hexagonal Tiles - Dynamic Programming / Java (0) | 2023.12.03 |
[백준] 6245 Cow Solitaire - Dynamic Programming / Java (0) | 2023.12.02 |
댓글