牛客-plan

链接:https://www.nowcoder.com/acm/contest/143/J
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

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

题解:题目要求最小的花费,比较性价比,做出大部分选择三人间还是二人间;

           其中有两个特例,就是在选择二人间后,如果还剩下一个人,此时需要比较两间二人间的花费和一间三人间的花费选择最小的;

           另一个是,在选择三人间后,如果还剩下一个人,此时需要比较两个二人间和两个三人间的价格选择最小;

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,a,b,i,j,k,sum=0;
    cin>>n>>a>>b;
    double x=a,y=b;
    x/=2;
    y/=3;
    if(n>2)
    {
 
        if(x<=y)
        {
            sum+=(n/2)*a;
            if(n%2==1)//特例
            {
                if(2*a>b)
                {
                    sum-=a;
                    sum+=b;
                }
                else
                {
                    sum+=a;
                }
            }
        }
        else
        {
            sum+=(n/3)*b;
            if(n%3==2)
            {
                if(a>b)
                {
                    sum+=b;
                }
                else
                {
                    sum+=a;
                }
            }
            if(a<b&&n%3==1)//特例
            {
                sum-=b;
                sum+=(2*a);
            }
 
        }
 
    }
    else//小于等于2时不需要考虑,直接选择最小值
    {
        sum=min(a,b);
    }
    cout<<sum<<endl;
}
扫描二维码关注公众号,回复: 2700943 查看本文章

猜你喜欢

转载自blog.csdn.net/WN_795/article/details/81381615