Code Forces 587 A. Duff and Weight Lifting(贪心)
来源:互联网 发布:淘宝网藏族服装 编辑:程序博客网 时间:2024/06/13 22:59
Description
达夫每天练习举重,教练给了她n个重量值为2^wi哑铃让她举,每次达夫可以举起满足2^a1+2^a2+…+2^ak=2^x的重量并将这些哑铃扔掉,问达夫最少几次完成教练的任务
Input
第一行为一整数n表示哑铃数,第二行n个整数wi表示第i个哑铃重2^wi
Output
输出达夫最少几次能完成任务
Sample Input
5
1 1 2 3 3
Sample Output
2
Solution
贪心,贪心策略是每次尽可能找到重量之和能够组成2^x的重量,显然如果a1,a2,…,ak互不相同是不可能满足条件的,同时由一个显然的结论2^n+2^n=2^(n+1)我们可以先将哑铃“合并”,那么最后还剩的重量就是每一次要举的重量,注意此处数组要开大一点,因为当有10^6个重量为2^(10^6)的哑铃时会合并出10^6+log(10^6)的x值,具体过程见代码
Code
#include<stdio.h>#include<string.h>#define maxn 1111111int n,w,num[maxn],ans;int main(){ while(~scanf("%d",&n)) { memset(num,0,sizeof(num)); ans=0; for(int i=0;i<n;i++) { scanf("%d",&w); num[w]++; } for(int i=1;i<maxn;i++)//合并 num[i]+=(num[i-1]/2),num[i-1]%=2; for(int i=0;i<maxn;i++)//剩下的就是每次需要举的重量 ans+=num[i]; printf("%d\n",ans); } return 0;}
0 0
- Code Forces 587 A. Duff and Weight Lifting(贪心)
- Code Forces 587 A. Duff and Weight Lifting
- codeforces-587A-Duff and Weight Lifting
- coderforce 587A Duff and Weight Lifting
- A. Duff and Weight Lifting
- Code Forces 588 A. Duff and Meat(贪心)
- Codeforces Round #326 (Div. 1)A. Duff and Weight Lifting
- Codeforces Round #326 (Div. 1)-A. Duff and Weight Lifting
- Codeforces Round #326 (Div. 2)587A. Duff and Weight Lifting
- Codeforces 588 C. Duff and Weight Lifting
- cf水题 --Duff and Weight Lifting
- cf 588C Duff and Weight Lifting
- CodeForces 588 C. Duff and Weight Lifting
- cf C. Duff and Weight Lifting (二进制编码_好题)
- codeforces#326-C-Duff and Weight Lifting-map应用
- codeblocks round 326 div2 C Duff and Weight Lifting
- Codeforces 588C Duff and Weight Lifting 【数学】
- 【1】【数学】CodeForces 588C Duff and Weight Lifting
- realloc malloc calloc的 区别
- Vim命令合集
- js取整数
- Python练习题
- 垃圾收集器的类型
- Code Forces 587 A. Duff and Weight Lifting(贪心)
- crti.S: No such file or directory
- 自定义SimpleMappingExceptionResolver实现个性化的异常处理
- swift学习- 泛型(二十三)
- linux系统/var/log目录下的信息详解
- 命令行使用MySql的入门基础语句
- Log4j配置实例(log4j.xml)
- Log4j应用实例
- 14.Android 控制Handler内存泄露 技巧