【Henu ACM Round#14 C】Duff and Weight Lifting

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

2^y可以由两个2^(y-1)相加得到。 则有一个贪心的策略。 就是2^x尽量都变成2^(x+1) (即能够凑就尽量凑) 如果x还有剩余的话。答案递增1 而凑上去的数字,显然是可以合并成1步操作的。因为他们的和就是2^(x+1)

【代码】

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e6;

int n,a[N+10];

int main()
{
    #ifdef LOCAL_DEFINE
        freopen("rush.txt","r",stdin);
    #endif // LOCAL_DEFINE
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> n;
    for(int i = 1;i <= n;i++){
        int x;
        cin >> x;
        a[x]++;
    }
    int ans = 0;
    for (int i = 1;i<=N+1;i++){
        a[i] += a[i-1]/2;
        a[i-1]%=2;
        ans+=a[i-1];
    }
    int x = a[N+1],y;
    while (1){
        y = x/2;
        if (x==0 && y==0) break;
        x%=2;
        ans+=x;
        x = y;

    }
    cout << ans << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/8341076.html