A
思路:
B
思路:有一种贪心的写法,将整个数组排序以后比较两个相邻数的同或值,取 m a x max max,不会证明
int th(int x, int y, int z) {
int res = 0;
for (int i = z - 1; i >= 0; i --) {
int dx = (x >> i & 1);
int dy = (y >> i & 1);
if (dx == dy) res = res * 2 + 1;
else res = res * 2;
}
return res;
}
void solve() {
int n, m; cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; i ++) {
cin >> a[i];
}
sort(a.rbegin(), a.rend());
int ans = 0;
for (int i = 1; i < n; i ++) {
ans = max(ans, th(a[i], a[i - 1], m));
}
cout << ans << '\n';
}
思路:字典树的写法,和最大异或对的差不多
int tr[N][2], vis[N][2];
int n, m, idx;
void insert(int x) {
int p = 0;
for (int i = m - 1; i >= 0; i --) {
int u = (x >> i & 1);
if (!tr[p][u]) tr[p][u] = ++idx;
p = tr[p][u];
}
}
int queyr(int x) {
int p = 0, res = 0;
for (int i = m - 1; i >= 0; i --) {
int u = (x >> i & 1);
if (tr[p][u]) res = res * 2 + 1, p = tr[p][u];
else res = res * 2, p = tr[p][u ^ 1];
}
return res;
}
void solve() {
cin >> n >> m;
int ans = 0;
for (int i = 0, x; i < n; i ++) {
cin >> x;
ans = max(ans, queyr(x));
insert(x);
}
cout << ans << '\n';
}
C
思路:
D
思路:直接根号判断素数即可
void solve() {
int x; cin >> x;
if (!judge(x)) cout << "No\n";
else cout << "Yes\n";
}
E
思路:
F
思路:
G
思路:模拟模拟
void solve() {
string s; cin >> s;
vector<int> a;
int sum = 0, res = 0;
for (int i = 0; i < s.size(); i ++) {
if (s[i] >= '0' && s[i] <= '9') {
res = res * 10 + (s[i] - '0');
} else {
a.push_back(res);
res = 0;
}
}
if (res) a.push_back(res);
sort(a.rbegin(), a.rend());
cout << a[0];
sum += a[0];
for (int i = 1; i < a.size(); i ++) {
cout << '+' << a[i];
sum += a[i];
}
cout << '\n';
cout << sum << '\n';
}
H
思路:我肯定要拿最多,要尽量让一半的人拿 1 1 1,奇数偶数讨论一下
void solve() {
int n, x; cin >> x >> n;
int t;
if (n & 1) t = n / 2;
else t = n / 2 - 1;
cout << x - t << '\n';
}
I
思路:
J
思路:
K
思路:
L
思路: