跳到正文

题解

题解:CF2231C Chipmunk Theo and Equality

Flurelia_Sylvaria约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年5月26日
收录
2026年8月8日
更新
2026年11月13日
篇幅
约 2 分钟
获赞
8
题目
CF2231C

版本

  1. v1当前

可以发现,对于一个数进行操作的次数是 log⁡ai\log a_i 级别的,所以考虑暴力一点的做法。

显然,如果要将一个序列中所有数变为 xx,那这个 xx 的取值范围肯定是原序列中的最小值经过若干次变换可以得到的数,所以可以直接把这些数存下来,然后每一个数都去算一遍答案求最小值即可。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
int t;
int n;
int a[N];
int ans=1e9;
int calc(int u,int v){//算把u变成v的操作次数
   int cnt=0;
   if(u==v) return 0;
   while(1){
       if(u&1) u++;
       else u>>=1;
       cnt++;
       if(u==v) return cnt; 
       if(u<v-1) return 1e9;//如果u不能变成v,返回极大值,不统计答案
   }
}
int solve(int x){
   int cnt=0;
   for(int i=1;i<=n;i++) {
       cnt+=calc(a[i],x);
   }
   return cnt;
}
vector<int> temp;
signed main()
{
   cin>>t;
   while(t--){
       cin>>n;
       temp.clear();
       ans=1e9;
       for(int i=1;i<=n;i++) cin>>a[i];
       sort(a+1,a+n+1);
       int tp=a[1];
       temp.push_back(a[1]);
       while(1){
           if(a[1]&1) a[1]++;
           else a[1]>>=1;
           temp.push_back(a[1]);
           if(a[1]==1) break;
       }
       a[1]=tp;
       for(int &i : temp){
           ans=min(solve(i),ans);
       }
       cout<<ans<<'\n';
   }
   return 0;
}