Codeforces 588C Duff and Weight Lifting 【数学】

本文解析了Codeforces588C题目的解题思路及算法实现,该题目要求最小化移除一系列权重(表示为2的幂次)所需的步骤数。通过将权重值相加并确保总和为2的幂次来移除权重。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:Codeforces 588C Duff and Weight Lifting

C. Duff and Weight Lifting
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there’s no more weight left. Malek asked her to minimize the number of steps.

Duff is a competitive programming fan. That’s why in each step, she can only lift and throw away a sequence of weights 2a1, …, 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + … + 2ak = 2x, i. e. the sum of those numbers is a power of two.

Duff is a competitive programming fan, but not a programmer. That’s why she asked for your help. Help her minimize the number of steps.

Input
The first line of input contains integer n (1 ≤ n ≤ 106), the number of weights.

The second line contains n integers w1, …, wn separated by spaces (0 ≤ wi ≤ 106 for each 1 ≤ i ≤ n), the powers of two forming the weights values.

Output
Print the minimum number of steps in a single line.

Examples
input
5
1 1 2 3 3
output
2
input
4
0 1 2 3
output
4
Note
In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it’s not possible to do it in one step because their sum is not a power of two.

In the second sample case: The only optimal way is to throw away one weight in each step. It’s not possible to do it in less than 4 steps because there’s no subset of weights with more than one weight and sum equal to a power of two.

题意:有n个数a[i]代表 2a[i] ,每次可以去掉若干个数当它们和为2的幂次时。问最少需要多少次。

思路:一直合并就可以了,因为当a!=b时, 2a+2b 肯定不是2的幂次。

AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e7 + 10;
int a[MAXN];
int vis[MAXN];
int main()
{
    int n;
    while(scanf("%d", &n) != EOF) {
        CLR(vis, 0); int Max = 0;
        for(int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
            vis[a[i]]++;
            Max = max(Max, a[i]);
        }
        for(int i = 0; i <= Max; i++) {
            if(vis[i] % 2 == 0) {
                vis[i+1] += vis[i] / 2;
                vis[i] = 0;
            }
            else {
                vis[i+1] += vis[i] / 2;
                vis[i] = 1;
            }
        }
        Max++;
        while(1) {
            if(vis[Max] == 1 || vis[Max] == 0) {
                break;
            }
            if(vis[Max] & 1) {
                vis[Max+1] += vis[Max] / 2;
                vis[Max] = 1;
            }
            else {
                vis[Max+1] += vis[Max] / 2;
                vis[Max] = 0;
            }
            Max++;
        }
        int ans = 0;
        for(int i = 0; i <= Max; i++) {
            ans += vis[i];
        }
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值