牛客多校第五场 J: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

题意

n个人住宾馆,二人房价钱p2,三人房价钱p3 。求这n个人全部住进宾馆的最少花费

思路

暴力进行判断

先对1个人进行特判:最少的花费肯定是min(p2,p3)。

然后因为二人房和三人房有一定的性价比(即平均每个人的住房花费)。所以入住的时候肯定要尽可能多的选择性价比高的房间(平均每人花费最少),所以可以选择:全部是二人间,全部是三人间,如果有剩余,让剩下的人住二人间或三人间,对于所有的情况取最小值就是最小花费。

大概就是这样,代码里写的有点麻烦,好像有些情况用不到,这样写情况容易考虑不全,WA了15次之后才把所有的情况写全。不知道大佬们都是怎么写的。如果有更好的思路,欢迎交流٩(๑>◡<๑)۶ 

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e6+10;
using namespace std;
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	ll n,p2,p3;
	ll ans=0;
	ll flag1=0,flag2=0;
	while(cin>>n>>p2>>p3)
	{
		flag1=n%2;
		flag2=n%3;
		if(n==1)
		{
			cout<<min(p2,p3)<<endl;
			continue;
		}
		if(flag1==0&&flag2==0)
			ans=min(n/2*p2,n/3*p3);
		else if(flag1==0&&flag2)
		{
			ll x=n/3;
			if(flag2==2)
				ans=min(n/2*p2,min(x*p3+p2,min((x-1)*p3+3*p2,(x+1)*p3)));
			if(flag2==1)
				ans=min(n/2*p2,min(x*p3+p2,min((x+1)*p3,min(p2*(n/2-1)+p3,p3*(x-1)+p2*2))));
		}
		else if(flag1&&flag2==0)
		{
			ll y=n/2;
			ans=min((y+1)*p2,min(n/3*p3,(y-1)*p2+p3));
		}
		else if(flag1&&flag2)
		{
			ll x=n/2;
			ll y=n/3;
			if(flag2==1)
				ans=min((x+1)*p2,min((y+1)*p3,min((y-1)*p3+2*p2,min(p2*(x-1)+p3,y*p3+p2))));
			else if(flag2==2)
				ans=min((x+1)*p2,min((y+1)*p3,min(y*p3+p2,min((x-1)*p2+p3,(y-1)*p3+3*p2))));
		}
		cout<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wang_123_zy/article/details/81360121