1. 程式人生 > >CodeForces - 270C Magical Boxes(思維)

CodeForces - 270C 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 2k

i. 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

Copy

2
0 3
1 5

output

Copy

3

input

Copy

1
0 4

output

Copy

1

input

Copy

2
1 10
2 2

output

Copy

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^i,輸出i

思路:先將箱子的尺寸升序排序,在遍歷一遍判斷每個箱子能否放入尺寸最大的箱子裡邊,如果不能那麼讓最大箱子的尺寸++

若可以,那判斷下一個。(在這我看很多博主寫的是遍歷每個箱子能不能放如比第一個比他大的箱子,我沒有這樣做,因為你想,如果這些小箱子的數量能放入最大的那個,其實肯定是有辦法放進去的)

#include <iostream>
#include <cstdio>
#define LL long long
#include <cstdlib>
#include <map>
#define MAXN 200005
#include <algorithm>
#include <cmath>
using namespace std;
 
struct Edge
{
    long long ki;
    long long ai;
    bool operator<(const Edge& ee)
    {
        return ki > ee.ki;
    }
}e[100005];
 
int main()
{
    long n,i;
    long long index,ans;
    while(cin>> n)
    {
        index = -1;
        for(i=0; i<n; i++)
        {
            cin>>e[i].ki>>e[i].ai;
            if(index < e[i].ki)
                index = e[i].ki;
        }
        sort(e,e+n);
        ans = index + 1;   //ans為當前最大的盒子的邊長 
        for(int i=0; i<n; i++)
        {
            index = ans - e[i].ki;
            if(index>30)  //如果最大的和一個小盒子的邊長差大於30,那算下來要pow(4,index)超範圍 
                continue;
            index = pow(4.0,index);//直接看當前最大的盒子能不能裝下當前全部的小盒子,如果能,那麼肯定 
            while(e[i].ai>index)   //是有辦法可以放進去的 (大小的巢狀),但是從俯視的角度看,都是 
            {					//一樣的 
                index*=4;
                ans++;
            }
        }
        cout << ans <<endl;
    }
 
    return 0;
}