๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[Codeforces] 1207A: There Are Two Types Of Burgers

by ์ฃผ๋ฐœ2 2020. 3. 10.
๋ฐ˜์‘ํ˜•

https://codeforces.com/problemset/problem/1207/A

 

Problem - 1207A - Codeforces

 

codeforces.com

์ฝ”๋“œ

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 bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());

		int t = Integer.parseInt(st.nextToken());	// ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜
		for(int tc=0; tc<t; tc++) {
			int price = 0;	// ์ตœ๋Œ€ ๊ฐ€๊ฒฉ
			st = new StringTokenizer(bf.readLine());
			int b = Integer.parseInt(st.nextToken());	// buns
			int p = Integer.parseInt(st.nextToken());	// beef
			int f = Integer.parseInt(st.nextToken());	// chicken

			st = new StringTokenizer(bf.readLine());
			int h = Integer.parseInt(st.nextToken());	// ํ–„๋ฒ„๊ฑฐ ๊ฐ€๊ฒฉ
			int c = Integer.parseInt(st.nextToken());	// ์น˜ํ‚จ๋ฒ„๊ฑฐ ๊ฐ€๊ฒฉ
			
			// ํ–„๋ฒ„๊ฑฐ์™€ ์น˜ํ‚จ๋ฒ„๊ฑฐ์ค‘ ๋น„์‹ผ๊ฑฐ ๋ถ€ํ„ฐ ํŒ”๊ธฐ
			if(h>c) {
				// ํ–„๋ฒ„๊ฑฐ๋ถ€ํ„ฐ ํŒ”๊ธฐ
				while(b>1 && p>0) {
					b -= 2;
					p -= 1;
					price += h;
				}
				// ํ–„๋ฒ„๊ฑฐ๋ฅผ ๋‹ค ํŒ ํ›„ ์น˜ํ‚จ๋ฒ„๊ฑฐ ํŒ”๊ธฐ
				while(b>1 && f>0) {
					b -= 2;
					f -= 1;
					price += c;
				}
			}
			else {
				// ํ–„๋ฒ„๊ฑฐ๋ฅผ ๋‹ค ํŒ ํ›„ ์น˜ํ‚จ๋ฒ„๊ฑฐ ํŒ”๊ธฐ
				while(b>1 && f>0) {
					b -= 2;
					f -= 1;
					price += c;
				}
				// ํ–„๋ฒ„๊ฑฐ๋ถ€ํ„ฐ ํŒ”๊ธฐ
				while(b>1 && p>0) {
					b -= 2;
					p -= 1;
					price += h;
				}
			}
			System.out.println(price);
		}
	}
}

ํ’€์ด

/* ๋ณ€์ˆ˜ */

t = ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜

price = ์ถœ๋ ฅํ•  ์ตœ๋Œ€ ๊ฐ€๊ฒฉ

b = ํ–„๋ฒ„๊ฑฐ, ์น˜ํ‚จ๋ฒ„๊ฑฐ ๋งŒ๋“ค๋•Œ ํ•„์š”ํ•œ buns ์žฌ๋ฃŒ(2๊ฐœ์”ฉ ํ•„์š”)

p = ํ–„๋ฒ„๊ฑฐ ๋งŒ๋“ค๋•Œ ํ•„์š”ํ•œ ์žฌ๋ฃŒ - 1๊ฐœ ํ•„์š”

f = ์น˜ํ‚จ๋ฒ„๊ฑฐ ๋งŒ๋“ค๋•Œ ํ•„์š”ํ•œ ์žฌ๋ฃŒ - 1๊ฐœ ํ•„์š”

h - ํ–„๋ฒ„๊ฑฐ ํ•œ๊ฐœ๋‹น ๊ฐ€๊ฒฉ

c = ์น˜ํ‚จ๋ฒ„๊ฑฐ ํ•œ๊ฐœ๋‹น ๊ฐ€๊ฒฉ

 

์ตœ๋Œ€๊ฐ€๊ฒฉ์œผ๋กœ ํ–„๋ฒ„๊ฑฐ๋ฅผ ํŒ๋งคํ•˜๋ ค๋ฉด, ํ–„๋ฒ„๊ฑฐ๋‚˜ ์น˜ํ‚จ๋ฒ„๊ฑฐ์ค‘ ๊ฐ€๊ฒฉ์ด ๋น„์‹ผ๊ฑธ ๊ฐ€์žฅ ๋งŽ์ด ํŒ๋งคํ•˜๋ฉด ๋œ๋‹ค.

๋”ฐ๋ผ์„œ h, c์˜ ๊ฐ€๊ฒฉ๋ณ„๋กœ ์กฐ๊ฑด์„ ๋‚˜๋ˆ„๊ณ  ๋” ๋น„์‹ผ๊ฑฐ๋ถ€ํ„ฐ ์žฌ๋ฃŒ๊ฐ€ ์†Œ์ง„๋  ๋•Œ ๊นŒ์ง€ ํŒ๋งคํ•œ๋‹ค.

 

๊ทธ ํ›„ ์žฌ๋ฃŒ๊ฐ€ ์†Œ์ง„๋˜์—ˆ์œผ๋ฉด, ๋‚จ์€์žฌ๋ฃŒ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋‚˜๋จธ์ง€ ๋ฒ„๊ฑฐ๋ฅผ ๋งŒ๋“ค์–ด ํŒ๋งคํ•œ๋‹ค.

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€