Codeforces 558C Amr and Chemistry rules
Question Link
Question:
Given n-long sequence
You can select a number to make it * = 2 or/= 2.
Ask how many operations are performed to make all numbers equal.
Ideas:
For each number, calculate the number that can be changed to and the minimum number of steps.
Cnt [I] indicates that the number of cnt in the sequence can be changed to I
Step [I] indicates the cost and the amount of I that can be changed to I.
Notice: if a [I] = 7, a [I] also can reach 6. by/= 2 then * = 2
7-> 3-> 1 ..
3-> 6-> 12
1-> 2-> 4
If it is an odd number (excluding 1), it takes two steps to reach the location of a [I]-1.
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; template
Inline bool rd (T & ret) {char c; int sgn; if (c = getchar (), c = EOF) return 0; while (c! = '-' & (C <'0' | c> '9') c = getchar (); sgn = (c = '-')? -1: 1; ret = (c = '-')? 0: (c-'0'); while (c = getchar (), c> = '0' & c <= '9 ') ret = ret * 10 + (c-'0'); ret * = sgn; return 1;} template
Inline void pt (T x) {if (x <0) {putchar ('-'); x =-x;} if (x> 9) pt (x/10); putchar (x % 10 + '0');} typedef long ll; typedef pair
Pii; const int n= 200005; const int inf = 1e9 + 10; int N; int a [n]; int cnt [N], step [N]; void up (int y, int s) {// enumerate up while (y <N) {cnt [y] ++; step [y] + = s; y <= 1; s ++ ;}} void go (int y) {up (y, 0); int now = 1; while (y) {// keep y down to reduce if (y> 1) & (y & 1) {y >>=1; up (y, now );} else {y> = 1; cnt [y] ++; step [y] + = now;} now ++ ;}} int main () {while (cin> n) {memset (cnt, 0, sizeof cnt); memset (step, 0, sizeof step); for (int I = 1; I <= n; I ++) {rd (a [I]); go (a [I]);} int ans = step [1]; for (int I = 2; I <N; I ++) {if (cnt [I] = n) ans = min (ans, step [I]);} pt (ans);} return 0 ;}