多校牛客5-----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

题目不难,就是比较坑。。。。

思路

1:暴力求解

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

唉,不说了,自己还是太菜了,继续练习吧~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值