Strange Test (贪心)

Strange Test

[Link](Problem - C - Codeforces)

题意

给你两个数 a , b a,b a,b每次有三种操作 1. a = a + 1   2. b = b + 1   3. a = a ∣ b 1.a=a+1\ 2.b=b+1\ 3.a = a | b 1.a=a+1 2.b=b+1 3.a=ab,问你最少操作多少次使得 a = = b a==b a==b

思路

首先我们最多或一次,因为 a ∣ = b a|=b a=b a > = b a>=b a>=b所以只需要让 b b b加过去就可以了。

如果说不适用或的话,操作数就是 b − a b-a ba,剩下就是使用一次或和它比较即可,贪心的来想就是一个变一个不变然后到达某个界限的时候直接或一下,例如变 a a a则当 a , ∣ b = = b 即 可 a^,|b==b即可 ab==b,或者是 b , ∣ a = b , b^,|a=b^, ba=b,,要保证 a ∣ a| a完不会让结果增加。

贪不出来我们可以写写式子,我们设 a 1 , b 1 a1,b1 a1,b1的时候他们或了一下就想同了,那么有

r e s = a 1 − a + ( a 1 ∣ b 1 ) − b + 1 = ( a 1 ∣ b 1 ) + a 1 + 1 − a − b res=a1-a+(a1|b1) -b+1=(a1|b1)+a1+1-a-b res=a1a+(a1b1)b+1=(a1b1)+a1+1ab

我们要找到 a 1 ∣ b 1 + a 1 a1|b1+a1 a1b1+a1的最小值,可以枚举 a a a然后贪心的改变 b b b的值保证 b 1 > = b b1>=b b1>=b且取得最小。发现从低位往高位枚举当前位置放的值会受前面放的影响,因此选择从高位往低位贪,如果当前位置 b b b 0 且 a , 为 1 0且a^,为1 0a1那么这一位将 b , 为 1 b^,为1 b1就可以保证 b , > b b^,>b b>b后面只需和 a , a^, a,填一样即可。

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 ++;
}
int n, m, k;
int a, b;

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int T;
	cin >> T;
	while (T --) {
		cin >> a >> b;
		int res = b - a;
		for (int i = a; i <= b; i ++) {
			int k = b;
			for (int j = 20; j >= 0; j --) 
				if ((i >> j) & 1)
					if ((k >> j & 1) == 0) {
						k |= 1 << j;
						k &= ~((1 << j) - 1);
					}
			res = min(res, i - a + k - b + 1);			
		}
		
		cout << res << endl;
	}
	return 0;
}
#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 ++;
}
int n, m, k;
int a, b;
int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int T;
	cin >> T;
	while (T --) {
		cin >> a >> b;
		int res = INF;
		res = b - a;
		for (int i = a; i < b; i ++) 
			if ((i | b) == b) res = min(res, i - a + (i | b) - b + 1);
		for (int i = b; i <= (a | b); i ++) 
			if ((i | a) == i) res = min(res, 1 + i - b);
		
		cout << res << endl;
	}
	return 0;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值