๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] 1049 Swift ๊ธฐํƒ€์ค„ Greedy

by Jouureee 2022. 4. 13.

๋ฌธ์ œ : 

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

 

1049๋ฒˆ: ๊ธฐํƒ€์ค„

์ฒซ์งธ ์ค„์— N๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๊ณ , M์€ 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ๋ธŒ๋žœ๋“œ์˜ ํŒจํ‚ค์ง€ ๊ฐ€๊ฒฉ๊ณผ ๋‚ฑ๊ฐœ์˜ ๊ฐ€๊ฒฉ์ด ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ฃผ

www.acmicpc.net

๋ถ„์„ : 

๋‹จ์ˆœํ•œ ๊ตฌํ˜„ ๋ฌธ์ œ์˜€๋‹ค. ๋ชจ๋“  ๋ธŒ๋žœ๋“œ ์ค‘์—์„œ ํŒจํ‚ค์ง€์™€ ๋‹จํ’ˆ์œผ๋กœ ๊ตฌ๋งคํ–ˆ์„๋•Œ ๊ฐ€๊ฒฉ ๋น„๊ต๋ฅผ ํ†ตํ•ด ์ตœ์†Œ๊ฐ’์„ choose ๋ณ€์ˆ˜์™€ select์— ์ €์žฅํ•œ ๋’ค, n์˜ ๊ฐ’์„ ์ค„์—ฌ๋‚˜๊ฐ€๋ฉฐ minPrice๋ฅผ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.

 

Swift ์ฝ”๋“œ :

//
//  main.swift
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2022/04/13.
//

import Foundation

var line = readLine()!.split(separator: " ").map { Int(String($0))! }
var n = line[0]
var Tcase = line[1]
var brand = [(Int, Int)]()

for i in 0..<Tcase {
    var line = readLine()!.split(separator: " ").map { Int(String($0))! }
    brand.append((line[0], line[1]))
}

var minPrice = 0
while n != 0 {
    var select = 100000
    for i in brand {
        var choose = 100000
        if n < 6 {
            //choose package or one
            choose = min(i.0, i.1 * n)
        }else {
            choose = min(i.0, i.1 * 6)
        }
        select = min(select, choose)
    }
    minPrice += select // ์ตœ์†Œ๊ฐ’ ์—…๋ฐ์ดํŠธ

    if(n < 6) {
        break
    }else {
        n = n - 6
    }
    
}

print(minPrice)

๋Œ“๊ธ€