1377 버블 정렬

지름길

https://www.acmicpc.net/problem/1377

#1377: 버블 정렬

N은 첫 번째 줄에 제공됩니다. N은 500,000 이하의 자연수이다. 두 번째 줄부터 N줄까지 A(1)에서 A(N)까지 순차적으로 주어진다. A에 포함된 숫자는 1,000,000 또는 0보다 작거나 같은 자연수입니다.

www.acmicpc.net

설명

– 각 버블 정렬에 대해 마지막 값이 결정됩니다.

– 각 작업에 대해 왼쪽으로 한 칸 이동

– 이 변경을 이용하면 왼쪽으로 이동한 값 중 최대값이 발생한 만큼 최대값에 +1을 더하면 변경이 잘못되었을 때 그 값을 얻을 수 있다.

암호

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class BJ_1377_버블소트 {
    public static void main(String() args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        Num() arr = new Num(N);
        for(int i = 0; i < N; i++){
            int num = Integer.parseInt(br.readLine());
            arr(i) = new Num(num, i);
        }
        Arrays.sort(arr);
        int max = 0;
        for(int i = 0; i<N; i++){
            Num num = arr(i);
            max = Math.max(num.index - i, max);
        }
        int res = max + 1;
        System.out.println(res);
    }
    public static class Num implements Comparable<Num>{
        int num;
        int index;
        public Num(int num, int index){
            this.num = num;
            this.index = index;
        }
        public int compareTo(Num o){
            return this.num - o.num;
        }
    }
}