Mobile wallpaper 1Mobile wallpaper 2Mobile wallpaper 3Mobile wallpaper 4Mobile wallpaper 5Mobile wallpaper 6
1328 字
7 分钟
Codeforces Round 1000 (Div. 2) D题题解

D. Game With Triangles

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 n+mn+m distinct points (a_1,0),(a_2,0),,(a_n,0),(b_1,2),(b_2,2),,(b_m,2)(a\_{1},0), (a\_{2},0), \dots , (a\_{n},0), (b\_{1},2), (b\_{2},2), \dots, (b\_{m},2) on the plane. Initially, your score is 00. 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 k_maxk\_{max} be the maximum number of operations that can be performed. For example, if it is impossible to perform any operation, k_max k\_{max} is 00. Additionally, define f(k)f(k) as the maximum possible score achievable by performing the operation exactly kk times. Here, f(k)f(k) is defined for all integers kk such that 0kkmax0 \le k \le k_{max}.

Find the value of k_maxk\_{max}, and find the values of f(x)f(x) for all integers x=1,2,,kmaxx=1,2,\dots,k_{max} independently.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t3104)(1 \le t \le 3 \cdot 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (1n,m2105)(1 \le n,m \le 2 \cdot 10^5).

The second line of each test case contains nn pairwise distinct integers a1,a2,,ana_1,a_2,\ldots,a_n — the points on y=0y=0 (109ai109)(-10^9 \le a_i \le 10^9).

The third line of each test case contains mm pairwise distinct integers b1,b2,,bmb_1,b_2,\ldots,b_m — the points on y=2y=2 (109bi109)(-10^9 \le b_i \le 10^9).

It is guaranteed that both the sum of nn and the sum of mm over all test cases do not exceed 21052 \cdot 10^5.

Output

For each test case, given that the maximum number of operations is k_maxk\_{\max}, you must output at most two lines:

  • The first line contains the value of k_maxk\_{\max};
  • The second line contains k_maxk\_{\max} integers denoting f(1),f(2),,f(k_max)f(1),f(2),\ldots,f(k\_{\max}). You are allowed to omit this line if k_maxk\_{\max} is 00.

Note that under the constraints of this problem, it can be shown that all values of f(x)f(x) are integers no greater than 101610^{16}.

Example

Input

5
1 3
0
0 1 -1
2 4
0 100
-100 -50 0 50
2 4
0 1000
-100 -50 0 50
6 6
20 1 27 100 43 42
100 84 1 24 22 77
8 2
564040265 -509489796 469913620 198872582 -400714529 553177666 131159391 -20796763
-1000000000 1000000000

Output

1
2
2
150 200
2
1000 200
4
99 198 260 283
2
2000000000 2027422256

Note

On the first test case, there are 1+3=41+3=4 points (0,0),(0,2),(1,2),(1,2)(0,0),(0,2),(1,2),(-1,2).

It can be shown that you cannot perform two or more operations. The value of k_maxk\_{\max} is 11, and you are only asked for the value of f(1)f(1).

You can choose (0,0)(0,0), (1,2)(-1,2), and (1,2)(1,2) as the three vertices of the triangle. After that, your score is increased by the area of the triangle, which is 22. 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 22. Therefore, the value of f(1)f(1) is 22.

On the fifth test case, there are 8+2=108+2=10 points.

It can be shown that you cannot perform three or more operations. The value of k_maxk\_{\max} is 22, and you are asked for the values f(1)f(1) and f(2)f(2).

To maximize the score with only one operation, you can choose three points (198872582,0)(198\,872\,582,0), (1000000000,2)(-1\,000\,000\,000,2), and (1000000000,2)(1\,000\,000\,000,2). 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 20000000002\,000\,000\,000. Therefore, the value of f(1)f(1) is 20000000002\,000\,000\,000.

To maximize the score with exactly two operations, you can choose the following sequence of operations.

  • Choose three points (509489796,0)(-509\,489\,796,0), (553177666,0)(553\,177\,666,0), and (1000000000,2)(-1\,000\,000\,000,2). The three points are erased.
  • Choose three points (400714529,0)(-400\,714\,529,0), (564040265,0)(564\,040\,265,0), and (1000000000,2)(1\,000\,000\,000,2). The three points are erased.

Then, the score after two operations becomes 20274222562\,027\,422\,256. It can be shown that the maximum value of your score after performing exactly two operations is 20274222562\,027\,422\,256. Therefore, the value of f(2)f(2) is 20274222562\,027\,422\,256.

Solution

  1. 要构造满足非共线的三点,需要从 y=0y=0 的点中选 22 个、y=2y=2 的点中选 11 个,或反过来。
  2. 每次选出的一组三点形成的三角形面积即对应行上选出的 22 点的横坐标之差(因为高度为 22面积=12××=×1面积=\frac{1}{2}\times 底\times 高=差\times 1)。
  3. 先对 y=0y=0 的所有点进行排序,计算“前后配对”的最大差值之和形成前缀数组 preApreA;同理对 y=2y=2 的所有点生成前缀数组 preBpreBpreA[i]preA[i] 表示恰好在 y=0y=0 行完成 ii 次“2点配对”操作的总最大面积,同理 preB[i]preB[i] 针对 y=2y=2 行。
  4. 每次总共做 kk 次操作,需要在 y=0y=0 行做 ii 次、在 y=2y=2 行做 (ki)(k - i) 次,要求 2i+(ki)n,i+2(ki)m2i+(k-i) \le n, i+2(k-i) \le m
  5. 用三分搜索或其它方法在可行区间内寻找 ii,使得 preA[i]+preB[ki]preA[i] + preB[k-i] 最大,记为 f(k)f(k)
  6. 最后找出最大可行的 kkk_maxk\_{max},并输出 f(1)f(k_max)f(1) \dots f(k\_{\max})
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;
}
Codeforces Round 1000 (Div. 2) D题题解
https://mizuki.mysqil.com/posts/codeforces-round-1000-div-2-d题题解/
作者
LANSGANBS
发布于
2025-01-22
许可协议
CC BY-NC-SA 4.0
封面
示例歌曲
示例艺术家
封面
示例歌曲
示例艺术家
0:00 / 0:00