Codeforces 558C Amr and Chemistry(dp)
来源:互联网 发布:java求职简历自我评价 编辑:程序博客网 时间:2024/06/04 03:53
题意:
给你n个数,让你通过下面两种操作,把它们转换为同一个数。求最少的操作数。
1.ai=ai∗2
2.ai=ai/2 ,向下取整
思路:
因为
ai<=100000 ,因此就可以想到通过处理出所有数转到num 所需要的最少操作数dp[num],维护dp[num] 的最小值即可。对于ai ,我们处理出它转换到所有其它数的最少操作数。
先枚举左移,在枚举右移。讲dp[num]的和记录在res[num]数组中表示到达num最少的总操作次数。注意:要满足全部的数字都能满足num,所以要在用一个cnt[num]来记录每个num的出现次数。
所以题意条件的是cnt[num]=n ,且res[num] 最小的。
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <cstdlib>using namespace std;typedef long long ll;const int MAXN = (1<<20)+5;const int INF = 0x3f3f3f3f;int cnt[MAXN];bool vis[MAXN];int res[MAXN], dp[MAXN];vector<int> vec;int Scan() { int res = 0, ch, flag = 0; if((ch = getchar()) == '-') //判断正负 flag = 1; else if(ch >= '0' && ch <= '9') //得到完整的数 res = ch - '0'; while((ch = getchar()) >= '0' && ch <= '9' ) res = res * 10 + ch - '0'; return flag ? -res : res; }int main() { int n, a; while(scanf("%d", &n) != EOF) { for(int k = 0; k < n; k++) { a = Scan(); vec.clear(); int num = MAXN; for(int i = 0; i <= 20 && num; i++) { num = (a >> i); if(num == 0) continue; for(int j = 0; num < MAXN; num <<= 1, j++) { if(!vis[num]) { dp[num] = i+j; cnt[num]++; vis[num] = true; vec.push_back(num); }else dp[num] = min(dp[num], i+j); } } for(int i = 0; i < vec.size(); i++) { int tmp = vec[i]; vis[tmp] = false; res[tmp] += dp[tmp]; } } int minv = INF; for(int i = 0; i < MAXN; i++) { if(cnt[i] == n) minv = min(minv, res[i]); cnt[i] = 0; } printf("%d\n", minv); memset(res, 0, sizeof(res)); } return 0;}
0 0
- Codeforces 558C Amr and Chemistry(dp)
- codeforces 558 C Amr and Chemistry
- Codeforces 558C Amr and Chemistry 规律
- Codeforces 558C Amr and Chemistry
- Codeforces 558C Amr and Chemistry 暴力 - -
- codeforces 558C Amr and Chemistry
- Codeforces 558C - Amr and Chemistry (枚举)
- Codeforces 558C Amr and Chemistry
- 【23.39%】【codeforces 558C】Amr and Chemistry
- codeforces 558C Amr and Chemistry(位操作)
- 558C Amr and Chemistry
- Codeforces 558C Amr and Chemistry 位操作
- Codeforces 558C Amr and Chemistry 全都变相等
- Codeforces 558C Amr and Chemistry(数论+位运算)
- Codeforces 558C. Amr and Chemistry(暴搞)
- 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
- CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
- !codeforces 558C Amr and Chemistry-yy题-(位运算相关)
- 初学struts2 出现There is no Action mapped for namespace...错误
- 【Spark】DAGScheduler源码浅析
- 解决Robotium测试中Installation error INSTALL_FAILED_VERSION_DOWNGRADE错误
- 微信企业号开发:corpsecret到底在哪块呢?
- UML类图几种关系的总结
- Codeforces 558C Amr and Chemistry(dp)
- 链表的创建、查询、删除,插入;
- YT05-动态归划求解课后题目-1003—免费馅饼 -(6.21日-烟台大学ACM预备队解题报告)
- eclipse 新建一个Java Project所要注意的几个设置选项
- Rcu
- Ubuntu下Sublime Text 2的安装
- ScrollView起始位置不是最顶部的解决办法
- 分割
- 在游戏中添加Chipmunk 物理引擎