D. Game With Triangles
D. Game With Triangles
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
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 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 .
解题思路
把在 上的点称为点 ,在 上的点称为点 。三角形分为两种,底边要么是两个点 构成的线段,要么是两个点 构成的线段。由于三角形的高都是 ,因此面积就是底边的长度,也即两点构成线段的长度。因此问题转换成了选出的线段总长度最大。
先求解 。设 为以点 为顶点的三角形数量,那么以点 为顶点的三角形数量最多为 。因此 。
考虑如何选择线段。当我们需要从点 中选出 条线段,为了使得总长度最大,一种贪心的选法是让首尾的点构成线段,此时总长度为 。从直观上理解,这种选法使线段尽可能覆盖多的位置,从而长度最长。
假设选出 个三角形的最大线段和为 ,为了快速得到 个三角形的最大线段和,我们可以在 的基础上得到 ,而无需重新求一遍。定义 ,, 表示已从 中选出的线段数量, 表示已从 中选出的线段数量。 的结果有以下三种情况:
- 既可以从 中选择线段,也可以从 中选择线段。此时有 和 ,我们只需根据 和 的大小,选择对应最长的线段即可,有 。
- 只能从 中选择线段。如果 ,此时有 ,那么直接选择 即可,有 。如果 ,此时有 ,那么我们需要将线段 对应的三角形退化掉(因为 说明还能选,我们贪心地删去长度最短的线段),然后选择 和 ,有 和 。
- 只能从 中选择线段。与上面分析类似,如果 ,此时有 ,那么直接选择 即可,有 。如果 ,此时有 ,那么我们需要将线段 对应的三角形退化掉,然后选择 和 ,有 。
将三种情况综合得到的相应代码如下(下标从 开始):
if (2 * i + j == n) s += pb[j++] - pa[--i];
else if (2 * j + i == m) s += pa[i++] - pb[--j];
if (n - 2 * i - j >= 2 && (m - 2 * j - i < 2 || pa[i] > pb[j])) s += pa[i++];
else s += pb[j++];
AC 代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 5;
int a[N], b[N], pa[N], pb[N];
void solve() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
sort(a, a + n);
sort(b, b + m);
for (int i = 0, j = n - 1; i < j; i++, j--) {
pa[i] = a[j] - a[i];
}
for (int i = 0, j = m - 1; i < j; i++, j--) {
pb[i] = b[j] - b[i];
}
int k = 0;
for (int i = 0; i <= n && i <= m >> 1; i++) {
k = max(k, i + min(n - i >> 1, m - 2 * i));
}
cout << k << '\n';
LL i = 0, j = 0, s = 0;
while (k--) {
if (2 * i + j == n) s += pb[j++] - pa[--i];
else if (2 * j + i == m) s += pa[i++] - pb[--j];
if (n - 2 * i - j >= 2 && (m - 2 * j - i < 2 || pa[i] > pb[j])) s += pa[i++];
else s += pb[j++];
cout << s << ' ';
}
cout << '\n';
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
再介绍一下官方题解的做法。分析和上面的基本一样,不同的地方在于每一个 通过三分来独立求解。但我感觉三分的做法并不直观,不太容易往凸函数的方面想。
当要选出 个三角形时,假设从 中选出的线段为 ,从 中选出的线段为 ,对应的约束条件为
可以解得 。然后在这个范围内三分 ,使得式子 的结果最大。该式是一个关于 的凸函数(实际上应该是凹函数,但这里只关注是否具有凹凸性),这是因为 和 都是单调的函数,意味着也是凸函数,而两个凸函数的和也是凸函数。代码实现中是用二分斜率来代替三分。
AC 代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 5;
int a[N], b[N];
LL sa[N], sb[N];
void solve() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
sort(a, a + n);
sort(b, b + m);
for (int i = 0, j = n - 1; i < j; i++, j--) {
sa[i + 1] = sa[i] + a[j] - a[i];
}
for (int i = 0, j = m - 1; i < j; i++, j--) {
sb[i + 1] = sb[i] + b[j] - b[i];
}
int k = 0;
for (int i = 0; i <= n && i <= m >> 1; i++) {
k = max(k, i + min(n - i >> 1, m - 2 * i));
}
cout << k << '\n';
for (int i = 1; i <= k; i++) {
int l = max(0, 2 * i - m), r = min(i, n - i), t = l;
auto get = [&](int x) {
if (x < t) return -1ll;
return sa[x] + sb[i - x];
};
while (l < r) {
int mid = l + r + 1 >> 1;
if (get(mid) - get(mid - 1) >= 0) l = mid;
else r = mid - 1;
}
cout << get(l) << ' ';
}
cout << '\n';
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
参考资料
CF2063:https://www.cnblogs.com/denghaoxuan/articles/18689043
Codeforces Round 1000 (Div. 2) — Editorial:https://codeforces.com/blog/entry/138593
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/18692905
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2024-01-27 F - Nim
2023-01-27 B. GCD Partition
2023-01-27 D. Fixed Prefix Permutations