CF 626C Block Towers 二分

版权声明:本文为博主原创文章,转载请标明原博客。 https://blog.csdn.net/sdau_fangshifeng/article/details/86534589

题目链接:

http://codeforces.com/problemset/problem/626/C

C. Block Towers

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

扫描二维码关注公众号,回复: 4964412 查看本文章

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.

Examples

input

Copy

1 3

output

Copy

9

input

Copy

3 2

output

Copy

8

input

Copy

5 0

output

Copy

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米高的木板堆塔,有m个人用3米高的木板堆塔。木板可以随便使用,要求任意两人堆成塔的高度不一,问满足该条件下所有人堆成塔的最大高度的最小值。

思路:

最大值的最小化问题,二分找最值。

This is the code 

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
bool check(int num,int n,int m)
{
    int o2=num/2;
    int o3=num/3;
    int o6=num/6;
    if(n<=o2 && m<=o3 && n+m<=o2+o3-o6)
        return true;
    return false;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int l=2;//注意范围
        int r=3000000;
        int mid;
        while(l<=r)
        {
            mid=(l+r)>>1;
            if(check(mid,n,m))
                r=mid-1;
            else
                l=mid+1;
        }
        printf("%d\n",r+1);//因为最后一个满足的mid传给r的时候是mid-1,所以要加一输出
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdau_fangshifeng/article/details/86534589