본문 바로가기
알고리즘 & 자료구조/백준

백준 15970

by 신재권 2022. 9. 7.
package baekjoon.정렬;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

class Main15970 {

   static int N, ans;
   static ArrayList<Integer>[] a;

   public static void main(String[] args) throws Exception {
      input();
      sort();
      find();
   }

   private static void find() {
      for (int i = 1; i <= N; i++) {
         for (int j = 0; j < a[i].size(); j++) {
            int toLeft = toLeft(i, j);
            int toRight = toRight(i, j);
            ans += Math.min(toLeft, toRight);
         }
      }
      System.out.println(ans);
   }

   private static int toLeft(int color, int idx) {
      if (idx == 0)
         return Integer.MAX_VALUE;
      return a[color].get(idx) - a[color].get(idx - 1);
   }

   private static int toRight(int color, int idx) {
      if (idx == a[color].size() - 1)
         return Integer.MAX_VALUE;
      return a[color].get(idx + 1) - a[color].get(idx);
   }

   private static void sort() {
      for (int i = 1; i <= N; i++) {
         a[i].sort(null);
      }
   }

   private static void input() throws IOException {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      StringTokenizer st;
      N = Integer.parseInt(br.readLine());
      a = new ArrayList[N + 1];
      for (int i = 1; i <= N; i++) {
         a[i] = new ArrayList<>();
      }
      for (int i = 1; i <= N; i++) {
         st = new StringTokenizer(br.readLine());
         int spot = Integer.parseInt(st.nextToken());
         int color = Integer.parseInt(st.nextToken());
         a[color].add(spot);
      }
   }

}

 

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 3273  (0) 2022.09.09
백준 1764  (0) 2022.09.08
백준 11652  (0) 2022.09.07
백준 1015  (0) 2022.09.07
백준 10825  (0) 2022.09.06