牛客网暑期ACM多校训练营(第五场)J-plan (模拟)

题目描述
n 个人出去玩,给定双人房和三人房的价格,求最少的住宿花费
1<=n<=10^9

解题思路
脑补一下可以发现:最后答案一定是几乎全选性价比最高的那种房间
然后再加上几间其他的
所以二人间和三人间里数量用的最少的房间不会超过3
枚举一下用了几间就好了

一定不要忘记n%2==1时可以把两个人和另一个人合住一间三人间的情况!!!(因为这wa哭了。。。)

代码:

#include <iostream>
#include <cstdio>
 
using namespace std;
 
long long n,p2,p3;
double x1,x2;
 
 
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
 
int main()
{
    n=read();p2=read();p3=read();
    x1=(double)p2/2;x2=(double)p3/3;
    long long sum=0;
    if(n==1)
    {
        if(p3>p2){
            cout<<p2<<endl;
            return 0;
        }
        else {cout<<p3<<endl;return 0;}
    }
    if(x1<=x2){
        if(n%2==1)
        {
            if(2*p2<=p3)
            {
                sum=(n/2)*p2+p2;
            }
            else {sum=(n/2-1)*p2+p3;}
        }
        else sum=n/2*p2;
    }
    else if(x1>x2)
    {
        if(n%3==1)
        {
            if(p2<p3)
            {
                sum=(n/3-1)*p3+2*p2;
            }
            else {sum=p3+n/3*p3;}
        }
 
        else if(n%3==2)
        {
            if(p2>p3)
            {
                sum=n/3*p3+p3;
            }
            else sum=p2+n/3*p3;
        }
        else sum=n/3*p3;
    }
    cout<<sum<<endl;
 
    return 0;
}
原文地址:https://www.cnblogs.com/Fy1999/p/9410346.html