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. k8s集群搭建笔记(细节有解释哦)

    本文中所有带引号的命令,请手动输入引号,不知道为什么博客里输入引号,总是自动转换成了中文 基本组成 pod:k8s 最小单位,类似docker的容器(也许) 资源清单:资源.资源清单语法.pod生命周 ...

  2. javascript入门 之 zTree(十四 增删查改)(二)

    <!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - addNodes / editName / rem ...

  3. 【python实现卷积神经网络】开始训练

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  4. work of 12/30/2015

    part 组员                今日工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云   merge UI 与reader     6  丰富re ...

  5. CodeForces - 913C (贪心)

    点完菜,他们发现好像觉得少了点什么? 想想马上就要回老家了某不愿透露姓名的林姓学长再次却陷入了沉思......... 他默默的去前台打算点几瓶二锅头. 他发现菜单上有n 种不同毫升的酒. 第 i 种有 ...

  6. selenium获取多窗口句柄并一切换至原窗口句柄(三个窗口)

    网上有很多是selenium基于python来获取两个窗口句柄与切换,本文实现用python+selenium获取多窗口句柄并一一切换至原窗口句柄(三个窗口),且在每个窗口下进行一个搜索或翻译,然后截 ...

  7. 详解 volatile关键字 与 CAS算法

    (请观看本人博文 -- <详解 多线程>) 目录 内存可见性问题 volatile关键字 CAS算法: 扩展 -- 乐观锁 与 悲观锁: 悲观锁: 乐观锁: 在讲解本篇博文的知识点之前,本 ...

  8. 13.create-react-app 构建的项目使用代理 proxy

    1. 正常运行 npm run eject 2. create-react-app 的版本在低于 2.0 的时候可以在 package.json 增加 proxy 配置, 配置成如下: "p ...

  9. 2.hover的使用

    1. 自身的hover div :hover{  :hover前要有空格  } 2.hover指向子元素 father:hover .childer {   :hover前不能有空格   } 3.ho ...

  10. 开源软件SoftEther使用

    最近在寻找比较好用的开源VPN,感觉SoftEther很符合我的需求.一方面是SoftEther属于开源软件并且一直在更新,另一方面是功能强大,好用. VPN支持路由功能和NAT功能,还支持多种类型的 ...