백준 문제 풀이 (1049번) java

tae_in·2022년 9월 7일
0

알고리즘

목록 보기
10/12

문제

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

풀이

이 문제는 M개의 브랜드 줄 중 어느것이 6개 짜리 줄의 가격(all)과 한 줄의 가격(one)이 최소값인지 구하고 가장 최소값인 6개 짜리 줄 가격(all[0])과 가장 최소값의 한개 짜리 줄(one[0]) * 6의 각격 중 어느 것이 저렴한지 구해서 N개의 줄을 사는 최소값을 구하는 그리디 알고리즘 문제이다.

코드

package Category;

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

public class q_1049 {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        st = new StringTokenizer(br.readLine(), " ");

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

        int[] all = new int[M]; 
        int[] one = new int[M];

        for (int i = 0; i < M; i++) {

            st = new StringTokenizer(br.readLine(), " ");

            all[i] = Integer.parseInt(st.nextToken());
            one[i] = Integer.parseInt(st.nextToken());
        }
		
        // 최소값을 구하기 위해서 정렬
        Arrays.sort(all);  
        Arrays.sort(one);

        if (all[0] < one[0] * 6) {
            System.out.println(all[0] * (N / 6) + Math.min(all[0], one[0] * (N % 6)));
        }
        else {
            System.out.println(one[0] * N);
        }
    }
}

0개의 댓글