Codeforces 626C Block Towers(二分)
C. Block Towers
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.
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.
Print a single integer, denoting the minimum possible height of the tallest tower.
1 3
9
3 2
8
5 0
10
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(二分)的更多相关文章
- Codeforces 626C Block Towers「贪心」「二分」「数学规律」
题意: 一堆人用方块盖塔,有n个人每次只能加两块方块,有m个人每次只能加三块方块.要求每个人盖的塔的高度都不一样,保证所用方块数最少,求最高的塔的高度. 0<=n+m 0<=n,m< ...
- CodeForces 626C Block Towers
构造AC的.左右两边都先不用6的倍数,然后哪边数字大那一边往回退一下,然后再比较哪边数字大.......直到结束 #include<cstdio> #include<cstring& ...
- 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 ...
- codeforce626C.Block Towers(二分)
C. Block Towers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces 626C
...
- CodeForces - 327D Block Tower
D. Block Tower time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- [Codeforces 1199C]MP3(离散化+二分答案)
[Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...
- 【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 ...
- (二分)Block Towers(cf626)
http://www.codeforces.com/contest/626/problem/C 题意是有一群小朋友在堆房子,现在有n个小孩每次可以放两个积木,m个小孩,每次可以放3个积木,最后每个小孩 ...
随机推荐
- OC学习9——反射机制
1.OC提供了3种编程方式与运行环境进行交互: 直接通过OC的源代码:这是最常见的方式,开发人员只是编写OC源代码,而运行环境负责在后台工作. 通过NSObject类中定义的方法进行动态编程:因为绝大 ...
- 每周.NET前沿技术文章摘要(2017-05-17)
汇总国外.NET社区相关文章,覆盖.NET ,ASP.NET等内容: .NET .NET Framework 4.7正式发布 链接: http://www.infoq.com/cn/news/2017 ...
- UVALive 4850 Installations
题目大意:有若干个任务,每个任务耗时si,期限为di,同一时间只能做一个任务.对于一个任务,惩罚值为max(0,完成时间-期限).问怎么安排,使(最大惩罚值+次大惩罚值)最小,O(n^2). 如果没有 ...
- 树链剖分X2
1.ZJOI树的统计 板子题 因为初始化没打改了几个小时 改到双腿软着出的机房(身体素质感人 #include<iostream> #include<cstdio> #incl ...
- 7、树莓派编程;gpio编程;led闪烁
本博文仅作本人操作过程的记录,留作备忘.自强不息 QQ12226981 1.树莓派接口对照,一定要找到对应的引脚,不要接错了.我画上箭头. 2.安装 下载地址,https://git.drogon.n ...
- 系统内置委托:Func/Action
lSystem.Func 代表有返回类型的委托 lpublic delegate TResult Func<out TResult>(); lpublic delegate TResul ...
- thinkinginjava学习笔记08_接口
抽象类和抽象方法 抽象方法是指没有具体实现的方法,仅仅有方法的声明和没有方法体:使用abstract关键字定义一个抽象方法:包含抽象方法的类成为抽象类,如果一个类中包含抽象方法则必须使用abstrac ...
- Python初体验
今天开始所有的工作脚本全都从perl转变到python,开发速度明显降低了不少,相信以后随着熟练度提升会好起来.贴一下今天一个工作代码,由于之前去一家小公司测序时,序列长度竟然都没有达到要求,为了之后 ...
- rpc之thrift
rpc之thrift 一.介绍 thrift是一个rpc(remove procedure call)框架,可以实现不同的语言(java.c++.js.python.ruby.c#等)之间的相互调用. ...
- js 对象的值传递
一.变量赋值的不同 1.原始值 在将一个保存着原始值的变量复制给另一个变量时,会将原始值的副本赋值给新变量,此后这两个变量是完全独立的. 2.引用值: 在将一个保存着对象内存地址的变量复制给另一个变量 ...