Codeforces 270C Magical Boxes【排序+进制处理】

C. Magical Boxes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≤ 1091 ≤ 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.


题目大意:


问用多大边长的正方形盒子,能够容纳所有给出的盒子。

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


思路:


我们先将盒子按照从小到大排序,那么对于临近的两个大小的盒子,我们考虑将小盒子放在大盒子中, 如果容纳不下,那么就相当于再多开几个大盒子。

因为这里要涉及到2进制计算,那么如果相邻两个盒子的大小差距相对较大的时候,我们就可以视为一个大盒子就能够装下所有的小盒子。

依次维护下去即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define ll __int64
struct node
{
    ll x,y;
}a[150000];
int cmp(node a,node b)
{
    return a.x<b.x;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d%I64d",&a[i].x,&a[i].y);
        }
        sort(a+1,a+1+n,cmp);
        for(int i=1;i<=n-1;i++)
        {
            if(a[i].x==a[i+1].x)
            {
                a[i+1].y+=a[i].y;
                continue;
            }
            if(a[i+1].x-a[i].x>=16)continue;
            else
            {
                ll Could=a[i+1].y*(ll)(pow(2,2*(a[i+1].x-a[i].x)));
                if(Could>=a[i].y)continue;
                else
                {
                    a[i+1].y+=(a[i].y-Could)/(ll)(pow(2,2*(a[i+1].x-a[i].x)));
                    if((a[i].y-Could)%(ll)(pow(2,2*(a[i+1].x-a[i].x)))!=0)a[i+1].y++;
                }
            }
        }
        ll cnt=0;
        ll temp=1;
        while(temp<a[n].y)
        {
            temp*=4;
            cnt++;
        }
        if(a[n].y==1)a[n].x++;
        printf("%I64d\n",a[n].x+cnt);
    }
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值