1. 程式人生 > >(第五場)J plan 【貪心】

(第五場)J plan 【貪心】

quest 貪心 bit nts fine tps amp price 我們

題目鏈接: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 }

(第五場)J plan 【貪心】