본문 바로가기
Problem Solving/Baekjoon

[백준] 17074 정렬 - Dynamic Programming / Java

by graycode 2023. 12. 5.

 문제 링크

 

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;
    }

}

댓글