二分&&Block Towers

原创 2018年04月16日 20:47:11

题目链接:点击打开链接

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.

Input

The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000n + 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 36, and 9 blocks. The tallest tower has a height of 9 blocks.

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

二分实现:

#include<bits/stdc++.h>
using namespace std;
int n,m;
int f(int x)
{
    int num1=x/2;//2的数量
    int num2=x/3;//3的数量
    int num3=x/6;//2&&3的数量
    /*
    要同时满足
    除以2的数量大于等于n 
    除以3的数量大于m 
    除以2的数量+除以3的数量-除以6的数量大于等于m+n
    这三个条件时才成立*/
    if(num1<n||num2<m||num1+num2-num3<m+n) return 0;
    else return 1;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int l=0,r=1e7;
        int ans;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(f(mid)) ans=mid,r=mid-1;
            else l=mid+1;
        }
        printf("%d\n",ans);
    }
}

直接实现:

#include<bits/stdc++.h>
using namespace std;
int n,m;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
      for(int i=0;;i++)
      {
          if(i/2>=n&&i/3>=m&&i/2+i/3-i/6>=m+n)
          {
              printf("%d\n",i);
              break;
          }
      }
    }
    return 0;
}


CodeForces-626C-Block Towers

Description Students in a class are making towers of blocks. Each student makes a (non-zero) towe...
  • Ypopstar
  • Ypopstar
  • 2016-03-26 20:13:11
  • 468

Coderforce 626C Block Towers(脑洞啊)

C. Block Towers time limit per test 2 seconds memory limit per test 256 megabytes i...
  • yu_ch_sh
  • yu_ch_sh
  • 2016-02-14 23:17:58
  • 792

Codeforces 626C Block Towers

C. Block Towers time limit per test 2 seconds memory limit per test 256 megabytes i...
  • zwj1452267376
  • zwj1452267376
  • 2016-02-14 23:56:37
  • 365

Codeforces 626C Block Towers 【二分】

C. Block Towers time limit per test 2 seconds memory limit per test 256 megabytes input standa...
  • chenzhenyu123456
  • chenzhenyu123456
  • 2016-02-14 16:54:15
  • 480

CodeForces--626C--Block Towers (二分)

Block Towers Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %...
  • qq_29963431
  • qq_29963431
  • 2016-03-02 11:03:04
  • 279

CodeForces 626C - Block Towers(二分)

C. Block Towers time limit per test2 seconds memory limit per test256 megabytes inputstandard inp...
  • Snakeflute
  • Snakeflute
  • 2016-08-09 00:01:31
  • 157

CodeForces 626C Block Towers(二分)

http://codeforces.com/problemset/problem/626/C 题意:有n个人每次可以放两个积木,m个人,每次可以放3个积木,最后每个人堆得高度都不同,求最大的高度最小...
  • w144215160044
  • w144215160044
  • 2016-05-12 14:10:43
  • 154

Codeforces 626C 简单题

题意:求出2的倍数n个,3的倍数m个,但是每个数只能出现一次,也就是说,6的倍数只能出现一次,问最后出现的最大的数? 分析:这题很简单,关键就是怎么判断6的倍数只用了一次,我的思路是先找出全部把...
  • loveyou11111111
  • loveyou11111111
  • 2016-02-18 16:08:51
  • 406

CodeForces-626C Block Towers 二分例题 & 贪心

题目链接: CF-626C大意: 给两个数字 n,m。 求构造出 n 个 2 的倍数,m 个 3 的倍数,数字各不相同,求最大值的最小值。思路: 用二分的方法确定。首先确定题目能否用二分思想:1...
  • Joovo
  • Joovo
  • 2017-08-12 01:53:36
  • 242

CodeForces 478D-Red-Green Towers

题目 D. Red-Green Towers time limit per test 2 seconds memory limit per test 256 me...
  • Druning
  • Druning
  • 2016-07-11 18:12:13
  • 524
收藏助手
不良信息举报
您举报文章:二分&&Block Towers
举报原因:
原因补充:

(最多只允许输入30个字)