首页 > 其他分享 >E2. Game with Marbles (Hard Version)

E2. Game with Marbles (Hard Version)

时间:2023-12-28 21:36:07浏览次数:28  
标签:10 marbles color Hard Alice number Game Version Bob

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 $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.

Input

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$.

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 $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.

 

解题思路

  在选定一种颜色的石头后,至少有一人该颜色的石头数量变为 $0$,又因为每次只能选择两人都有的石头颜色,因此在选定一种颜色后,之后该颜色都不会再被选。因此游戏本质上就是轮流选择未选过的颜色。

  现在假设一开始 Bob 一个人选择了所有的颜色,那么分数就是 $S = 0 - \sum\limits_{i=1}^{n}{(b_i - 1)}$。Alice 先手选择一种颜色,撤销 Bob 对该颜色的选择并变成 Alice 的选择。此时 $S$ 的变化就是 $S \gets S + (a_i + b_i - 2)$。轮到 Bob 的回合,只能选择之前未选过的颜色进行保留,此时 $S$ 不会发生改变,在 Alice 的回合则不能选择该颜色。以此类推,Alice 和 Bob 按照这种规则轮流进行游戏直到选择完 $n$ 种颜色。

  每个人为了取得最优解,对于 Alice 而言,肯定是在之前未选过的颜色中选择值 $a_i + b_i$ 最大的颜色,来使得 $S$ 变大。对于 Bob 而言,肯定也是在之前未选过的颜色中选择值 $a_i + b_i$ 最大的颜色保留,以避免 Alice 选到使得 $S$ 变大。

  因此实际上只需对每种颜色按照 $a_i + b_i$ 从大到小排序,然后 Alice 选择排序后的第 $1, \, 3, \, 5, \, \ldots$ 种颜色,Bob 则选择第 $2, \, 4, \, 6, \, \ldots$ 种颜色。

  AC 代码如下,时间复杂度为 $O(n \log{n})$:

#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;
}

  通过这题带来的启发是,如果每个元素都有两种状态,其中两种状态的数量都是确实的,此时可以先让所有元素都表示为某一种状态,然后在选择相应数量的元素变成另外一种状态,通过这种思想来分析最优解的选法。

  例如老鼠和奶酪这题,可以先把所有的奶酪分给第二只老鼠,分析发现如果将第 $i$ 个奶酪变成分给第一只老鼠,那么总和的就会加上 $a_i - b_i$。因此将 $a_i - b_i$ 从大到小排序,选择前 $k$ 个奶酪分给第一只老鼠剩下的分给第二只老鼠。

  还有 D. Maximum Subarray,先把所有元素都减去 $x$,然后通过 dp 来求得选择哪 $k$ 个元素加上 $2x$ 能够得到最大子段和。

 

参考资料

  Codeforces Round 916 (Div. 3) Editorial:https://codeforces.com/blog/entry/123530

标签:10,marbles,color,Hard,Alice,number,Game,Version,Bob
From: https://www.cnblogs.com/onlyblues/p/17933606.html

相关文章

  • shardingSphere-JDBC 多数据源主从+切片配置
    4.x版本配置maven依赖<dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId><version>4.1.1</version></dependency>application配置spri......
  • SpringBoot 整合 ShardingSphere JDBC、MySQL分表实例
    1.概述ShardingSphere分为ShardingSphere-JDBC、ShardingSphere-Proxy、ShardingSphere-Sidecar(TODO)。ShardingSphere官方手册:传送门;这里使用的是ShardingSphere-JDBC,ShardingSphere-JDBC为轻量级Java框架,在Java的JDBC层提供的额外服务。它使用客户端直连数据库,以jar......
  • 解决/lib64/libc.so.6: version `GLIBC_2.14' not found(Linux环境)
    ......
  • Neural Networks for Game AI: A Comprehensive Overview
    1.背景介绍随着计算机游戏的不断发展和进步,游戏人工智能(AI)已经成为游戏开发中的一个重要组成部分。在过去的几十年里,游戏AI的研究和应用已经取得了显著的进展,但仍然面临着许多挑战。这篇文章将深入探讨神经网络在游戏AI中的应用和挑战,并提供一个全面的概述。神经网络是一种模仿生物......
  • D. Yet Another Inversions Problem
    D.YetAnotherInversionsProblemYouaregivenapermutation$p_0,p_1,\ldots,p_{n-1}$ofoddintegersfrom$1$to$2n-1$andapermutation$q_0,q_1,\ldots,q_{k-1}$ofintegersfrom$0$to$k-1$.Anarray$a_0,a_1,\ldots,a_{nk-1}$oflength$n......
  • 记一道攻防世界上的Reverse-gametime
    一、题目描述把文件下载下来,运行后发现是一个简单的小游戏,属于那种玩通关了就给flag的,自己先玩了一下,玩了一下大概游戏规则就是看到s就按空格,遇到x就按x,遇到m就按m,但是玩到后面速度非常快,如果你足够厉害,玩到后面应该是可以玩出来的。这里我们就要IDA进行静态分析,又是小......
  • 使用 PostgreSQL 16.1 + Citus 12.1 作为多个微服务的分布式 Sharding 存储后端
    在本教程中,我们将使用PostgreSQL16.1+Citus12.1作为多个微服务的存储后端,演示此类集群的样例设置和基本操作。Citus12.1实验环境设置Docker快速启动Citus分布式集群docker-compose.ymlversion:"3"services:master:container_name:"${COMPOSE_PROJECT......
  • CodeForces 1906K Deck-Building Game
    洛谷传送门CF传送门UNR#2黎明前的巧克力。枚举两个人选的卡的并集\(S\),那么当\(\bigoplus\limits_{i\inS}a_i=0\)时\(S\)有贡献\(2^{|S|}\)。考虑将\(2^{|S|}\)分摊到每个元素上,也就是每个元素有\(2\)的贡献,然后把这些贡献乘起来。所以题目其实是想让我们算......
  • Windows 10 on ARM, version 22H2 (updated Dec 2023) ARM64 AArch64 中文版、英文版
    Windows10onARM,version22H2(updatedDec2023)ARM64AArch64中文版、英文版下载基于ARM的Windows10请访问原文链接:https://sysin.org/blog/windows-10-arm/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgWindows10,version22H2(releasedNov2021)......
  • Windows 10, version 22H2 (updated Dec 2023) 中文版、英文版下载
    Windows10,version22H2(updatedDec2023)中文版、英文版下载Windows1022H2企业版arm64x64请访问原文链接:https://sysin.org/blog/windows-10/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgWindows10更新历史记录Windows10,version22H2,alledit......