Test instructions: The number of n, each can be multiplied by two or divided by two (rounding down equivalent to left or right), ask at least how many operations can make this n number disguised.
Idea: First of all consider the possible value of each number, a number is represented as s*2^k form, S is odd.
Then all possible values for this number are S ' *2^x, (S ' =s/2, (S/2)/2,.....) and S ' *2^x<=100000
Because the data range is small, and each value may not be a maximum of hundreds of, so record 1 to 100000 the number of times each value may be taken and the total number of operations, and finally from 1 to 100000 to take the smallest ans can
PS: Individual race This problem did an afternoon countless messy write code huge disgusting ... The evening came back to optimize a bit can see ...
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt #include <string> #include <map> #include <set> #define EPS 1e-6 #define LL Long long using namespace std; const int MAXN = + 5;const int INF = 100000000;int N, abc;int vol[100010], ans[100010]; Vol represents the number of times each capacity may be taken, ANS indicates the total number of operations to take to that capacity 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 cn t = 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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces AMR and chemistry (math + WTF)