题解
最大方案 先将两个货物高度持平 再判断当前高度下哪个货物比较重(包含累加的重量) 移动较重的到另一侧
最小方案 对两堆货物求和 和较小的为最小方案 注意反向输出
求最大方案时需要先输出最大值再输出移动方案 可以用stringstream流将输出存起来后面直接输出
AC代码
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 10;
int a[MAXN], b[MAXN];
int main()
{
#ifdef LOCAL
freopen("C:/input.txt", "r", stdin);
#endif
int T;
cin >> T;
while (T--)
{
int N, M;
ll sa = 0, sb = 0; //ab求和
cin >> N >> M;
for (int i = 1; i <= N; i++)
scanf("%lld", &a[i]), sa += a[i];
for (int i = 1; i <= M; i++)
scanf("%lld", &b[i]), sb += b[i];
ll tot = 0, mx = 0; //已经移动过的重量 最大答案
int i = N, j = M, k = 0; //ab最高位置 移动过的在a还是b上0为a
stringstream ss; //将输出用字符串流先存储起来
while (i && j) //一个到底则结束 先持平再尽量移动重的
{
if (i > j || i == j && a[i] + tot * !k > b[j] + tot * k) //a高或者a顶部重
{
ss << "A>" << a[i] + tot * !k << ">B\n";
mx += a[i] + tot * !k; //记录答案
tot += a[i--], k = 1; //将A上移动到B
}
else if (i < j || i == j && a[i] + tot * !k < b[j] + tot * k)
{
ss << "B>" << b[j] + tot * k << ">A\n";
mx += b[j] + tot * k;
tot += b[j--], k = 0;
}
}
cout << mx << endl; //输出最大方案
cout << ss.str();
cout << min(sa, sb) << endl; //输出最小方案
if (sa < sb)
for (int i = N; i >= 1; i--)
printf("A>%d>B\n", a[i]);
else
for (int i = M; i >= 1; i--)
printf("B>%d>A\n", b[i]);
}
return 0;
}