본문 바로가기

Algorithm

BOJ 9237번 이장님 초대

www.acmicpc.net/problem/9237

 

1654번: 랜선 자르기

첫째 줄에는 오영식이 이미 가지고 있는 랜선의 개수 K, 그리고 필요한 랜선의 개수 N이 입력된다. K는 1이상 10,000이하의 정수이고, N은 1이상 1,000,000이하의 정수이다. 그리고 항상 K ≦ N 이다. 그

www.acmicpc.net

접근 

1. CPU 스케쥴링 기법 중 SRT 와 유사한 문제이다.

 

 CPU 스케쥴링

hyunah030.tistory.com/4

 

 

풀이

1. 나무들을 내림차순으로 정렬한다.
2. 현재 나무를 키우는데 필요한 시간은 시간(1일) + 키우는데 필요한 시간(trees[i]) + 현재 날짜(index) 이다.
3. for문을 돌면서 키우는데 걸리는 시간 중 최대값을 찾는다.
4. 마지막으로 현재 날짜가 1일이므로 답에 1을 더해준다.

 

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

class Main {
    static int stoi(String s) {
        return Integer.parseInt(s);
    }

    static long stol(String s) {
        return Long.parseLong(s);
    }

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        int N = stoi(br.readLine());

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

        Arrays.sort(trees, Collections.reverseOrder());

        int answer = 0;
        for (int i = 0; i < trees.length; i++) {
            if(answer < trees[i] + i + 1){
                answer = trees[i] + i + 1;
            }
        }

        System.out.println(answer+1);
    }
}

'Algorithm' 카테고리의 다른 글

BOJ 7562 나이트의 이동  (0) 2020.06.23
BOJ 10816번 숫자 카드 2  (0) 2020.06.23
BOJ 1654번 랜선 자르기  (0) 2020.06.23
BOJ 1439번 뒤집기  (0) 2020.06.23
BOJ 15649번 N과 M (1)  (0) 2020.06.23