牛牛有一些木棍,现在牛牛想让这些木棍都变成同样的长度
牛牛可以进行如下的操作
选择一根长度为L>=2的木棍
如果L为偶数,将木棍一分为2,变成两根L/2的木棍
否则,分成一根为(L+1) / 2, 另一根为(L-1) / 2;
在分成的两根中选择其中一根,扔掉另外一根
牛牛的任务是让剩下的木棍变成一样长 ,现在请你帮助牛牛算出最少需要几步可以完成任务.
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head #ifdef ONLINE_JUDGE const int N = 1e6+10; #else const int N = 111; #endif int n; map<int,int> cnt, mi; int main() { scanf("%d", &n); REP(i,1,n) { int t; scanf("%d", &t); set<int> s; queue<pii> q; q.push(pii(t,0)); while (q.size()) { pii u = q.front(); q.pop(); if (s.count(u.x)) continue; s.insert(u.x); ++cnt[u.x], mi[u.x] += u.y; q.push(pii(u.x/2,u.y+1)); if (u.x%2) q.push(pii(u.x/2+1,u.y+1)); } } int ans = 1e9; for (auto &&t:cnt) if (t.y==n) ans = min(ans, mi[t.x]); printf("%d\n", ans); }