J - Block Towers

学生用不同数量的积木建塔,要求高度各不相同。目标是最小化最高塔的高度。算法通过调整2块和3块积木的组合,找到最小可能的最大塔高。

Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks.

The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers.

Input

The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively.

Output

Print a single integer, denoting the minimum possible height of the tallest tower.

Sample 1

InputcopyOutputcopy
1 3
9

Sample 2

InputcopyOutputcopy
3 2
8

Sample 3

InputcopyOutputcopy
5 0
10

Note

In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks.

In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.

题意翻译

一群人在搭建积木塔。 有N个人2块2块往上搭,有M个人3块3块往上搭,每个人塔的高度都要大于0,且高度各不相同,问最高的塔高度的最小值。(0<=n,m<=1000000)

#include<iostream>
#include<algorithm>
#include<queue>
 using namespace std;
 typedef long long ll;

int main()
{
	int n, m; cin >> n >> m;
	int t2 = 2 * n, t3 = 3 * m;
	while (n >= 3 && m >= 2)  //循环继续的条件是当塔2和塔3均存在6的倍数时
	{
		if (t2 <= t3)
		{
			t2 += 2;
			n++;
		}
		else
		{
			t3 += 3;
			m++;
		}
		n -= 3;  //每次从塔2和塔3分别拿走一个6的倍数
		m -= 2;

		//每次循环结束,我们默认把刚从两个塔拿出来的6的倍数加入未增加高度的塔中
		//这样就可以保证在没有重复高度下得到最高高度的最小值了
	}
	cout << max(t2, t3) << endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值