Codeforce -269 - A. Magical Boxes

A. Magical Boxes
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 ≤ 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.

题意:小明需要一个大箱子把所有的小箱子装进去,而小箱子又可以把比他小的箱子装入,求小明所需的大箱子的最小 size 是多少。

AC代码思路:因为一个箱子里只能装四个比他小一号的箱子,所以这题就变成了一个进制问题。即满四进一

AC代码:

#include <iostream>
using namespace std;
int main()
{
    int n;cin>>n;
    int p=0,maxk=0;
    for (int i=1;i<=n;i++)
    {
        int k,a;cin>>k>>a;
        maxk=max(k,maxk);//把最大的 K 记下
        int m=0,s=1;
        while (s<a)//此处就是找到 size==k 的盒子们所需的最大的盒子是自己的几倍
        {
            s*=4;
            m++;
        }
        p=max(p,k+m);
    }
    if (p==maxk) p++;//若 p==maxk ,即说明小盒子们所需的最大的盒子的 size 就是 k ,然而 size==k 的盒子也需要一个 k+1 的盒子来装,所以 p++ 
    cout<<p;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值