[BOJ 19637] IF๋ฌธ ์ข€ ๋Œ€์‹  ์จ์ค˜

 

1. ๋ฌธ์ œ : https://www.acmicpc.net/problem/19637

 

2. ํ’€์ด

๋น„๊ตํ•˜๋Š” ์กฐ๊ฑด์„ ์ด์ง„ํƒ์ƒ‰์œผ๋กœ ๊ณจ๋ผ์„œ ๋น„๊ต

 

3. ์ฝ”๋“œ

package baekjoon;

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

public class IF๋ฌธ์ข€๋Œ€์‹ ์จ์ค˜ {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(br.readLine());
        StringBuilder sb=new StringBuilder();

        int N=Integer.parseInt(st.nextToken());
        int M=Integer.parseInt(st.nextToken());

        String[][] title=new String[N][2];

        for(int i=0;i<N;i++){
            st=new StringTokenizer(br.readLine());
            title[i][0]=st.nextToken();
            title[i][1]=st.nextToken();
        }

        for(int i=0;i<M;i++){
            int num=Integer.parseInt(br.readLine());

            int left=0;
            int right=N-1;
            int mid=(left+right)/2;
            while(left<=right){
                mid=(left+right)/2;

                int titleInt=Integer.parseInt(title[mid][1]);
                if(titleInt<num){
                    left=mid+1;
                }
                else{
                    right=mid-1;
                }
            }

            sb.append(title[left][0]+"\n");
        }

        System.out.print(sb);
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ