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 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
1 3
Output
9
Input
3 2
Output
8
Input
5 0
Output
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.

题目链接:http://codeforces.com/contest/626/problem/C

分析:

题意:

你需要找n个2的倍数,m个3的倍数,要求所有数都不一样

你的目的是使得其中最大的数最小,然后问你这个数是多少呢?

(0 ≤ n, m ≤ 1 000 000, n + m > 0)

那么我们怎么做呢?

我们首先第一步,认识到这个答案是具有单调性的!

如果T这个值是符合答案的,那么存在To>T,To显然也是符合答案的。

因为To范围内的数显然比T多,而且To完全可以和T选取一模一样的数出来。

反之,如果T是不符合答案的,如果To<T,显然To也是不符合答案的。

然后我们就可以开始二分答案咯。

我们把问题转换为,给你T,问你T范围内,是否能够分离出n个2的倍数,和m个3的倍数,并且这些数都是不一样的呢?

我们可以知道,T范围内,是2的倍数的数有T/2个,是3的倍数的有T/3个,即是2又是3的倍数的数,有T/6个,那么只要(max(0,n-(T/2-T/6))+max(m-(T/3-T/6),0))<=T/6就好了!

我们就二分答案,不断check就好了。

我们首先找到答案的下界,l = 0,以及答案的上界r = 3*1000000。

然后我们不断check((l+r)/2),如果(l+r)/2是符合答案的,那么根据单调性,显然[(l+r)/2,+∞)这个范围,都是符合答案的,那么我们令r = mid。如果(l+r)/2不符合答案,同理,根据单调性,(-∞,(l+r)/2]都是不符合答案的,我们就令l=mid。

然后这样不断的进行check,直到l > r的时候,这样我们就能找到那个不符合和符合的分界线了~

这条分界线,显然就是答案咯。

喵,每一次check的时候,时间复杂度是O(1),我最多check logn次,所以二分的总时间复杂度是O(logn)的,完全可以接受。

附上小图:(丑图一张)

下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
int n,m;
bool check(int x)
{
int num1=x/;
int num2=x/;
int num3=x/;
if(num1<n)
return false;
if(num2<m)
return false;
if(min(num3,num1-n)<m-(num2-num3))
return false;
return true;
}
int main()
{
scanf("%d%d",&n,&m);
int l=,r=1e7,ans=;
while(l<=r)
{
int mid=(l+r)/;
if(check(mid))
{
ans=mid;
r=mid-;
}
else l=mid+;
}
cout<<ans<<endl;
return ;
}

Codeforces 626C Block Towers(二分)的更多相关文章

  1. Codeforces 626C Block Towers「贪心」「二分」「数学规律」

    题意: 一堆人用方块盖塔,有n个人每次只能加两块方块,有m个人每次只能加三块方块.要求每个人盖的塔的高度都不一样,保证所用方块数最少,求最高的塔的高度. 0<=n+m  0<=n,m< ...

  2. CodeForces 626C Block Towers

    构造AC的.左右两边都先不用6的倍数,然后哪边数字大那一边往回退一下,然后再比较哪边数字大.......直到结束 #include<cstdio> #include<cstring& ...

  3. 8VC Venture Cup 2016 - Elimination Round C. Block Towers 二分

    C. Block Towers 题目连接: http://www.codeforces.com/contest/626/problem/C Description Students in a clas ...

  4. codeforce626C.Block Towers(二分)

    C. Block Towers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  5. Codeforces 626C

                                                                                                        ...

  6. CodeForces - 327D Block Tower

    D. Block Tower time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  7. [Codeforces 1199C]MP3(离散化+二分答案)

    [Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...

  8. 【CodeForces 626C】Block Towers

    题意 给你n,m,如果 n个2的倍数和m个3的倍数,这n+m个数各不相同,那么求最大的数的最小值. 分析 方法1:枚举最大值为i,直到 i/2+i/3-i/6(不重复的2或3的倍数)≥n+m,并且要i ...

  9. (二分)Block Towers(cf626)

    http://www.codeforces.com/contest/626/problem/C 题意是有一群小朋友在堆房子,现在有n个小孩每次可以放两个积木,m个小孩,每次可以放3个积木,最后每个小孩 ...

随机推荐

  1. iOS 5个Xcode开发调试技巧

    转自Joywii的博客,原文:Four Tips for Debugging in XCode Like a Bro    1.Enable NSZombie Objects(开启僵尸对象) Enab ...

  2. go实例之轻量级线程goroutine、通道channel与select

    1.goroutine线程 goroutine是一个轻量级的执行线程.假设有一个函数调用f(s),要在goroutine中调用此函数,请使用go f(s). 这个新的goroutine将与调用同时执行 ...

  3. c语言库函数测试

    1.函数名: abort功  能: 异常终止一个进程用  法: void abort(void);程序例: #include <stdio.h> #include <stdlib.h ...

  4. 2018第一发:记一次【Advanced Installer】打包之旅

    一.前言 2017年最后几天,你们都高高兴兴的跨年,博主还在加班制作.net安装包.因为年前要出来第一版的安装包,所以博主是加班加点啊.本来想用VS自带的制作工具,不过用过的人都知道,真是非常好(to ...

  5. Tensorflow之MNIST机器学习入门

    MNIST机器学习的原理: 通过一次次的 输入某张图片的像素值(用784维向量表示)以及这张图片对应的数字(用10维向量表示比如数字1用[0,1,0,0,0,0,0,0,0,0]表示),来优化10*7 ...

  6. Django2.0中文文档

    title: Django2.0中文文档 tags: Python,Django,入沐三分 grammar_cjkRuby: true --- Django2.0版本已经发布了,我们先来看一个图片 从 ...

  7. windows c++ 错误汇总

    1.fatal error C1900 错误:fatal error C1900: “P1”(第“20081201”版)和“P2”(第“20080116”版)之间 Il 不匹配 检查之后发现jepgl ...

  8. SQLServer Agent执行[分发清除: distribution] 无法删除快照文件

    由于之前创建的发布订阅造成严重的性能压力,症状表现为发布订阅表查询产生CMEMTHREAD  suspend等待,由于开发配置每隔十分钟会产生大量的SQLCOMMAND(create table,cr ...

  9. Python的可变类型与不可变类型

    Python基础知识,自己写一写比较不容易忘 Python的每个对象都分为可变和不可变,主要的核心类型中,数字.字符串.元组是不可变的,列表.字典是可变的. 对不可变类型的变量重新赋值,实际上是重新创 ...

  10. SignalR的另类实现技巧

    很久之前发表过一篇名为<通过三个DEMO学会SignalR的三种实现方式>的文章,在那篇文章里面详细介绍了在WEB应用下的常用SignalR实现方法,而今天我们来利用SignalR来实现其 ...