Bits Reverse(二进制+贪心)

本文介绍了一道算法题目,目标是最少次数地通过翻转二进制位来使两个数相等。文章详细解释了解题思路,即分别考虑奇数位和偶数位的情况,并给出了一段C++实现代码。

Bits Reverse

[Link](Problem - D - Codeforces)

题意

给你两个数n,m,每次操作可以将n的连续三个二进制位翻转,问你最少操作多少次可以使n变成m。

题解

首先发现操作并不会将n的奇数位和偶数位的交换,分奇偶来看,只看奇数位,操作等价于将奇数位的相邻两位交换,如果将n和m的二进制的每一位处理出来假设为 c n t a n , c n t b n cnta_n,cntb_n cntan,cntbn,如果他们为1的位数不同则一定无解,假设当某一个 c n t b i cntb_i cntbi为1且 c n t a i cnta_i cntai为0,则第 a 的 二 进 制 第 i a的二进制第i ai位应该由其他位转移过来,那么一定是越近越好,所有将所有的n和m二进制所有为1的位处理出来,从前往后算一下差值即可,偶数位同理。

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath> 
#include <stack>
#include <iomanip>
#include <deque> 
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
LL n, m, k;
int a[N], b[N];
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int T, C = 0;
    cin >> T;
    while (T -- ) {
        cin >> n >> m;
        cout << "Case "<< ++ C <<": ";
        for (int i = 60; i >= 0; i -- ) 
            a[i] = (n >> i & 1);
        for (int i = 64; i >= 0; i -- )
            b[i] = (m >> i & 1);
        int sum = 0;
        vector<int> cnta, cntb;
        for (int i = 60; i >= 0; i -- ) 
            if (i & 1) {
                if (a[i]) cnta.push_back(i);
                if (b[i]) cntb.push_back(i);
            }
        if (cnta.size() != cntb.size()) {
            cout << -1 << endl;
            continue ;
        }
        for (int i = 0; i < cnta.size(); i ++ ) sum += abs(cnta[i] - cntb[i]) / 2;
        cnta.clear(), cntb.clear();
        for (int i = 60; i >= 0; i -- ) 
            if (i % 2 == 0) {
                if (a[i]) cnta.push_back(i);
                if (b[i]) cntb.push_back(i);
            }
        // cout << endl;
        // for (auto x : cnta) cout << x << ' ';
        // cout << endl;
        // for (auto x : cntb) cout << x << ' ';
        // cout << endl;
        if (cnta.size() != cntb.size()) {
            cout << -1 << endl;
            continue ;
        }
        for (int i = 0; i < cnta.size(); i ++ ) sum += abs(cnta[i] - cntb[i]) / 2;
        cout << sum << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值