解法一:贪心
两个数组分别按升序排列,然后正数和正数相乘、负数和负数相乘,且两者的绝对值尽可能大(数学原理)。
#include <bits/stdc++.h>using namespace std;long long A[100005], B[100005];int main() {ios::sync_with_stdio(false);cin.tie(0);int lenA, lenB;cin >> lenA;for (int i = 0; i < lenA; ++i) {cin >> A[i];}cin >> lenB;for (int i = 0; i < lenB; ++i) {cin >> B[i];}sort(A, A + lenA);sort(B, B + lenB);double total = 0;int head = 0, tail = lenB - 1;for (int i = 0; i < lenA; ++i) {if (A[i] < 0) {if (B[head] < 0) {total += A[i] * B[head];++head;}} else {break;}}for (int i = lenA - 1; i >= 0; --i) {if (A[i] > 0) {if (B[tail] > 0) {total += A[i] * B[tail];--tail;}} else {break;}}cout << fixed;cout << setprecision(0) << total << '\n';}
