fork(11) download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <string>
  6. #include <cctype>
  7. #include <stack>
  8. #include <queue>
  9. #include <vector>
  10. #include <map>
  11. #include <sstream>
  12. #include <cmath>
  13. #include <limits>
  14. #include <utility>
  15. #include <iomanip>
  16. #include <set>
  17. #include <numeric>
  18. #include <cassert>
  19. #include <ctime>
  20.  
  21. #define INF_MAX 2147483647
  22. #define INF_MIN -2147483647
  23. #define INF_LL 9223372036854775807LL
  24. #define INF 2000000000
  25. #define PI acos(-1.0)
  26. #define EPS 1e-8
  27. #define LL long long
  28. #define mod 1000000007
  29. #define pb push_back
  30. #define mp make_pair
  31. #define f first
  32. #define s second
  33. #define setzero(a) memset(a,0,sizeof(a))
  34. #define setdp(a) memset(a,-1,sizeof(a))
  35. #define bits(a) __builtin_popcount(a)
  36.  
  37. using namespace std;
  38.  
  39. int cnt[100005], vis[100005], steps[100005];
  40.  
  41. int main()
  42. {
  43. //ios_base::sync_with_stdio(0);
  44. //freopen("lca.in", "r", stdin);
  45. //freopen("lca.out", "w", stdout);
  46. int n, res = INF, x, y;
  47. scanf("%d", &n);
  48. for(int i=1;i<=n;i++)
  49. {
  50. scanf("%d", &x);
  51. queue<pair<int, int> > q;
  52. q.push(mp(x, 0));
  53. while(!q.empty())
  54. {
  55. x = q.front().f;
  56. y = q.front().s;
  57. q.pop();
  58. if(x > 100003) continue;
  59. if(vis[x] == i) continue;
  60. vis[x] = i;
  61. steps[x]+=y;
  62. cnt[x]++;
  63. q.push(mp(x * 2, y + 1));
  64. q.push(mp(x / 2, y + 1));
  65. }
  66. }
  67. for(int i=0;i<=100000;i++)
  68. if(cnt[i] == n)
  69. if(res > steps[i])
  70. res = steps[i];
  71. printf("%d", res);
  72. return 0;
  73. }
Success #stdin #stdout 0s 4452KB
stdin
3
4 8 2
stdout
2