D. Absolute Beauty
D. Absolute Beauty
Kirill has two integer arrays and of length . He defines the absolute beauty of the array as Here, denotes the absolute value of .
Kirill can perform the following operation at most once:
- select two indices and () and swap the values of and .
Help him find the maximum possible absolute beauty of the array after performing at most one swap.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of test cases follows.
The first line of each test case contains a single integer () — the length of the arrays and .
The second line of each test case contains integers () — the array .
The third line of each test case contains integers () — the array .
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output one integer — the maximum possible absolute beauty of the array after no more than one swap.
Example
input
6
3
1 3 5
3 3 3
2
1 2
1 2
2
1 2
2 1
4
1 2 3 4
5 6 7 8
10
1 8 2 5 3 5 3 1 1 3
2 9 2 4 8 2 3 5 3 1
3
47326 6958 358653
3587 35863 59474
output
4
2
2
16
31
419045
Note
In the first test case, each of the possible swaps does not change the array .
In the second test case, the absolute beauty of the array without performing the swap is . After swapping the first and the second element in the array , the absolute beauty becomes . These are all the possible outcomes, hence the answer is .
In the third test case, it is optimal for Kirill to not perform the swap. Similarly to the previous test case, the answer is .
In the fourth test case, no matter what Kirill does, the absolute beauty of remains equal to .
解题思路
nmd 这场前面几题全是思维贪心题,直接爆 0 掉大分。一开始还以为是推式子然后用数据结构维护枚举统计最大值,然后直接司马了。直接抄的官方题解。
将数对 看作数轴上的线段,那么 就是这条线段的长度。交换 和 可以分成以下三种情况(实际上不止,但线段长度的变化可以归为以下 类):
官方题解默认 ,实际上如果 然后互相交换并不会影响答案,带几组数据套上面的图模拟下就知道了。
可以知道只有在第一种情况线段的长度会增加,对此我们希望第一个区间的右端点离第二个区间的左端点尽可能远。因此考虑所有的 和 ,,找到 的最大值,那么答案就是初始时所有线段的总长度加上 。为了快速得到答案,只需求出 的最大值, 的最小值,把差值的两倍加上即可(差值要大于 )。
AC 代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int a[N], b[N];
void solve() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
}
for (int i = 0; i < n; i++) {
scanf("%d", b + i);
}
LL s = 0;
int mx = -2e9, mn = 2e9;
for (int i = 0; i < n; i++) {
if (a[i] > b[i]) swap(a[i], b[i]); // 令a[i]为线段左端点,b[i]为线段右端点
s += b[i] - a[i];
mx = max(mx, a[i]);
mn = min(mn, b[i]);
}
printf("%lld\n", s + max(0, mx - mn << 1));
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
参考资料
Editorial for Codeforces Round #910 (Div. 2):https://codeforces.com/blog/entry/120960
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17847219.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2022-11-21 B. Elimination of a Ring