CF1914E1.Game with Marbles (Easy Version)

普及/提高-

通过率:0%

时间限制:3.50s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The easy and hard versions of this problem differ only in the constraints on the number of test cases and nn. In the easy version, the number of test cases does not exceed 10310^3, and nn does not exceed 66.

Recently, Alice and Bob were given marbles of nn different colors by their parents. Alice has received a1a_1 marbles of color 11, a2a_2 marbles of color 22,..., ana_n marbles of color nn. Bob has received b1b_1 marbles of color 11, b2b_2 marbles of color 22, ..., bnb_n marbles of color nn. All aia_i and bib_i are between 11 and 10910^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 ii such that both players have at least one marble of that color. The player then discards one marble of color ii, and their opponent discards all marbles of color ii. The game ends when there is no color ii 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)(A-B), where AA is the number of marbles Alice has and BB 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.

本题的简单版本与困难版本仅在测试用例数量和 nn 的约束上有所不同。在简单版本中,测试用例数量不超过 10310^3,且 nn 不超过 66。

最近,Alice 和 Bob 从父母那里收到了 nn 种不同颜色的弹珠。Alice 收到了颜色 11 的弹珠 a1a_1 颗、颜色 22 的弹珠 a2a_2 颗、……、颜色 nn 的弹珠 ana_n 颗;Bob 收到了颜色 11 的弹珠 b1b_1 颗、颜色 22 的弹珠 b2b_2 颗、……、颜色 nn 的弹珠 bnb_n 颗。所有 aia_i 和 bib_i 均在 11 到 10910^9 之间。

经过一番讨论,Alice 和 Bob 设计了如下游戏:两人轮流进行,Alice 先手。在每一轮中,当前玩家选择一种颜色 ii,要求双方在该颜色上均至少拥有一颗弹珠。然后,当前玩家丢弃一颗颜色 ii 的弹珠,而其对手则丢弃所有颜色 ii 的弹珠。当不存在任何颜色 ii 满足双方在该颜色上均至少拥有一颗弹珠时,游戏结束。

游戏的得分为游戏结束时 Alice 剩余弹珠数与 Bob 剩余弹珠数之差。换言之,游戏得分为 (A−B)(A-B),其中 AA 表示游戏结束时 Alice 拥有的弹珠数,BB 表示 Bob 拥有的弹珠数。Alice 希望最大化该得分,而 Bob 希望最小化该得分。

若双方均采取最优策略,请计算游戏结束时的得分。

输入格式

The first line contains a single integer tt (1≤t≤1031 \le t \le 10^3) — the number of test cases.

Each test case consists of three lines:

  • the first line contains a single integer nn (2≤n≤62 \le n \le 6) — the number of colors;
  • the second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1091 \le a_i \le 10^9), where aia_i is the number of marbles of the ii-th color that Alice has;
  • the third line contains nn integers b1,b2,…,bnb_1, b_2, \dots, b_n (1≤bi≤1091 \le b_i \le 10^9), where bib_i is the number of marbles of the ii-th color that Bob has.

第一行包含一个整数 tt(1≤t≤1031 \le t \le 10^3)—— 测试用例的数量。

每个测试用例由三行组成:

  • 第一行包含一个整数 nn(2≤n≤62 \le n \le 6)—— 颜色的种类数;
  • 第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1091 \le a_i \le 10^9),其中 aia_i 表示 Alice 拥有的第 ii 种颜色的弹珠数量;
  • 第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \dots, b_n(1≤bi≤1091 \le b_i \le 10^9),其中 bib_i 表示 Bob 拥有的第 ii 种颜色的弹珠数量。

输出格式

For each test case, output a single integer — the score at the end of the game if both Alice and Bob act optimally.

对于每个测试用例,输出一个整数——即在 Alice 和 Bob 均采取最优策略的情况下,游戏结束时的得分。

输入输出样例

  • 输入#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 11 is as follows:

  1. Alice chooses color 11, discards 11 marble. Bob also discards 11 marble;
  2. Bob chooses color 33, discards 11 marble. Alice also discards 11 marble;
  3. Alice chooses color 22, discards 11 marble, and Bob discards 22 marble.

As a result, Alice has a=[3,1,0]a = [3, 1, 0] remaining, and Bob has b=[0,0,3]b = [0, 0, 3] remaining. The score is 3+1−3=13 + 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 11, then Bob will choose color 44, after which Alice will choose color 22, and Bob will choose color 33. It can be shown that this is the optimal game.

在第一个例子中,一种得到分数 11 的方法如下:

  1. 爱丽丝选择颜色 11,丢弃 11 颗弹珠;鲍勃也丢弃 11 颗弹珠;
  2. 鲍勃选择颜色 33,丢弃 11 颗弹珠;爱丽丝也丢弃 11 颗弹珠;
  3. 爱丽丝选择颜色 22,丢弃 11 颗弹珠,而鲍勃丢弃 22 颗弹珠。

最终,爱丽丝剩余的弹珠为 a=[3,1,0]a = [3, 1, 0],鲍勃剩余的弹珠为 b=[0,0,3]b = [0, 0, 3]。得分为 3+1−3=13 + 1 - 3 = 1。

可以证明:若双方均采取最优策略,则谁都无法获得更高的分数。

在第二个例子中,爱丽丝可先选择颜色 11,随后鲍勃将选择颜色 44,接着爱丽丝选择颜色 22,鲍勃再选择颜色 33。可以证明这是最优的游戏过程。

输入解题思路,AI测评打分。不知道怎么写?

首页