Duff and Weight Lifting - 587A

时间:2025-04-10 00:06:55

题目大意:某个人训练举重,他每次可以举起来2^wi的重量,不过这个人比较懒所以他想尽量减少训练的次数,如果所有的训练重量2^a1 +2^a2+....2^ak = 2^x,那么这些重量可以一次性训练(不需要连续),问他最少要训练几次才行。

分析:

已知 2^x+2^x = 2^(x+1),所以相同的是可以合并成下一个的,最后只需要判断,有多少个不能合成的即可。

代码如下:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std; const int MAXN = 2e6+; int a[MAXN+]; int main()
{
int N, x, ans=; scanf("%d", &N); for(int i=; i<N; i++)
{
scanf("%d", &x);
a[x] += ;
} for(int i=; i<MAXN; i++)
{
a[i+] += a[i] / ;
a[i] %= ;
ans += a[i];
} printf("%d\n", ans); return ;
}