牛客网暑期ACM多校训练营(第五场) 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元,问最小花费;

思路:看6个人住的花费,因为这样每个房间是住满的,且是最小单元,如果3人间花费少,就住三人,否则双人,然后再讨论住不满的情况,假如住三人间,剩余两人的话有两种选择,一是住双人间,二是住三人间,哪个花费少就住哪个,剩余一人的话有三种选择,住双人或者三人间,或者将一个三人间换成两个双人间,哪个少选哪个,双人间一样;

下面附上我的代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<map>
#include<set>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define LL long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define lson o<<1
#define rson o<<1|1
using namespace std;
LL n, p2, p3;
int main()
{
	LL sum = 0;
	cin >> n >> p2 >> p3;
	if(n == 1)
		printf("%lld", min(p2, p3));
	else{
		if(3 * p2 >= 2 * p3)
		{
			sum += n / 3 * p3;
			if(n % 3 == 1)
				sum += min(min(2 * p2 - p3, p2), p3);
			else if(n % 3 == 2)
				sum += min(p2, p3);
		}
		else{
			sum += n / 2 * p2;
			if(n % 2)
				sum += min(min(p3 - p2, p2), p3);
		}
		printf("%lld\n", sum);
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/gtuif/article/details/81740221
今日推荐