CF一道Div3题求助!有个地方很奇怪!
  • 板块学术版
  • 楼主LukeSu
  • 当前回复5
  • 已保存回复5
  • 发布时间2022/5/10 16:37
  • 上次更新2023/10/28 01:45:49
查看原帖
CF一道Div3题求助!有个地方很奇怪!
593753
LukeSu楼主2022/5/10 16:37

题目链接

以下是AC代码:

#include<bits/stdc++.h>
#define int long long
#define QWQ cin.tie(0)->sync_with_stdio(false);
using namespace std;

signed main(){
	QWQ
	int t, n;
	cin >> t;
	
	auto solve = [&](){
		cin >> n;
		vector<int> v(n), used(n + 1, false);
		for(auto &i : v) cin >> i;
		for(auto &i : v){
			int x = i;
			while(x > n or used[x]) x /= 2;
			if(x > 0){
				used[x] = true;
			} 
			else{
				cout << "NO\n";
				return;
			}
		}
		cout << "YES\n";
	};
	
	while(t--) solve();
	
	return 0;
}

然后我对代码做了个小改动(很小很小),令我惊讶的事情发生了:

以下注释是改动的地方:

#include<bits/stdc++.h>
#define int long long
#define QWQ cin.tie(0)->sync_with_stdio(false);
using namespace std;

signed main(){
	QWQ
	int t, n;
	cin >> t;
	
	auto solve = [&](){
		cin >> n;
		vector<int> v(n), used(n + 1, false);
		for(auto &i : v) cin >> i;
		for(auto &i : v){
			int x = i;
			while(used[x] or x > n) x /= 2; //就是这句,我只是把'or'两边换了下位置,就WA了!!??
			if(x > 0){
				used[x] = true;
			} 
			else{
				cout << "NO\n";
				return;
			}
		}
		cout << "YES\n";
	};
	
	while(t--) solve();
	
	return 0;
}

报错信息:

Probably, the solution is executed with error 'out of bounds or other memory error' on the line 17(line 17就是我改动的那一句)

综上所述,这就是我感到奇怪的点:while括号里面是如何执行条件的呢?是不是有暗藏什么玄机呢?为什么更换下or两边条件的位置就会发生Runtime error?

2022/5/10 16:37
加载中...