Even Little John needs money to buy a house. But he recently lost his job; how will he earn money now? Of course, by playing a game that gives him money as a reward! Oh well, maybe not those kinds of games you are thinking about.
There are distinct points on the plane. Initially, your score is . To increase your score, you can perform the following operation:
- Choose three distinct points which are not collinear;
- Increase your score by the area of the triangle formed by these three points;
- Then, erase the three points from the plane.
An instance of the game, where the operation is performed twice.
Let be the maximum number of operations that can be performed. For example, if it is impossible to perform any operation, is . Additionally, define as the maximum possible score achievable by performing the operation exactly times. Here, is defined for all integers such that .
Find the value of , and find the values of for all integers independently.
Input
Each test contains multiple test cases. The first line contains the number of test cases . The description of the test cases follows.
The first line of each test case contains two integers and .
The second line of each test case contains pairwise distinct integers — the points on .
The third line of each test case contains pairwise distinct integers — the points on .
It is guaranteed that both the sum of and the sum of over all test cases do not exceed .
Output
For each test case, given that the maximum number of operations is , you must output at most two lines:
- The first line contains the value of ;
- The second line contains integers denoting . You are allowed to omit this line if is .
Note that under the constraints of this problem, it can be shown that all values of are integers no greater than .
Example
Input
51 300 1 -12 40 100-100 -50 0 502 40 1000-100 -50 0 506 620 1 27 100 43 42100 84 1 24 22 778 2564040265 -509489796 469913620 198872582 -400714529 553177666 131159391 -20796763-1000000000 1000000000
Output
122150 20021000 200499 198 260 28322000000000 2027422256
Note
On the first test case, there are points .
It can be shown that you cannot perform two or more operations. The value of is , and you are only asked for the value of .
You can choose , , and as the three vertices of the triangle. After that, your score is increased by the area of the triangle, which is . Then, the three points are erased from the plane. It can be shown that the maximum value of your score after performing one operation is . Therefore, the value of is .
On the fifth test case, there are points.
It can be shown that you cannot perform three or more operations. The value of is , and you are asked for the values and .
To maximize the score with only one operation, you can choose three points , , and . Then, the three points are erased from the plane. It can be shown that the maximum value of your score after performing one operation is . Therefore, the value of is .
To maximize the score with exactly two operations, you can choose the following sequence of operations.
- Choose three points , , and . The three points are erased.
- Choose three points , , and . The three points are erased.
Then, the score after two operations becomes . It can be shown that the maximum value of your score after performing exactly two operations is . Therefore, the value of is .
Solution
- 要构造满足非共线的三点,需要从 的点中选 个、 的点中选 个,或反过来。
- 每次选出的一组三点形成的三角形面积即对应行上选出的 点的横坐标之差(因为高度为 ,)。
- 先对 的所有点进行排序,计算“前后配对”的最大差值之和形成前缀数组 ;同理对 的所有点生成前缀数组 。 表示恰好在 行完成 次“2点配对”操作的总最大面积,同理 针对 行。
- 每次总共做 次操作,需要在 行做 次、在 行做 次,要求 。
- 用三分搜索或其它方法在可行区间内寻找 ,使得 最大,记为 。
- 最后找出最大可行的 值 ,并输出 。
vector<int> buildPrefix(vector<int> &arr) { sort(arr.begin(), arr.end()); int half = arr.size() / 2; vector<int> diff(half); for (int i = 0; i < half; i++) { diff[i] = arr[arr.size() - 1 - i] - arr[i]; } sort(diff.begin(), diff.end(), greater<int>()); vector<int> prefix(half + 1, 0LL); for (int i = 1; i <= half; i++) { prefix[i] = prefix[i - 1] + diff[i - 1]; } return prefix;}
int ternaryMax(const vector<int> &A, const vector<int> &B, int k, int low, int high) { if (low > high) return 0; auto G = [&](int x) { return A[x] + B[k - x]; }; while (high - low >= 3) { int m1 = low + (high - low) / 3; int m2 = high - (high - low) / 3; if (G(m1) < G(m2)) low = m1 + 1; else high = m2 - 1; } int ret = 0; for (int i = low; i <= high; i++) { ret = max(ret, G(i)); } return ret;}
void solve() { int n, m; cin >> n >> m; vector<long long> a(n), b(m); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int j = 0; j < m; j++) { cin >> b[j]; }
vector<long long> preA = buildPrefix(a); vector<long long> preB = buildPrefix(b); int maxPickA = (int)preA.size() - 1; int maxPickB = (int)preB.size() - 1; int kmax = min((n + m) / 3LL, (long long)maxPickA + maxPickB); vector<long long> f(kmax + 1, 0LL);
for (int k = 1; k <= kmax; k++) { // i的可行区间 // 要满足 i + (k−i) = k,2*i + (k−i) <= n => i <= n-k // i + 2*(k−i) <= m => i >= 2*k - m // 同时 i >= 0, i <= k, i <= maxPickA, k−i <= maxPickB int low = max(0LL, (int)max((long long)k - maxPickB, 2LL * k - m)); int high = min(k, min(maxPickA, n - k)); if (low <= high) { auto bestVal = ternaryMax(preA, preB, k, low, high); f[k] = bestVal; } }
while (kmax > 0 && f[kmax] == 0) kmax--;
cout << kmax << endl; if (kmax > 0) { for (int i = 1; i <= kmax; i++) { cout << f[i] << (i < kmax ? ' ' : '\n'); } }}
signed main() { setIO(); int tt = 1; cin >> tt; while (tt--) { solve(); } return 0;}