题目
Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|.
In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5].
Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b.
输入
The first line contains integer n (1 ≤ n ≤ 2000) — the number of elements in the array a.
The second line contains n integers ai ( - 109 ≤ ai ≤ 109) — the elements of the array a.
The third line contains integer m (1 ≤ m ≤ 2000) — the number of elements in the array b.
The fourth line contains m integers bj ( - 109 ≤ bj ≤ 109) — the elements of the array b.
输出
In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps.
The second line should contain the number of swaps k (0 ≤ k ≤ 2).
Each of the next k lines should contain two integers xp, yp (1 ≤ xp ≤ n, 1 ≤ yp ≤ m) — the index of the element in the array a and the index of the element in the array b in the p-th swap.
If there are several optimal solutions print any of them. Print the swaps in order the professor did them.
解法
我们讨论三种情况不交换,交换一次,交换两次 对于前两种情况我们可以直接讨论对于最后一种情况我们先处理同一数组中两两相等的情况然后用双指针去维护操作之后suma刚好小于sumb的情况然后讨论一下就行了
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const int N = 2e5 + 10;
int a[N], tot, b[N];
pair<int, int> que[N];
void solve() {
int n, m; scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
scanf("%d", &m);
for (int i = 1; i <= m; i++) scanf("%d", b + i);
ll numa = 0, numb = 0;
for (int i = 1; i <= n; i++) numa += a[i];
for (int i = 1; i <= m; i++) numb += b[i];
ll mi = abs(numa - numb);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
ll x = numa + b[j] - a[i];
ll y = numb - b[j] + a[i];
if (abs(x - y) < mi) {
mi = abs(x - y);
tot = 1;
que[tot] = {i, j};
}
}
if (n < 2 || m < 2) {
printf("%lld\n%d\n", mi, tot);
if (tot)
printf("%d %d\n", que[1].first, que[1].second);
return;
}
vector<array<ll, 3>> p, q;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (i != j) q.push_back({a[i] + a[j], i, j});
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= m; j++)
if (i != j) p.push_back({b[j] + b[i], i, j});
sort(q.begin(), q.end(), [](array<ll, 3> a, array<ll, 3> b) {
return a[0] < b[0];
});
sort(p.begin(), p.end(), [](array<ll, 3> a, array<ll, 3> b) {
return a[0] < b[0];
});
int l = 0;
for (int i = 0; i < q.size(); i++) {
while (l < p.size() && (numa + 2 * p[l][0] < numb + 2 * q[i][0])) l ++;
if (l < p.size()) {
ll ans = abs((numa + 2 * p[l][0]) - (numb + 2 * q[i][0]));
if (ans < mi) {
tot = 2;
mi = ans;
que[1] = {q[i][1], p[l][1]};
que[2] = {q[i][2], p[l][2]};
}
}
if (l > 0) {
ll ans = abs((numa + 2 * p[l - 1][0] )- (numb + 2 * q[i][0]));
if (ans < mi) {
tot = 2;
mi = ans;
que[1] = {q[i][1], p[l-1][1]};
que[2] = {q[i][2], p[l-1][2]};
}
}
}
printf("%lld\n%d\n", mi, tot);
if (tot > 0)
printf("%d %d\n", que[1].first, que[1].second);
if (tot > 1)
printf("%d %d\n", que[2].first, que[2].second);
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) solve();
}