[topcoder]BoxesDiv2

https://community.topcoder.com/stat?c=problem_statement&pm=13192

#include <vector>
#include <queue>
#include <functional>
using namespace std;

class BoxesDiv2
{
public:
	vector<int> power2;
	int findSize(vector <int> candyCounts)
	{
		priority_queue<int, vector<int>, greater<int> > q;
		power2.clear();
		int tmp = 1;
		for (int i = 0; i < 32; i++)
		{
			power2.push_back(tmp);
			tmp *= 2;
		}
		int size = candyCounts.size();
		for (int i = 0; i < size; i++)
		{
			int x = leastPower(candyCounts[i]);
			q.push(x);
		}
		
		while (q.size() > 1)
		{
			int a = q.top();
			q.pop();
			int b = q.top();
			q.pop();
			int bigger = max(a,b);
			q.push(bigger*2);
		}
		if (!q.empty())
		{
			return q.top();
		}
		else
		{
			return -1;
		}
	}
	
	int leastPower(int x)
	{
		int i = 0;
		while (x > power2[i])
			i++;
		return power2[i];
	}
};

 

posted @ 2014-08-02 15:53  阿牧遥  阅读(176)  评论(0编辑  收藏  举报