Codeforces 558C Amr and Chemistry(数论+位运算)

本文探讨了化学实验中将不同体积的化学物质转换为相同体积的最优化操作数问题,通过两种基本操作(增倍和减半)实现目标,并详细解释了解决方案和步骤。
C. Amr and Chemistry
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.

Amr has n different types of chemicals. Each chemical i has an initial volume of ai liters. For this experiment, Amr has to mix all the chemicals together, but all the chemicals volumes must be equal first. So his task is to make all the chemicals volumes equal.

To do this, Amr can do two different kind of operations.

  • Choose some chemical i and double its current volume so the new volume will be 2ai
  • Choose some chemical i and divide its volume by two (integer division) so the new volume will be 

Suppose that each chemical is contained in a vessel of infinite volume. Now Amr wonders what is the minimum number of operations required to make all the chemicals volumes equal?

Input

The first line contains one number n (1 ≤ n ≤ 105), the number of chemicals.

The second line contains n space separated integers ai (1 ≤ ai ≤ 105), representing the initial volume of the i-th chemical in liters.

Output

Output one integer the minimum number of operations required to make all the chemicals volumes equal.

Sample test(s)
input
3
4 8 2
output
2
input
3
3 5 6
output
5
Note

In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4.

In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1.

题意:给出n个数,让你通过下面两种操作,把它们转换为同一个数。求最少的操作数。

1.ai = ai*2

2.ai = ai/2,向下取整


思路:可以除以二 或者 乘以二,就相当于位运算的右移和左移。用两个数组,vis 数组, cnt 数组。刚开始都初始化为0; vis[i] 表示 i 这个数可以由几个数转化而来,cnt[i] 表示题目给出的 n 个数全部转化为 i 需要的操作数。

首先遍历数组找到 ai 的最大值记为 MAX,那么所有数转化的上界就是 MAX,因为如果最终转化的数如果大于MAX,那么所有值都要转化为大于MAX的那个数,很明显这不是最后的答案。

把一个数表示为二进制数,

1、如果最低位是0,那么这个数右移一位(除以2),再左移一位(乘以2),就得到原来的数

2、如果最低位是1,那么这个数右移一位(除以2),再左移一位(乘以2),得不到原来的数

那么:

3要转化为8,最少需要4步操作,先除以2,再乘以2,再乘以2,再乘以2

2要转化为8,最少需要2步操作,先乘以2,再乘以2

处理一个数 ai:

1、对 ai 执行左移操作,记录 ai 通过左移能够得到的数字,上限为MAX,vis 数组加1,cnt数组记录步数

2、对 ai 执行右移操作,直到 ai 为0

若 ai 的最低位为1,右移一步之后,进行左移,上限为MAX,维持vis数组和cnt数组

若 ai 的最低位为0,右移一步,维持vis数组和cnt数组

这样我们就把一个数的所有情况都处理出来了,并且是最少的操作数。

最后遍历数组找 vis[i] 为n,并且操作数最小。

<span style="font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

const double PI = acos(-1.0);
const double e = 2.718281828459;
const double eps = 1e-8;
const int MAXN = 100010;
int a[2*MAXN];
int vis[2*MAXN];
int cnt[2*MAXN];
int n;
int MAX = 0;

void solve(int v)
{
    vis[v]++;
    int temp1 = v;
    int temp2 = v;
    int step1 = 0;
    int step2 = 0;
    while(temp1 <= MAX)
    {
        temp1 <<= 1;
        step1++;
        vis[temp1]++;
        cnt[temp1] += step1;
    }
    while(temp2)
    {
        if(temp2&1 && temp2!=1)
        {
            temp2 >>= 1;
            step2++;
            vis[temp2]++;
            cnt[temp2] += step2;
            int temp3 = temp2;
            int step3 = step2;
            while(temp3 <= MAX)
            {
                temp3 <<= 1;
                step3++;
                vis[temp3]++;
                cnt[temp3] += step3;
            }
        }
        else
        {
            temp2 >>= 1;
            step2++;
            vis[temp2]++;
            cnt[temp2] += step2;
        }
    }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    while(cin>>n)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            MAX = max(a[i], MAX);
        }
        memset(vis, 0, sizeof(vis));
        memset(cnt, 0, sizeof(cnt));
        for(int i = 1; i <= n; i++)
            solve(a[i]);
        int ans = 1<<29;
        for(int i = 1; i <= 2*MAX-1; i++)
        {
            if(vis[i] == n)
                ans = min(ans, cnt[i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

</span>


### 关于Codeforces上的位运算题目解答 #### 逻辑与运算的应用 在处理一系列整数时,为了找到这些数字共同拥有的二进制特征,可以利用`&`(按位与)运算符。因为当两个对应的二进制位都为1时,结果才为1;只要有一个不为1,则结果就变为0。这意味着,在一组数值中通过连续执行按位与操作能够筛选出所有成员共有的二进制模式[^1]。 ```cpp #include <cstdio> using namespace std; int main() { int t; scanf("%d", &t); while(t--) { int n, x; scanf("%d%d", &n, &x); int ans = x; for(int i = 1; i < n; i++) { scanf("%d", &x); ans &= x; // 对序列中的每一个数做按位与 } printf("%d\n", ans); // 输出最终的结果 } return 0; } ``` 这段代码展示了如何遍历输入的一系列整数并应用按位与(`&`)来找出它们共享的最低限度公共设置位。 #### 处理区间内最小值的最大化问题 对于特定范围内的某些优化挑战,比如最大化某个区间的最小可能值,可以通过巧妙地调整循环条件以及使用特殊的增量表达式(~i&-~i),从而有效地减少不必要的迭代次数[^2]。 ```cpp for (long long i = l; i <= r; i += ~i&-~i) { ans = i; } cout << ans << endl; ``` 此片段尝试在一个指定范围内寻找满足一定条件下最大的起点位置。 #### 字符串匹配验证 针对包含不同类型的标记字符(如'Q'代表提问,'A'代表回答)的任务,可通过简单的计数器机制确保每种事件的发生频率相匹配,以此判断是否存在未解决的问题实例[^3]。 ```cpp // 假设已经读取到整个字符串s bool isValid(const string& s){ int questionCount = 0; for(char c : s){ if(c == 'Q') ++questionCount; else if(c == 'A'){ if(questionCount > 0)--questionCount; else return false; } } return questionCount==0; } ``` 上述伪码提供了一个简易的方法去检验给定字符串里是否有足够的答案对应每个提出的问题。 #### 差异求解策略 面对需要构建新数组使得其元素之差等于已知量的情况,考虑采用分步构造法,并借助中间变量辅助完成目标设定下的最优路径规划[^4]。 ```cpp if(u > v || (v-u)%2!=0){printf("-1\n");return 0;} if(u==0&&v==0){printf("0\n");return 0;} if(u==v){printf("1\n");printf("%lld\n",u);return 0;} w=(v-u)/2;if((u&w)==0){ printf("2\n"); printf("%lld %lld\n",w,u^w); }else{ printf("3\n"); printf("%lld %lld %lld\n",u,w,w); } ``` 这里展示的是根据不同情况分别采取最短步骤达到预期效果的过程。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值