E2. Game with Marbles (Hard Version)
E2. Game with Marbles (Hard Version)
The easy and hard versions of this problem differ only in the constraints on the number of test cases and . In the hard version, the number of test cases does not exceed , and the sum of values of over all test cases does not exceed . Furthermore, there are no additional constraints on in a single test case.
Recently, Alice and Bob were given marbles of different colors by their parents. Alice has received marbles of color , marbles of color ,..., marbles of color . Bob has received marbles of color , marbles of color , ..., marbles of color . All and are between and .
After some discussion, Alice and Bob came up with the following game: players take turns, starting with Alice. On their turn, a player chooses a color such that both players have at least one marble of that color. The player then discards one marble of color , and their opponent discards all marbles of color . The game ends when there is no color such that both players have at least one marble of that color.
The score in the game is the difference between the number of remaining marbles that Alice has and the number of remaining marbles that Bob has at the end of the game. In other words, the score in the game is equal to , where is the number of marbles Alice has and is the number of marbles Bob has at the end of the game. Alice wants to maximize the score, while Bob wants to minimize it.
Calculate the score at the end of the game if both players play optimally.
Input
The first line contains a single integer () — the number of test cases.
Each test case consists of three lines:
- the first line contains a single integer () — the number of colors;
- the second line contains integers (), where is the number of marbles of the -th color that Alice has;
- the third line contains integers (), where is the number of marbles of the -th color that Bob has.
Additional constraint on the input: the sum of for all test cases does not exceed .
Output
For each test case, output a single integer — the score at the end of the game if both Alice and Bob act optimally.
Example
input
5
3
4 2 1
1 2 4
4
1 20 1 20
100 15 10 20
5
1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1
3
5 6 5
2 1 7
6
3 2 4 2 5 5
9 4 7 9 2 5
output
1
-9
2999999997
8
-6
Note
In the first example, one way to achieve a score of is as follows:
- Alice chooses color , discards marble. Bob also discards marble;
- Bob chooses color , discards marble. Alice also discards marble;
- Alice chooses color , discards marble, and Bob discards marble.
As a result, Alice has remaining, and Bob has remaining. The score is .
It can be shown that neither Alice nor Bob can achieve a better score if both play optimally.
In the second example, Alice can first choose color , then Bob will choose color , after which Alice will choose color , and Bob will choose color . It can be shown that this is the optimal game.
解题思路
在选定一种颜色的石头后,至少有一人该颜色的石头数量变为 ,又因为每次只能选择两人都有的石头颜色,因此在选定一种颜色后,之后该颜色都不会再被选。因此游戏本质上就是轮流选择未选过的颜色。
现在假设一开始 Bob 一个人选择了所有的颜色,那么分数就是 。Alice 先手选择一种颜色,撤销 Bob 对该颜色的选择并变成 Alice 的选择。此时 的变化就是 。轮到 Bob 的回合,只能选择之前未选过的颜色进行保留,此时 不会发生改变,在 Alice 的回合则不能选择该颜色。以此类推,Alice 和 Bob 按照这种规则轮流进行游戏直到选择完 种颜色。
每个人为了取得最优解,对于 Alice 而言,肯定是在之前未选过的颜色中选择值 最大的颜色,来使得 变大。对于 Bob 而言,肯定也是在之前未选过的颜色中选择值 最大的颜色保留,以避免 Alice 选到使得 变大。
因此实际上只需对每种颜色按照 从大到小排序,然后 Alice 选择排序后的第 种颜色,Bob 则选择第 种颜色。
AC 代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int a[N], b[N], p[N];
void solve() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
p[i] = i;
}
LL ret = 0;
for (int i = 0; i < n; i++) {
scanf("%d", b + i);
ret += 1 - b[i];
}
sort(p, p + n, [&](int i, int j) {
return a[i] + b[i] > a[j] + b[j];
});
for (int i = 0; i < n; i += 2) {
ret += a[p[i]] + b[p[i]] - 2;
}
printf("%lld\n", ret);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
通过这题带来的启发是,如果每个元素都有两种状态,其中两种状态的数量都是确实的,此时可以先让所有元素都表示为某一种状态,然后在选择相应数量的元素变成另外一种状态,通过这种思想来分析最优解的选法。
例如老鼠和奶酪这题,可以先把所有的奶酪分给第二只老鼠,分析发现如果将第 个奶酪变成分给第一只老鼠,那么总和的就会加上 。因此将 从大到小排序,选择前 个奶酪分给第一只老鼠剩下的分给第二只老鼠。
还有 D. Maximum Subarray,先把所有元素都减去 ,然后通过 dp 来求得选择哪 个元素加上 能够得到最大子段和。
参考资料
Codeforces Round 916 (Div. 3) Editorial:https://codeforces.com/blog/entry/123530
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17933606.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效