Codeforces Round #768 (Div. 2)
A
#include <bits/stdc++.h>
using namespace std;
int a[10010], b[10010];
int main() {
int T;
cin >> T;
while (T -- ) {
int n;
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
for (int i = 1; i <= n; i ++ ) cin >> b[i];
for (int i = 1; i <= n; i ++ )
if (a[i] < b[i])
swap(a[i], b[i]);
sort(a + 1, a + 1 + n);
sort(b + 1, b + 1 + n);
cout << a[n] * b[n] << endl;
}
return 0;
}
B
贪心,由于最后一个数字无法被改变,所以可以将数组反转后再从前往后贪心
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int T, n;
int a[N];
int main() {
cin >> T;
while (T -- ) {
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
for (int i = 1; i <= n; i ++ )
a[i] == a[n] ? a[i] = 1 : a[i] = 0;
int res = 0, pos = 1;
reverse(a + 1, a + 1 + n);
while (pos < n) {
if (a[pos + 1] == a[1]) {
pos ++;
continue;
}
pos *= 2;
res ++;
}
cout << res << endl;
}
return 0;
}
C
特殊处理\(m=0和m=n-1\)的情况
首先考虑\(\sum\limits_1^{n/2}{a_i\&{b_i}}=0\)的方案
之后在考虑\(a_i \& b_i == m\)
#include <bits/stdc++.h>
#define PII pair<int, int>
using namespace std;
int T, n, m;
int main() {
scanf("%d", &T);
while (T -- ) {
scanf("%d%d", &n, &m);
int s = n;
vector<PII> v, ans;
n --;
while (n >= 0) {
int l, r;
int x = 1;
while (true) {
if (x * 2 >= n) {
int len = n - x + 1;
l = x - len;
r = n;
n = x - len - 1;
v.push_back({l, r});
break;
}
else x *= 2;
}
}
reverse(v.begin(), v.end());
for (auto &[x, y] : v) {
int l = x, r = y;
while (l < r) {
ans.push_back({l, r});
l ++, r --;
}
}
n = s;
if (m == 0) {
for (auto &[x, y] : ans)
printf("%d %d\n", x, y);
continue;
}
if (m == n - 1 && n > 4) {
ans[0] = {n - 2, n - 1};
ans[1] = {1, n - 3};
ans[2] = {0, 2};
for (auto &[x, y] : ans)
printf("%d %d\n", x, y);
continue;
}
int pos = -1;
for (int i = 0; i < ans.size(); i ++ )
if (ans[i].first == m || ans[i].second == m) {
if (ans[i].first != m) swap(ans[i].first, ans[i].second);
pos = i;
}
bool flag = 0;
for (int i = 0; i < ans.size(); i ++ ) {
if (i == pos) continue;
if (((ans[i].first & ans[pos].first) == m) && ((ans[i].second & ans[pos].second) == 0)) {
flag = true;
swap(ans[i].second, ans[pos].first);
break;
}
if (((ans[i].second & ans[pos].first) == m) && ((ans[i].first & ans[pos].second) == 0)) {
flag = true;
swap(ans[i].first, ans[pos].first);
break;
}
}
if (!flag) puts("-1");
else {
for (auto &[x, y] : ans)
printf("%d %d\n", x, y);
}
}
return 0;
}