D2. Dances (Hard Version)
D2. Dances (Hard Version)
This is the hard version of the problem. The only difference is that in this version .
You are given two arrays of integers and . Before applying any operations, you can reorder the elements of each array as you wish. Then, in one operation, you will perform both of the following actions, if the arrays are not empty:
- Choose any element from array and remove it (all remaining elements are shifted to a new array ),
- Choose any element from array and remove it (all remaining elements are shifted to a new array ).
Let be the final size of both arrays. You need to find the minimum number of operations required to satisfy for all .
This problem was too easy, so the problem author decided to make it more challenging. You are also given a positive integer . Now, you need to find the sum of answers to the problem for pairs of arrays , where . Array is obtained from as follows:
- ,
- , for .
Input
Each test consists of multiple test cases. The first line contains a single integer () - the number of sets of input data. This is followed by their description.
The first line of each test case contains two integers and (, ) - the size of arrays and and the constraints on the value of element .
The second line of each test case contains integers ().
The third line of each test case contains integers ().
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output the total number of minimum operations for all pairs of arrays .
Example
input
4
2 4
1
3 2
4 7
5 1 5
3 8 3 3
8 4
4 3 3 2 2 1 1
1 1 1 1 3 3 3 3
9 1
9 2 8 3 7 4 6 5
1 2 3 2 1 4 5 6 5
output
2
12
16
4
Note
In the first test case:
- For the pair of arrays , the answer is . No operations or reordering of elements are needed.
- For the pair of arrays , the answer is . The elements of the first array can be rearranged to obtain . No operations are needed.
- For the pair of arrays , the answer is . The element can be removed from the first array and the element can be removed from the second array.
- For the pair of arrays , the answer is . The element can be removed from the first array and the element can be removed from the second array.
解题思路
先解决 D1. Dances (Easy version),此时有 。容易知道答案具有二段性,因此可以二分出最小的删除次数。如果当前要求删除 次,那么应该分别将 和 的哪 个元素删除使得剩余的元素满足 呢?首先将数组 和 从小到大排序,由于最后要满足 ,因此容易贪心地想到 中应该保留尽可能小的数, 中保留尽可能大的数。所以应该删除 的后 个元素,删除 的前 个元素,最后再从左往右依次检查剩余的每对元素是否满足条件即可。时间复杂度为 。
证明比较简单,这里只证 数组, 数组同理。假设某个最优方案中不是删除后 个数,意味着后 个元素中必然有一个没被删除,假设为 ,相对应的,前 个元素中必然有一个被删除,假设为 。由于满足条件,此时有 ,,如果我们改成删除 而保留 ,那么这个条件还是满足的,相当于 之后的元素都右移一个单位与 比较,而这些位置的 都变小了。
考虑 的情况,定义 表示 时最小的删除次数。由上面贪心的思想知道,当 时, 一定是 中最小的。然后题解直接来了句显然存在某个 使得 ,改变 中的一个元素最多会使得答案加 。要是真那么显然比赛时早想出来了...
我按照题解的提示大概推了下,以 , 为例。假设 在变大的过程中没有出现 的情况:
那么就会有 。
而如果 在变大的过程出现某个 ,那么就要进行一次删除操作(删除 中剩余的最大元素, 中的最小元素)。删除一次后必然再次满足条件:
所以必然存在某个 ,满足 都比 多 ,且只能多 。
可以发现 也满足二段性,因此可以二分出来。二分出 后令 ,然后用求 的方法求 即可。最后答案就是 。
这里解释一下为什么二分的边界设为 ,这是因为在 中可能不存在分界点 ,如果不存在的话最后二分的结果是 (否则就是 ),上面计算答案的式子仍然成立。
AC 代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int n, m;
int a[N], b[N], c[N];
bool check(int mid) {
for (int i = 0; i < n - mid; i++) {
if (c[i] >= b[i + mid]) return false;
}
return true;
}
int get(int x) {
memcpy(c, a, n + 10 << 2);
c[0] = x;
sort(c, c + n);
int l = 0, r = n;
while (l < r) {
int mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
return l;
}
void solve() {
scanf("%d %d", &n, &m);
for (int i = 1; i < n; i++) {
scanf("%d", a + i);
}
for (int i = 0; i < n; i++) {
scanf("%d", b + i);
}
a[0] = 1;
sort(a, a + n);
sort(b, b + n);
int ret = get(1);
int l = 1, r = m + 1;
while (l < r) {
int mid = l + r >> 1;
if (get(mid) > ret) r = mid;
else l = mid + 1;
}
printf("%lld\n", (l - 1ll) * ret + (m - l + 1ll) * (ret + 1));
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
参考资料
Codeforces Round #904 (Div. 2) Editorial:https://codeforces.com/blog/entry/121618
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17783795.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2022-10-23 A. Bestie
2022-10-23 三元组