A16426 | Game with Marbles (Hard Version)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The easy and hard versions of this problem differ only in the constraints on the number of test cases and $n$ . In the hard version, the number of test cases does not exceed $10^4$ , and the sum of values of $n$ over all test cases does not exceed $2 \cdot 10^5$ . Furthermore, there are no additional constraints on $n$ in a single test case.
Recently, Alice and Bob were given marbles of $n$ different colors by their parents. Alice has received $a_1$ marbles of color $1$ , $a_2$ marbles of color $2$ ,..., $a_n$ marbles of color $n$ . Bob has received $b_1$ marbles of color $1$ , $b_2$ marbles of color $2$ , ..., $b_n$ marbles of color $n$ . All $a_i$ and $b_i$ are between $1$ and $10^9$ .
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 $i$ such that both players have at least one marble of that color. The player then discards one marble of color $i$ , and their opponent discards all marbles of color $i$ . The game ends when there is no color $i$ 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 $(A-B)$ , where $A$ is the number of marbles Alice has and $B$ 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.
Recently, Alice and Bob were given marbles of $n$ different colors by their parents. Alice has received $a_1$ marbles of color $1$ , $a_2$ marbles of color $2$ ,..., $a_n$ marbles of color $n$ . Bob has received $b_1$ marbles of color $1$ , $b_2$ marbles of color $2$ , ..., $b_n$ marbles of color $n$ . All $a_i$ and $b_i$ are between $1$ and $10^9$ .
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 $i$ such that both players have at least one marble of that color. The player then discards one marble of color $i$ , and their opponent discards all marbles of color $i$ . The game ends when there is no color $i$ 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 $(A-B)$ , where $A$ is the number of marbles Alice has and $B$ 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.
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of test cases.
Each test case consists of three lines:
- the first line contains a single integer $n$ ( $2 \le n \le 2 \cdot 10^5$ ) — the number of colors;
- the second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 10^9$ ), where $a_i$ is the number of marbles of the $i$ -th color that Alice has;
- the third line contains $n$ integers $b_1, b_2, \dots, b_n$ ( $1 \le b_i \le 10^9$ ), where $b_i$ is the number of marbles of the $i$ -th color that Bob has.
Additional constraint on the input: the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$ .
Each test case consists of three lines:
- the first line contains a single integer $n$ ( $2 \le n \le 2 \cdot 10^5$ ) — the number of colors;
- the second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 10^9$ ), where $a_i$ is the number of marbles of the $i$ -th color that Alice has;
- the third line contains $n$ integers $b_1, b_2, \dots, b_n$ ( $1 \le b_i \le 10^9$ ), where $b_i$ is the number of marbles of the $i$ -th color that Bob has.
Additional constraint on the input: the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$ .
输出格式
For each test case, output a single integer — the score at the end of the game if both Alice and Bob act optimally.
输入输出样例
输入 #1
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
输出 #1
1 -9 2999999997 8 -6
In the first example, one way to achieve a score of $1$ is as follows:
1. Alice chooses color $1$ , discards $1$ marble. Bob also discards $1$ marble;
2. Bob chooses color $3$ , discards $1$ marble. Alice also discards $1$ marble;
3. Alice chooses color $2$ , discards $1$ marble, and Bob discards $2$ marble.
As a result, Alice has $a = [3, 1, 0]$ remaining, and Bob has $b = [0, 0, 3]$ remaining. The score is $3 + 1 - 3 = 1$ .
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 $1$ , then Bob will choose color $4$ , after which Alice will choose color $2$ , and Bob will choose color $3$ . It can be shown that this is the optimal game.
1. Alice chooses color $1$ , discards $1$ marble. Bob also discards $1$ marble;
2. Bob chooses color $3$ , discards $1$ marble. Alice also discards $1$ marble;
3. Alice chooses color $2$ , discards $1$ marble, and Bob discards $2$ marble.
As a result, Alice has $a = [3, 1, 0]$ remaining, and Bob has $b = [0, 0, 3]$ remaining. The score is $3 + 1 - 3 = 1$ .
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 $1$ , then Bob will choose color $4$ , after which Alice will choose color $2$ , and Bob will choose color $3$ . It can be shown that this is the optimal game.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted