C. Magical Boxes

Emuskald is a well-known illusionist. One of his trademark tricks involves a set of magical boxes. The essence of the trick is in packing the boxes inside other boxes.

From the top view each magical box looks like a square with side length equal to 2k (k is an integer, k ≥ 0) units. A magical box v can be put inside a magical box u, if side length of v is strictly less than the side length of u. In particular, Emuskald can put 4 boxes of side length 2k - 1 into one box of side length 2k, or as in the following figure:

Emuskald is about to go on tour performing around the world, and needs to pack his magical boxes for the trip. He has decided that the best way to pack them would be inside another magical box, but magical boxes are quite expensive to make. Help him find the smallest magical box that can fit all his boxes.

Input

The first line of input contains an integer n (1 ≤ n ≤ 105), the number of different sizes of boxes Emuskald has. Each of following n lines contains two integers ki and ai (0 ≤ ki ≤ 109, 1 ≤ ai ≤ 109), which means that Emuskald has ai boxes with side length 2ki. It is guaranteed that all of ki are distinct.

Output

Output a single integer p, such that the smallest magical box that can contain all of Emuskald’s boxes has side length 2p.

Examples

input

2
0 3
1 5

output

3

input

1
0 4

output

1

input

2
1 10
2 2

output

3

Note

Picture explanation. If we have 3 boxes with side length 2 and 5 boxes with side length 1, then we can put all these boxes inside a box with side length 4, for example, as shown in the picture.

In the second test case, we can put all four small boxes into a box with side length 2.

题目大意:

问用多大边长的正方形盒子,才能够能够装下所有给出的盒子

其中,如果一个盒子的边长大于另一个盒子,那么这个盒子就能够容纳那另一个盒子 。

思路:

我们先将盒子按照从小到大排序,那么对于临近的两个大小的盒子,我们考虑将小盒子放在大盒子中, 如果容纳不下,那么就相当于再多开几个大盒子。用这个贪心的思想,最后放完之后,还剩多少盒子,即外层盒子。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
#define LL long long
#define MAXN 100100
struct node
{
LL x;
LL y;
} a[MAXN*2];
bool cmp(node u,node v)
{
return u.x<v.x;
}
int main()
{
LL n,i,j;
cin>>n;
for(i=1; i<=n; i++)
cin>>a[i].x>>a[i].y;
sort(a+1,a+n+1,cmp);
LL k=a[1].x,l=a[1].y;
for(i=2; i<=n; i++)
{
if(a[i].x==k)
l=l+a[i].y;
else
{
LL b=a[i].x-k;
for(j=1; j<=b; j++)
{
if(l%4==0)
l=l/4;
else
l=(l/4)+1;
if(l==1)
break;
}
k=a[i].x,l=max(l,a[i].y);
}
}
while(1)
{
if(l==1)
break;
if(l%4==0)
l=l/4;
else
l=(l/4)+1;
k++;
}
if(k==a[n].x)
cout<<k+1;
else
cout<<k;
return 0;
}

codeforce 270C Magical Boxes的更多相关文章

  1. Codeforces Round #165 (Div. 2)

    C. Magical Boxes 问题相当于求\[2^p \gt \max{a_i \cdot 2^{k_i}},p \gt k_i\] D. Greenhouse Effect \(dp(i,j)\ ...

  2. codeforce C. Okabe and Boxes

    题目传送门 这道题 每次删除一个点 那么这个点必然在栈里面 那么如果堆顶不是他 我们就需要一次操作使得堆合理 这时我们可以把他删除然后把他下面的点打个标记表示这下面的点以后想怎么排就怎么排以后都不需要 ...

  3. Magical平台类库代码分享

    这些天闲来无事,就整理了一些类库.jQuery插件和自定义控件.今天和大家分享下Magical平台类库代码. 下图为整个解决方案图.MagicalPlatForm里面定义的是众多的Layer层:Mag ...

  4. Fedora 24 Gnome Boxes 无法ping通网络

    安装Fedora 24在试用虚拟机时发现无法ping通外网. 我傻傻地以为是软件问题. 问题描述: 尝试ping程序来测试网络连通性: (我之前也是ping百度,后来在为了少打字百度了一些比较短的域名 ...

  5. STL : map函数的运用 --- hdu 4941 : Magical Forest

    Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  6. hdu4941 Magical Forest (stl map)

    2014多校7最水的题   Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit ...

  7. Problem B Boxes in a Line

     省赛B题....手写链表..其实很简单的.... 比赛时太急了,各种手残....没搞出来....要不然就有金了...注:对相邻的元素需要特判..... Problem B Boxes in a Li ...

  8. Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s

    C. Inna and Candy Boxes   Inna loves sweets very much. She has n closed present boxes lines up in a ...

  9. Open judge C16H:Magical Balls 快速幂+逆元

    C16H:Magical Balls 总时间限制:  1000ms 内存限制:  262144kB 描述 Wenwen has a magical ball. When put on an infin ...

随机推荐

  1. Java第十七天,Set接口

    Set接口 1.特点 (1)不包含重复元素. (2)没有索引. (3)继承自Collection接口,所以Collection接口中的所有方法都适用于Set接口. 2.解析 (1)为什么不能包含重复元 ...

  2. Oacle学习-01Oracle的安装

    @ 目录 下载Oracle 安装Oracle 安装plsqldeveloper客户端 下载Oracle 官方下载地址:Oracle下载 网盘地址:链接:https://pan.baidu.com/s/ ...

  3. 震撼!全网第一张源码分析全景图揭秘Nginx

    不管是C/C++技术栈,还是PHP,Java技术栈,从事后端开发的朋友对nginx一定不会陌生. 想要深入学习nginx,阅读源码一定是非常重要的一环,但nginx源码量毕竟还是不算少,一不小心就容易 ...

  4. Python Requests-学习笔记(9)-错误与异常

    遇到网络问题(如:DNS查询失败.拒绝连接等)时,Requests会抛出一个ConnectionError 异常. 遇到罕见的无效HTTP响应时,Requests则会抛出一个 HTTPError 异常 ...

  5. Linux远程登陆

    Linux 远程登录 Linux一般作为服务器使用,而服务器一般放在机房,你不可能在机房操作你的Linux服务器. 这时我们就需要远程登录到Linux服务器来管理维护系统. Linux系统中是通过ss ...

  6. Win10安装Keras+Tensorflow+Opencv

    Win10安装keras 安装 Anaconda 清华加速下载链接: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 我选择的版本是: A ...

  7. AJ学IOS(27)UI之iOSUIKit字符属性NSAttributedString概述

    AJ分享,必须精品 UIKit字符属性NSAttributedString概述 字符属性 字符属性可以应用于 attributed string 的文本中. NSString *const NSFon ...

  8. AJ学IOS(01) UI之Hello World与加法计算器

    不多说,AJ分享,必须精品 这两个一个是HelloWorld(左边) 另一个是 加法计算器(右边)的截图. 先运行第一个 程序看看效果 1.打开Xcode(没有哦mac系统的没有xcode的帮你们默哀 ...

  9. vue中SPA的优缺点和理解

    说说你对SPA的理解,他的优缺点分别是什么? SPA(single-page application) 尽在Web页面初始化时加载相应的HTML,JavaScript和CSS.一旦页面加载完成,SPA ...

  10. F - Pearls HDU - 1300

    简单dp. 题目大意:有n种珍珠,这n种珍珠有不同的需求量,不同的价格,价格越高,质量越高,在购买每一种珍珠时,都需要在原来的基础上多买10个.也就是说如果需要买x种珍珠,那就要付x+10个的钱.每一 ...