版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38749759/article/details/81390307
链接: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
题目不难,就是比较坑。。。。
思路
1:暴力求解
扫描二维码关注公众号,回复:
5892243 查看本文章
2:按平均每个床位低的方式求解
坑点:
1.当n个人按三人间分时,剩余1人时,可以有两种方案
<1>开一个新房(选便宜的二人间或三人间)
<2>退掉一个三人间,改成两个三人间
2.当n个人按二人间分时,剩余1人时,可以有两种方案
<1>开一个新房(选便宜的二人间或三人间)
<2>退掉一个双人间,改成三人间
只有把上述两种情况加入答案的讨论,就可以ac了。很无奈啊~~~~~
代码如下
1暴力
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll ans;
int main()
{
ll n,p2,p3;
while(cin>>n>>p2>>p3){
ll an1,an2,an3;
if(n<=2)
ans=min(p2,p3);
else if(n==3)
ans=min(2*p2,p3);
if(n>3){
ll t;
t=n; an1=(t/2)*p2;
t=n; an2=(t/3)*p3;
if(t%2){
ll tt=an1;
an1+=min(p2,p3) ;
if(t%2==1){//坑1
tt=tt-p2+p3;
an1=min(tt,an1);
}
}
if(t%3){
ll tt=an2;
an2+=min(p2,p3);
if(t%3==1){//坑2
tt=tt-p3+2*p2;
an2=min(tt,an2);
}
}
ans=min(an1,an2);
}
cout<<ans<<endl;
}
return 0;
}
思路2的方法
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
long long n,p1,p2;
while (cin>>n>>p1>>p2) {
long long ans=0;
double v1=p1/2.0,v2=p2/3.0;
if(v1<v2)
{
long long bns;
ans=n/2*p1;
if(n%2==1) {
bns=ans-p1+p2;
ans=ans+p1;
ans=min(bns,ans);
}
}
else
{
long long bns;
ans=n/3*p2;
if(n%3==1) {
bns=ans-p2+p1+p1;
ans=ans+p2;
ans=min(ans,bns);
}
else if(n%3==2) {
bns=ans+p1;
ans=min(ans+p2,bns);
}
}
cout<<ans<<endl;
}
return 0;
}
唉,不说了,自己还是太菜了,继续练习吧~~~