求助站外题(玄关,先到先得)
  • 板块灌水区
  • 楼主Hope5365
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/6/8 17:46
  • 上次更新2024/6/8 20:25:02
查看原帖
求助站外题(玄关,先到先得)
1004336
Hope5365楼主2024/6/8 17:46

#include <iostream>
#include <cmath>
using namespace std;
#define int unsigned long long

const int N = 1e5 + 10;
int n,m,t[N],ans,sum; //t[i]表示第1 << i有几个(不包括拆分)

signed main()
{
	ios::sync_with_stdio(0);
	cin >> n >> m;
	for(int i = 1;i <= m;i ++)
	{
		int x;
		cin >> x;
		sum += x;
		t[(int)log2(x)] ++;
	}
	if (sum < n)
	{
		cout << -1 << endl;
		return 0;
	}
//	for(int i = 0;i < 63;i ++) cout << i << ' ' << t[i] << endl;
//	cout << (23 & (1 << 4)) << endl;
	for(int i = 0;(1 << i) <= n;i ++)
	{
		if (!t[i] && (n & (1 << i)))
			for(int j = i + 1;(1 << j) <= n;j ++)
				if (t[j]) 
				{
//					cout << "i:" << i << " j:" << j << endl;
//					ans += j - i;
					for(int k = j - 1;k >= i;k --)
						ans ++,t[k + 1] --,t[k] += 2;
					break;
				}
		if (n & (1 << i)) t[i] --;
		t[i + 1] += t[i] / 2;
	}
	cout << ans << endl;
	return 0;
}
2024/6/8 17:46
加载中...