본문 바로가기
백준/20001 - 25000

[백준] 21176번 : Smoothie Stand(JAVA)

by lms0806 2021. 7. 26.
728x90
반응형

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

 

 

21176번: Smoothie Stand

The first line contains two integers $k$ and $r$, separated by a space. The value $k$ is the number of different ingredients Olivia uses in her smoothies and $r$ is the number of different recipes she makes. You may assume $1 \le k \le 100\,000$, $1

www.acmicpc.net

풀이

K(재료 갯수), R(레시피 갯수)를 입력받습니다.

첫줄은 소유하고 있는 재료의 갯수들, 다음줄부터 레시피에 필요한 재료의 갯수들과 마지막에 금액이 입력됩니다.

소유하고 있는 재료들를 사용하여 금액을 판매할 수 있는 최대 금액을 구하는 문제입니다.

 

처음에 받은 재료의 갯수를 배열에 저장시켜두고,

레시피의 갯수만큼 반복하면서 min(소유재료갯수 / 사용재료갯수)를 통해 해당 레시피를 만들 수 있는 수량을 구합니다.

그리고 수량을 마지막에 입력받은 금액과 곱한 값의 최대값을 구해주면 되는 문제입니다.

 

소스코드

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

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 k = Integer.parseInt(st.nextToken()), r = Integer.parseInt(st.nextToken());
		
		int[] num = new int[k];
		
		st = new StringTokenizer(br.readLine());
		for(int i = 0; i < k; i++) {
			num[i] = Integer.parseInt(st.nextToken());
		}
		
		int max = 0;
		while(r --> 0) {
			st = new StringTokenizer(br.readLine());
			
			int min = 100000;
			for(int i = 0; i < k; i++) {
				int n = Integer.parseInt(st.nextToken());
				if(n != 0) {
					min = Math.min(min, num[i] / n);
				}
			}
			
			max = Math.max(max, min * Integer.parseInt(st.nextToken()));
		}
		System.out.print(max);
	}
}

 

 

첫솔입니다!

728x90
반응형

댓글