牛客-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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章