标题:plan | 时间限制:1秒 | 内存限制:256M
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<=n<=10^9
1<=p2,p3<=10^9
示例1
输入
4 2 3
输出
4
示例2
输入
5 1 3
输出
3
题目大意:n 人住宿,有两人间和三人间可以选,问怎样住费用最少。
分析:类别一下性价比,每间房住满,每个人平均要花费多少。
分情况讨论,
如果双人房性价比高,那么先把双人房住满,此时如果还有剩余的一个人(只可能剩一个人);这个人,可以去住一间双人房(相当于后面三个人两间双人房)或者去和前面的两人一起去住三人房(三个人一间三人房),在这里就需要再次判断一下
如果三人房性价比高,那么先把三人房住满,此时剩余可能是1或2;如果是2,那么只要选一间便宜的住即可,如果是1,那么这个人,可以去住一间三人房(相当于后面四个人两间三人房)或者和前面的三人去住两间双人房(四个人两间双人房),在这里也需要再次判断
如果两者性价比相同,那么必然是住双人房,直接求即可。
最初ac代码:
#include<iostream>
#include<cmath>
#define LL unsigned long long
using namespace std;
int main()
{
LL n,p2,p3,sum=0;
scanf("%lld%lld%lld",&n,&p2,&p3);
LL num2=n/2,num3=n/3;//有几间可以完全住下
LL sh2=n%2,sh3=n%3;//剩余的人数<=1,<=2;
if(p2*3<p3*2)
{
sum=p2*num2;
if(sh2!=0)
{
if(p2*2>p3)
{
sum+=p3;
sum-=p2;
}
else{
sum+=p2;
}
}
}
else if(p2*3==p3*2)
{
if(n<=1)
sum+=p2;
else
sum=n*(p2/2);
}
else
{
sum=p3*num3;
if(sh3!=0)
{
if(sh3==2)
sum+=min(p2,p3);
else
{
if(p2<=p3)
{
sum+=2*p2;
sum-=p3;
}
else
{
sum+=p3;
}
}
}
}
printf("%lld\n",sum);
return 0;
}
简化后代码:
int n,p2,p3;
int main()
{
cin>>n>>p2>>p3;
if(n<=2)
cout<<min(p2,p3);
else if(n==3)
cout<<min(2*p2,p3);
else if(p2*3<p3*2)
{
if(n%2)
cout<<(n-3)/2*p2+min(2*p2,p3);
else
cout<<p2*(n/2);
}
else
{
if(n%3==0)
cout<<n/3*p3;
else if(n%3==1)
cout<<(n-4)/3*p3+min(2*p2,2*p3);
else if(n%3==2)
cout<<n/3*p3+min(p2,p3);
}
return 0;
}