CF 558C Amr and Chemistry Thinking, Binary

Question meaning: sequence a of length n, operation 1: a[i]*2. operation 2: a[i]/2 (round down).
n, a[i]<=1e5 .How many operations are required to make the number of sequence a the same?




The final number will definitely not exceed the maximum value mx in the sequence.
If y>mx, then the last operation of each element must be *2=y>mx , at this time, the multiplication by 2 operation is removed, the number of operands is reduced, and the number of the sequence is still the same. The


operation is multiplied or divided by 2 [round down ]
every time . 10-5-2-4-8-... When the division changes from an odd number to an even number, the multiplication can be changed to a new number at this time. That is, if the lowest bit of the binary is 1, then at this time / 2 is followed by * 2 to get The new number is different from the original. The maximum number of binary digits is m. In the worst case, the bits are all 1, and at most m*(m+1)/2 new numbers are generated (the prefix is ​​11 after each operation. .. number at the beginning).







Calculate f[i]: It means that the first k numbers become the minimum value of i. O(n*m*(m+1)/2)

#include <bits/stdc++.h>
using namespace std;
const int N=3e5+5,inf=0x3f3f3f3f;
int n,a[N],f[N],h[N];
void calc(int x,int cnt)
{
	int mx=3e5;
	while(x<=mx)
	{
		f[x]+=cnt,h[x]++;
		x*=2;
		cnt++;
	}
}
intmain()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
//	freopen("1.txt","r",stdin);
	memset(f,0,sizeof(f));
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++)
	{
		h[a[i]]++;
		calc (a [i] * 2.1);
		int cnt=1,num;
		while(a[i])
		{
			if(a[i]!=1&&a[i]%2)
				calc(a[i]/2*2,cnt+1);
			a[i]/=2;
			f[a[i]]+=cnt,h[a[i]]++;
			cnt++;
		}
	}
	int res=inf;
	for(int i=1;i<N;i++)
		if(h[i]==n)
			res = min (res, f [i]);
	cout<<res<<'\n';
	return 0;
}

Standard distance:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <limits>
#include <utility>
#include <iomanip>
#include <set>
#include <numeric>
#include <cassert>
#include <ctime>
 
#define INF_MAX 2147483647
#define INF_MIN -2147483647
#define INF_LL 9223372036854775807LL
#define INF 2000000000
#define PI acos(-1.0)
#define EPS 1e-8
#define LL long long
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define setzero(a) memset(a,0,sizeof(a))
#define setdp(a) memset(a,-1,sizeof(a))
#define bits(a) __builtin_popcount(a)
 
using namespace std;
 
int cnt[100005], vis[100005], steps[100005];
 
intmain()
{
  //ios_base::sync_with_stdio(0);
  //freopen("lca.in", "r", stdin);
  //freopen("lca.out", "w", stdout);
  int n, res = INF, x, y;
  scanf("%d", &n);
  for(int i=1;i<=n;i++)
  {
    scanf("%d", &x);
    queue<pair<int, int> > q;
    q.push(mp(x, 0));
    while(!q.empty())
    {
      x = q.front().f;
      y = q.front().s;
      q.pop();
      if(x > 100003) continue;
      if(vis[x] == i) continue;
      show [x] = i;
      steps[x]+=y;
      cnt[x]++;
      q.push(mp(x * 2, y + 1));
      q.push(mp(x / 2, y + 1));
    }
  }
  for(int i=0;i<=100000;i++)
    if(cnt[i] == n)
      if(res > steps[i])
        res = steps[i];
  printf("%d", res);
  return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326401965&siteId=291194637