[백준 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

📝 풀이

 

💻 코드

package org.kyhslam.bakjun.month_10;

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

public class p1377 {

    public static void main(String[] args) throws IOException {

        //5
        //10 1 5 2 3

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(reader.readLine());
        mData[] A = new mData[N];

        for (int i = 0; i < N; i++) {
            A[i] = new mData(Integer.parseInt(reader.readLine()), i);
        }

        for (mData mData : A) {
            System.out.println(mData.index + " , " + mData.value);
        }

        System.out.println("00000");
        Arrays.sort(A);

        for (mData mData : A) {
            System.out.println(mData.index + " , " + mData.value);
        }
        int MAX = 0;
        for (int i = 0; i < N; i++) {
            if(MAX < A[i].index - i){
                MAX = A[i].index - i;
            }
        }
        System.out.println(MAX+1);
    }
}

class mData implements Comparable<mData> {
    int value;
    int index;

    public mData(int value, int index) {
        super();
        this.value = value;
        this.index = index;
    }

    @Override
    public int compareTo(mData o) {
        return this.value - o.value; // value기준 오름차순순
    }
}

댓글

Designed by JB FACTORY