PS

백준 5545. 최고의 피자

tose33 2021. 8. 25. 18:45

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

 

5545번: 최고의 피자

첫째 줄에 토핑의 종류의 수 N(1 ≤ N ≤ 100)이 주어진다. 둘째 줄에는 도우의 가격 A와 토핑의 가격 B가 주어진다. (1 ≤ A, B ≤ 1000) 셋째 줄에는 도우의 열량 C가 주어진다. (1 ≤ C ≤ 10000) 다음 줄

www.acmicpc.net

 

토핑의 칼로리를 내림차순으로 정렬하고 칼로리가 높은 토핑부터 하나씩 더해가며 1원당 열량이 높은 값을 갱신한다.

주의할점은 토핑을 하나도 선택하지 않을수도 있다.

 

#include <iostream>
#include <algorithm>
using namespace std;

int n, doughPrize, toppingPrize, doughCal;
int toppingCal[10010];

int main()
{
    cin >> n >> doughPrize >> toppingPrize >> doughCal;
    for(int i = 0; i < n; i++)
        cin >> toppingCal[i];

    sort(toppingCal, toppingCal+n, greater<>());

    int cal = doughCal;
    int prize = doughPrize;
    // 토핑을 전혀 선택하지 않는 경우
    int ans = doughCal / doughPrize;
    for(int i = 0; i < n; i++)
    {
        cal += toppingCal[i];
        prize += toppingPrize;

        ans = max(ans, cal/prize);
    }

    cout << ans;
}