Codeforces 270C Magical Boxes【排序+进制处理】
来源:互联网 发布:机械臂编程语言 编辑:程序博客网 时间:2024/04/19 06:18
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.
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 a single integer p, such that the smallest magical box that can contain all of Emuskald’s boxes has side length 2p.
20 31 5
3
10 4
1
21 102 2
3
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 __int64struct 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); }}
- Codeforces 270C Magical Boxes【排序+进制处理】
- Codeforces 270c Magical Boxes
- CODEFORCES 270C Magical Boxes <<小箱子装大箱子>>
- Codeforces #165 div2 C. Magical Boxes
- A. Magical Boxes
- codeforces 903C. Boxes Packing
- Codeforce -269 - A. Magical Boxes
- Codeforces 551C GukiZ hates Boxes【二分+思维处理】好题!
- CodeForces 390C Inna and Candy Boxes
- codeforces 390c Inna and Candy Boxes
- codeforces 551 C GukiZ hates Boxes
- codeforces 390C Inna and Candy Boxes
- 【24.67%】【codeforces 551C】 GukiZ hates Boxes
- Codeforces 821 C. Okabe and Boxes
- Codeforces 821C-Okabe and Boxes
- Codeforces 821C Okabe and Boxes 题解
- codeforces 420 div2 C Okabe and Boxes
- Codeforces#420 C. Okabe and Boxes
- Codeforces270D Greenhouse Effect【思维+LIS】
- trie+dfs+贪心 big
- 11.内置对象request
- 12.response和session
- scala List列表
- Codeforces 270C Magical Boxes【排序+进制处理】
- Maven下SSM框架搭建-下
- POJ 3376 Finding Palindromes(Trie+EKMP)
- 进程间通信-pipe函数
- PHP判断中英文混排长度
- 最长公共子序列
- 用户和组
- JDBC学习笔记—对时间类型的处理
- Linux学习(1)-常用快捷键、文件管理和查询 阅读目录