[백준] PS/Java [실랜디]

[백준 2012] 등수 매기기 - JAVA

SH3542 2025. 3. 7. 17:59

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

 

그리디

 

오름차순 정렬 후 idx와 a[idx] 비교

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

class Main {

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

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int N = Integer.parseInt(br.readLine());
    long[] a = new long[N + 1];

    for (int i = 1; i <= N; i++) {
      a[i] = Integer.parseInt(br.readLine());
    }

    Arrays.sort(a);

    long ans = 0;
    for (int i = 1; i <= N; i++) {
      ans += Math.abs(a[i] - i);
    }
    System.out.println(ans);
  }
}