[java] 1449 수리공 항승

ideal dev·2023년 1월 10일
0

코딩테스트

목록 보기
51/69

1. 문제 링크 및 문제

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

2. 해결 방법 생각해보자 ...

0.5 간격으로 테이프를 설치해줘야하니,
1부터라고 하면 0.5 ~
3부터라고 하면 2.5~ 설치해줘야 하기 때문에
테이프의 범위를 잘 설정하여 반복문을 돌리면서 확인하면 된다.

3. 코드

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

public class Main {

    static int N,L;
    static int[] arr;

	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 L = Integer.parseInt(st.nextToken());
        arr = new int[N];

        st = new StringTokenizer(br.readLine());
        for(int i=0;i<N;i++)arr[i] = Integer.parseInt(st.nextToken());

        Arrays.sort(arr);

        int ans = 1;
        int tapeRange = (int)(arr[0] - 0.5 + L);

        for(int i=1;i<N;i++){
            if(arr[i] > tapeRange ){
                tapeRange = (int)(arr[i] - 0.5 + L);
                ans += 1;
            }
        }
        System.out.println(ans);
	}
}

0개의 댓글