Codeforces 588 C Duff and Weight Lifting【思维】
来源:互联网 发布:手机游戏下载java 编辑:程序博客网 时间:2024/05/01 13:35
C. Duff and Weight Lifting
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 ofi-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 integerx 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个数,每个数都表示2^a【i】的数,每一次选取可以从n个数中挑取任意个数的数字,必须让其和是2的幂数才行,问最少选取几次才能将所有数都取走。
思路:
1、首先明确这一点,在二进制中,想要进位一定是同位相加才能进位。那么给出的数又是2的幂数,那么明显只有a【i】==a【j】的时候,才有进位合并的可能。
2、那么设定一个数组vis【i】表示2^i有多少个。那么我们从0开始扫,令vis【i】=vis【i】%2;vis【i+1】=vis【i】/2,即可,最后ans=累加vis【i】;
Ac代码:
#include<stdio.h>#include<string.h>using namespace std;int vis[1000500];int main(){ int n; while(~scanf("%d",&n)) { memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) { int x; scanf("%d",&x); vis[x]++; } for(int i=0;i<1000400;i++) { vis[i+1]+=vis[i]/2; vis[i]%=2; } int output=0; for(int i=0;i<1000400;i++) { output+=vis[i]; } printf("%d\n",output); }}
- Codeforces 588 C Duff and Weight Lifting【思维】
- CodeForces 588C - Duff and Weight Lifting(思维)
- Codeforces 588 C. Duff and Weight Lifting
- CodeForces 588 C. Duff and Weight Lifting
- Codeforces 588C Duff and Weight Lifting 【数学】
- 【1】【数学】CodeForces 588C Duff and Weight Lifting
- cf 588C Duff and Weight Lifting
- codeforces#326-C-Duff and Weight Lifting-map应用
- codeforces-587A-Duff and Weight Lifting
- A. Duff and Weight Lifting
- Codeforces Round #326 (Div. 2)Problem C - Duff and Weight Lifting
- Codeforces Round #326 (Div. 1)A. Duff and Weight Lifting
- Codeforces Round #326 (Div. 1)-A. Duff and Weight Lifting
- codeblocks round 326 div2 C Duff and Weight Lifting
- 【Codeforces Round 326 (Div 2)C】【贪心】Duff and Weight Lifting 每次取数二的幂数最小取数次数
- cf水题 --Duff and Weight Lifting
- coderforce 587A Duff and Weight Lifting
- Codeforces Round #326 (Div. 2)587A. Duff and Weight Lifting
- 9.1AT课堂M笔记
- 【STL】deque的常用方法
- Android 关于 px、dp、sp 的那些事
- [GDOI2017模拟9.4]同桌的你
- Arithmetic Progression
- Codeforces 588 C Duff and Weight Lifting【思维】
- HTTP协议知识点
- bzoj1179(缩点+乱搞)
- Android资源文件(备忘录) 供个人记忆
- 关于一些常识的总结
- leetcode题解
- 复选框 CheckBox 的实例
- POJ2121——Inglish-Number Translator
- IOS最完整小说阅读器Demo