Codeforces Amr and Chemistry(数学+乱搞)
来源:互联网 发布:mysql distinct 用法 编辑:程序博客网 时间:2024/05/21 09:18
题意:给n个数,每个数每次可以乘二或除以二(向下取整相当于左移或右移),问最少经过多少次操作可以使这n个数变相等。
思路:首先考虑每个数的可能取值,将一个数表示成s*2^k的形式,s是奇数。
那么这个数的所有可能取值为s'*2^x,(s'=s/2,(s/2)/2,.....)且s'*2^x<=100000
因为这题数据范围不大,而且每个值可能的取值不多最多几百个,所以记录1到100000每个值可能被取到的次数以及总操作数,最后从1遍历到100000取最小的ans即可
ps:个人赛这道题做了一下午无数乱搞写的代码巨恶心......晚上回来优化了一下勉强能看..........
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack> #include<string>#include<map> #include<set>#define eps 1e-6 #define LL long long using namespace std; //const int maxn = 100 + 5;const int INF = 100000000;int n, abc;int vol[100010], ans[100010]; //vol代表每个容量可能被取到的次数,ans表示取到该容量所要的操作总数 int main() {//freopen("input.txt", "r", stdin);while(scanf("%d", &n) == 1) {memset(vol, 0, sizeof(vol));memset(ans, 0, sizeof(ans));for(int i = 0; i < n; i++) {cin >> abc;int t = abc;int cnt = 1, cnt1 = 0;abc <<= 1;while(abc <= 100000) {vol[abc]++;ans[abc] += cnt;cnt++;abc <<= 1;}cnt = 0;while(t%2 == 0) {vol[t]++;ans[t] += cnt;cnt++;cnt1++;t >>= 1;}vol[t]++;ans[t] += cnt;t >>= 1;cnt1++;while(t != 0) {int u = t;int cnt2 = cnt1;while(u <= 100000) {vol[u]++;ans[u] += cnt2;cnt2++;u <<= 1;}if(t%2 == 0) {while(t%2 == 0) {t >>= 1; cnt1++;vol[t]++;ans[t] += cnt1;}}t >>= 1; cnt1++;}}int pans = INF;for(int i = 0; i <= 100000; i++) if(vol[i] == n)pans = min(pans, ans[i]);//cout << vol[2] << endl << vol[1] << endl << vol[8] << endl;cout << pans << endl;}return 0;}
0 0
- Codeforces Amr and Chemistry(数学+乱搞)
- Codeforces 558C Amr and Chemistry(dp)
- codeforces 558C Amr and Chemistry(位操作)
- 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
- codeforcesC. Amr and Chemistry
- 【CF】【Amr and Chemistry】
- codeforces558C Amr and Chemistry
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry(技巧)
- CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
- !codeforces 558C Amr and Chemistry-yy题-(位运算相关)
- C. Amr and Chemistry(Codeforces Round #312 (Div. 2) 二进制+暴力)
- [CodeEdit--Sublime]一些好用的Plugins
- 分别使用Eclipse EE和IntelliJ IDEA构建基于Maven的Java Web项目
- 论编程学习方法的重要性
- 2015年7月22日笔记
- poj1836--Alignment题解
- Codeforces Amr and Chemistry(数学+乱搞)
- linux解压zip中文乱码问题
- javaScript中自定义对象(类的定义方式)
- Java设计模式之基本原则(一)
- 杭电oj red and black(简单的深搜)
- 【IAP支付之一】In-App Purchase Walk Through 整个支付流程
- C# Winform界面中的分隔线问题
- try catch注意事项
- jar包与aar包文件的区别