본문 바로가기

코드포스

[코드포스 Practice9] A. Water Buying

Polycarp wants to cook a soup. To do it, he needs to buy exactly n liters of water.

Polycarp은 스프를 요리하고 싶다. 그렇게 하기 위해서, 그는 정확히 n 리터의 물을 사야한다. 


There are only two types of water bottles in the nearby shop — 1-liter bottles and 2-liter bottles. There are infinitely many bottles of these two types in the shop.

근처 상점에는 오직 두 종류의 물병이 있다. -- 1L 물병과 2L 물병임. 가게에는 이 두 종류의 물병이 무한개 있다. 

The bottle of the first type costs a burles and the bottle of the second type costs b burles correspondingly.

첫 번째 종류의 물병은 a burles (단위인 것 같음)에 상응하는 비용이 들고, 두 번째 종류의 물병은 b burles에 상응하는 비용이 든다.


Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly n liters of water in the nearby shop if the bottle of the first type costs a burles and the bottle of the second type costs b burles.

Polycarp은 가능한 적은 돈을 쓰려고 한다. 너가 해야할 일은 만약 첫 번째 종류의 병이 a burles가 들고 두 번째 종류의 병이 b burles가 든다면, Polycarp이 근처 상점에서 정확히 nL의 물을 사는데 쓰는 가장 적은 돈을 구하는 것이다. 

You also have to answer q independent queries.

서로 다른 q개의 질문에 답해야 한다.


Input
The first line of the input contains one integer q (1≤q≤500) — the number of queries.

입력의 첫 줄에는 하나의 정수 q가 있다. -- 문제의 개수임


The next n lines contain queries. The i-th query is given as three space-separated integers ni, ai and bi (1≤ni≤1012,1≤ai,bi≤1000) — how many liters Polycarp needs in the i-th query, the cost (in burles) of the bottle of the first type in the i-th query and the cost (in burles) of the bottle of the second type in the i-th query, respectively.

다음 n개의 줄에는 문제들이 있다. i번째 문제는 " " 공백으로 분리된 세개의 정수 n1, a1, b1가 있다. -- 각각은 i번째 문제에서 Polycarp이 필요한 리터이고, i번째 문제에서 첫 번째 종류의 병의 비용이고, i번째 문제에서 두 번째 종류의 병의 비용임.

(끝에 붙은 respectively는 각각이라는 뜻임 여러 개 설명해서 붙은듯)

Output
Print q integers. The i-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly ni liters of water in the nearby shop if the bottle of the first type costs ai burles and the bottle of the second type costs bi burles.

q개의 정수를 출력한다. i 번째 정수는 만약 첫 번째 종류의 병이 a burles가 들고 두 번째 종류의 병이 b burles가 든다면, Polycarp이 근처 상점에서 정확히 nL의 물을 사는데 쓰는 가장 적은 비용과 같아야 한다. 

 


 

양념치킨 문제처럼 섞는다면 모를까 단순히 두 개로 비교한다면 가장 단가가 싼 걸로 사면 된다. 

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
 
using namespace std;
using i64 = long long;
 
int main() {
    int q;
    scanf("%d", &q);
    
    for(int i = 0; i < q; i++){
        i64 n, a, b;
        scanf("%lld %lld %lld", &n, &a, &b);
        
        if(2*a < b){
            printf("%lld\n", n*a);
        }
        else {
            printf("%lld\n", n/2*b + n%2*a);
        }
    }
    
    
    return 0;
}

1L병은 나머지가 없으므로 그냥 사면 되는데 2L병으로 사게 된다면 홀수일 경우 나머지가 있으므로 그건 1L로 채운다.