题解:CF1937C(CF1936A)——Bitwise Operation Wizard

题解:CF1937C(CF1936A)——Bitwise Operation Wizard

一、 理解题意

1. 链接题目网址

CodeForces;

LuoGu

2. 翻译英文题面

图片来源:洛谷

二、 设计算法

1. 观察数据范围

读题发现 ∑ n ≤ 1 0 4 \sum n\leq 10^4 n104,因此考虑 O ( n ) O(n) O(n) 做法,当然是直接操作一些结论

2. 思考合理解法

前置一个重点

如果想比较两个数(比如 p a , p b p_a,p_b pa,pb)的大小,我们可以询问“? a a b b”。

显然,最终异或起来的两个数中一定有一个是 n − 1 n-1 n1

怎样找到 n − 1 n-1 n1 呢?我们自然可以用正常取最大值的方法——打擂台,只不过比较大小的操作变成了上面的询问操作。这样,找到 n − 1 n-1 n1 就需要耗费我们 n − 1 n-1 n1 次询问。

显然,如果一个数和 n − 1 n-1 n1 异或起来最大,那么它和 n − 1 n-1 n1 按位或起来也应该是最大,并且它是所有与 n − 1 n-1 n1 按位或最大的数中最小的一个。对于每一位,如果 n − 1 n-1 n1 的是 0 0 0,它必然是 1 1 1,否则显然选择 0 0 0 的那个最终的值最小,显然,按照这种规则找到的数就是我们的答案。

找到与 n − 1 n-1 n1 按位或起来最大的数也是打擂台,用 n − 1 n-1 n1 次,在过程中我们搞一个 set 把所有“和 n − 1 n-1 n1 按位或起来最大的数”扔进去,最后在这个 set 里找出一个最小的——也是打擂台,最多要用 n − 1 n-1 n1 次。

显然最终的答案就是 n − 1 n-1 n1 的位置和最后一次打擂台打出来的位置。

最终,我们发现,我们可以用不超过 3 n − 3 3n-3 3n3 次询问就解决这个问题,妥妥的 AC。

3. 分析时间代价

O ( n ) O(n) O(n)

三、 实现代码

#include<bits/stdc++.h>
#define N 11000
using namespace std;
int t,n;
char c;
set<int>ret;
char act_or(int a,int b,int k);
bool bigger(int a,int b);
int main(){
	fflush(stdout);
	scanf("%d",&t);
	while(t--){
		fflush(stdout);
		scanf("%d",&n);
		int id=1;
		for(int i=2;i<=n;i++){
			if(bigger(i,id)==true){
				id=i;
			}
		}
		int id2=1;
		if(id==1){
			id2=2;
		}
		ret.clear();
		ret.insert(id2);
		for(int i=id2+1;i<=n;i++){
			if(i==id){
				continue;
			}
			char ans=act_or(i,id2,id);
			if(ans=='>'){
				id2=i;
				ret.clear();
				ret.insert(i);
			}else{
				if(ans=='='){
					ret.insert(i);
				}
			}
		}
		int id3=0;
		for(int i:ret){
			if(id3==0){
				id3=i;
			}else{
				if(bigger(i,id3)==false){
					id3=i;
				}
			}
		}
		printf("! %d %d\n",id-1,id3-1);
	}
	return 0;
}
char act_or(int a,int b,int k){
	printf("? %d %d %d %d\n",a-1,k-1,b-1,k-1);
	fflush(stdout);
	cin>>c;
	return c;
}
bool bigger(int a,int b){
	printf("? %d %d %d %d\n",a-1,a-1,b-1,b-1);
	fflush(stdout);
	cin>>c;
	if(c=='>'){
		return true;
	}
	return false;
}

注意:这里面数组下标是 0 base!

  • 21
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于扫雷游戏的题解,我可以给你一些思路和代码示例。首先,你需要了解扫雷游戏的规则和要求。接下来,你可以使用C++语言来实现游戏逻辑和界面。 下面是一个简单的扫雷游戏的C++代码示例: ```cpp #include <iostream> #include <vector> #include <random> using namespace std; class MinesweeperGame { private: int rows; int cols; vector<vector<char>> board; vector<vector<bool>> revealed; vector<pair<int, int>> directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; public: MinesweeperGame(int m, int n, int mineCount) { rows = m; cols = n; board.resize(rows, vector<char>(cols, ' ')); revealed.resize(rows, vector<bool>(cols, false)); placeMines(mineCount); calculateNumbers(); } void printBoard() { cout << " "; for (int j = 0; j < cols; j++) { cout << j << " "; } cout << endl; for (int i = 0; i < rows; i++) { cout << i << " |"; for (int j = 0; j < cols; j++) { cout << board[i][j] << "|"; } cout << endl; } } bool isGameOver() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (board[i][j] == 'M' && revealed[i][j]) { return true; } } } return false; } void reveal(int row, int col) { if (row < 0 || row >= rows || col < 0 || col >= cols || revealed[row][col]) { return; } revealed[row][col] = true; if (board[row][col] == 'M') { return; } if (board[row][col] == '0') { for (auto dir : directions) { reveal(row + dir.first, col + dir.second); } } } private: void placeMines(int mineCount) { random_device rd; mt1

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值