牛客网暑期ACM多校训练营(第五场)j - plan(贪心)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sxh759151483/article/details/81390976

There are n students going to travel. And hotel has two types room:double room and triple room. The price of a double room is p2 and the price of a triple room is p3

Now you need to calulate the minimum total cost of these students.

输入描述:

The first line has three integers n, p2, p3

输出描述:

Output the minimum total cost.

示例1

输入

复制

4 2 3

输出

复制

4

示例2

输入

复制

5 1 3

输出

复制

3

备注:

1<=n<=10^9

1<=p2,p3<=10^9

贪心,优先选性价比高的房间,如果有多余出来的人再看选的是2还是3,拿出来一个2或者3,再找最优选法

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n, p2, p3;
    scanf("%lld%lld%lld", &n, &p2, &p3);
    ll ans = 0;
    if(p2 / 2.0 <= p3 / 3.0){
        if(n % 2)
            ans = (n / 2 - 1) * p2 + min(p2 * 2, p3);
        else
            ans = n / 2 * p2;
    }
    else{
        if(n % 3 == 0){
            ans = n / 3 * p3;
        }
        else if(n % 3 == 1){
            ans = (n / 3 - 1) * p3 + min(p2 * 2, p3 * 2);
        }
        else{
            ans = (n / 3 - 1) * p3 + min(p2 + p3, p3 * 2);
        }
    }
    printf("%lld\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sxh759151483/article/details/81390976