Test instructions
n number, each time you can choose a number to make it *=2 or/=2
Ask at least how many times the operation makes all the numbers equal.
Ideas:
For each number, calculate the number that the number can be converted to, and the smallest number of steps to change to, save with two arrays
Cnt[i] indicates that the number of CNT in a sequence can be changed to I
Step[i] The number of those that can become I is the cost of I and how much.
Which, when encountered odd times to special treatment:
For example, 7 can be 6 by/2 and then * *, which means that any odd I (excluding 1) can be changed to i-1 by 2 operations;
The code is as follows:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;typedef Long Long ll;const int N = 1e5+10;const int INF = 1e9 + 10;int n;int a[n], cnt[n];//number ll step[n];//total number of steps void u p (int x, int now) {while (X < N) {Cnt[x]++;step[x] + = now;now++;x <<= 1;}} void Go (int u) {up (U, 0), Int. now = 1;while (u) {if ((u>1) && (u&1)) {u >>= 1;up (U, now);} Else{u >>= 1;cnt[u]++;step[u] + = Now;} now++;} }int Main () {while (~SCANF ("%d", &n)) {memset (CNT, 0, sizeof (CNT)), memset (step, 0, sizeof (step)), for (int i = 1; I <= n ; i++) {scanf ("%d", &a[i]); Go (A[i]);} ll ans = step[1];for (int i = 2; i < N; i++) {if (cnt[i] = = N) ans = min (ans, step[i]);} printf ("%i64d\n", ans);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces 558C AMR and Chemistry