본문 바로가기
Softeer 풀이

[Sofreer 문제풀이] Lv.2(GBC) feat. Java

by happyhelen 2023. 11. 3.

1. GBC

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException{
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      StringTokenizer st = new StringTokenizer(br.readLine());
      int N = Integer.parseInt(st.nextToken());
      int M = Integer.parseInt(st.nextToken());
      int[][] standard = new int[N][N];
      int[][] test =new int[M][M];
      ArrayList<Integer> difList = new ArrayList<>();

      for(int i=0; i<N; i++){
        st = new StringTokenizer(br.readLine());
        int distance = Integer.parseInt(st.nextToken());
        int limit = Integer.parseInt(st.nextToken());
        standard[i][0] = distance;
        standard[i][1] = limit;
      }

      for(int i=0; i<M; i++){
        st = new StringTokenizer(br.readLine());
        int distance = Integer.parseInt(st.nextToken());
        int limit = Integer.parseInt(st.nextToken());
        test[i][0] = distance;
        test[i][1] = limit;
      }

      int idx = 0;
      for(int i=0; i<N; i++){
        for(int j=idx; j<M; j++){
          
          if(standard[i][0] < test[j][0] ){
            test[j][0] -= standard[i][0]; // 60-50 
            difList.add(test[j][1] - standard[i][1]);
            break;
          }else if(standard[i][0] > test[j][0] ){
            standard[i][0] -= test[j][0]; // 40-10
            difList.add(test[j][1] - standard[i][1]);
            idx++;
          }
          else{
            difList.add(test[j][1] - standard[i][1]);
            idx++;
            break;
          }

        }
      }

      Collections.sort(difList, Collections.reverseOrder());
      System.out.println(difList.get(0));


    }
}

 

구간마다의 거리"차"를 로직에 맞게 계산하는식으로 풀어야한다.