(第五场)J plan 【贪心】

题目链接:https://www.nowcoder.com/acm/contest/143/J

题目描述 

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

 

题目大意:

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

官方题解:

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

大概思路:

因为从全局来看我们要多选性价比高的房间, 所以模拟一下分为两种大情况,而每种小情况的最后可能刚刚好住满,可能有剩余,如果双人房性价比高,那么最后有可能会剩下一个可怜的家伙,那时我们要考虑单个住便宜或是跟前面的合住三人房便宜了;如果三人房性价比高,那么最后可能剩下一个人,可能剩下两个人,综上所述,数量用的最少的房间不超过3.

 

AC code:

 1 #include <bits/stdc++.h>
 2 #define INF 0x3f3f3f3f
 3 #define ll long long int
 4 using namespace std;
 5 
 6 ll N, p2, p3, u;
 7 
 8 int main()
 9 {
10     scanf("%lld%lld%lld", &N, &p2, &p3);
11     double xj_1 = p2/2.0;
12     double xj_2 = p3/3.0;
13     long long int ans = 0;
14     if(xj_1 <= xj_2)
15     {
16         if(N%2)
17         {
18             ans = (N/2-1)*p2 + min(p2*2, p3);
19         }
20         else ans = (N/2)*p2;
21     }
22     else
23     {
24         if(N%3 == 1)
25         {
26             ans = (N/3-1)*p3 + min(p2*2, p3*2);
27         }
28         else if(N%3 == 2)
29         {
30             ans = (N/3-1)*p3 + min(p2+p3, p3*2);
31         }
32         else
33         {
34             ans = N/3*p3;
35         }
36     }
37     printf("%lld\n", ans);
38     return 0;
39 }

 

转载于:https://www.cnblogs.com/ymzjj/p/9424908.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值