牛客网暑期ACM多校训练营(第五场)
J-plan(贪心)
题目描述
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个学生全部住进2人间或者3人间(可以不住满)后使得总花费最少。典型的贪心策略,考虑性价比,设2人间的单价为x/间,性价比为x/2,3人间的单价为y/间,性价比为y/3,分为两种情况:一、如果3*x>2*y,表示应选3人间比较划算,那么有3种贪心可能:①n%3余数为0,全选3人间;②余数为1,全选3人间,或者最后1个学生选2人间,或者最后4个学生选2人间;③余数为2,全选3人间,或者最后2个学生选2人间;二、如果3*x<=2*y,表示选2人间比较划算,那么有2种贪心可能:①n%2余数为1,全选2人间,或者最后一个学生选3人间;②余数为0,全选2人间。
AC代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL;LL n,x,y; 4 int main(){ 5 while(cin>>n>>x>>y){ 6 if(3*x>2*y){ 7 if(!(n%3))cout<<n/3*y<<endl; 8 else if(n%3==1)cout<<min(min((n/3+1)*y,n/3*y+x),(n/3-1)*y+2*x)<<endl; 9 else cout<<min((n/3+1)*y,n/3*y+x)<<endl; 10 } 11 else{ 12 if(n%2)cout<<min((n/2+1)*x,(n/2-1)*x+y)<<endl; 13 else cout<<n/2*x<<endl; 14 } 15 } 16 return 0; 17 }