这个代码的时间复杂度是否正确?
查看原帖
这个代码的时间复杂度是否正确?
1001535
Ahws_rwhy楼主2025/8/2 18:19
#include <bits/stdc++.h>
using namespace std;
void solve(int n) {
	vector<bool> u(n + 1);
	vector<vector<int>> g;
	for (int y = n; y >= 1; y--)
	     if (!u[y]) {
			int x = y;
			while (--x && (u[x] || (x & y)));
			if (x)g.push_back({x, y}), u[x] = u[y] = 1;
		}
	vector<int> z = {0};
	for (int x = 1; x <= n; x++)
	if (!u[x]) z.push_back(x);
	g.push_back(z);
	cout << g.size() << '\n';
	for (auto&v : g) {
		cout << v.size();
		for (int x : v)cout << ' ' << x;
		cout << '\n';
	}
}
int main() {
	int T;
	cin >> T;
	while (T--) {
		int n;
		cin >> n;
		solve(n);
	}
	return 0;
}
2025/8/2 18:19
加载中...