CF2248C.Maximize the Score

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an array aa of length 2n2n. Each integer from 11 to nn occurs exactly twice in aa.

Initially, your score is 00.

You can repeatedly perform the following operation while aa is non-empty:

  • Choose an integer xx that is present in aa.
  • Let ll and rr be the indices of the leftmost and rightmost occurrences of xx in the current array, respectively. If xx occurs only once, then l=rl = r.
  • Add (r−l+1)2(r - l + 1)^2 to your score.
  • Delete the elements al,al+1,…,ara_l, a_{l + 1}, \ldots, a_r from aa. The remaining elements are concatenated without changing their order and re-indexed starting from 11.

Find the maximum possible score after making the array empty.

给你一个长度为 2n2n 的数组 aa。其中,从 11 到 nn 的每个整数在 aa 中恰好出现两次。

初始时,你的得分为 00。

当数组 aa 非空时,你可以重复执行以下操作:

  • 选择一个当前存在于 aa 中的整数 xx;
  • 设 ll 和 rr 分别为 xx 在当前数组中最左侧和最右侧出现位置的下标(若 xx 仅出现一次,则 l=rl = r);
  • 将 (r−l+1)2(r - l + 1)^2 加入你的得分;
  • 从 aa 中删除元素 al,al+1,…,ara_l, a_{l + 1}, \ldots, a_r;剩余元素保持原有顺序拼接,并重新从下标 11 开始编号。

求将数组清空后所能获得的最大可能得分。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5).

The second line contains 2n2n integers a1,a2,…,a2na_1, a_2, \ldots, a_{2n} (1≤ai≤n1 \le a_i \le n).

It is guaranteed that each integer from 11 to nn occurs exactly twice in aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)。

第二行包含 2n2n 个整数 a1,a2,…,a2na_1, a_2, \ldots, a_{2n}(1≤ai≤n1 \le a_i \le n)。

保证 aa 中恰好包含 11 到 nn 的每个整数各两次。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single integer — the maximum possible score.

对于每个测试用例,输出一个整数——可能获得的最高分数。

输入输出样例

  • 输入#1

    6
    1
    1 1
    2
    1 2 1 2
    2
    1 2 2 1
    3
    1 1 2 3 3 2
    3
    1 2 3 3 2 1
    4
    1 2 3 4 1 2 3 4

    输出#1

    4
    10
    16
    20
    36
    28

说明/提示

In the second test case, one optimal strategy is to choose x=1x = 1 first. This deletes the subarray [1,2,1][1, 2, 1] and adds 32=93^2 = 9 to the score. The remaining array is [2][2]; choosing x=2x = 2 adds 11. The total score is 1010.

In the third test case, choosing x=1x = 1 deletes the whole array and adds 42=164^2 = 16 to the score.

In the fourth test case, choose x=1x = 1 first and then choose x=2x = 2. The total score is 22+42=202^2 + 4^2 = 20.

In the sixth test case, choose x=2x = 2 first. This deletes the subarray [2,3,4,1,2][2, 3, 4, 1, 2] from the middle of the array and adds 52=255^2 = 25 to the score. After deleting this subarray and concatenating the remaining elements, the array becomes [1,3,4][1, 3, 4]. Choosing each of the three remaining values then adds 11, so the total score is 2828.

在第二个测试用例中,一种最优策略是首先选择 x=1x = 1。这将删除子数组 [1,2,1][1, 2, 1],并向得分中添加 32=93^2 = 9。剩余数组为 [2][2];再选择 x=2x = 2 可额外获得 11 分。总得分为 1010。

在第三个测试用例中,选择 x=1x = 1 将删除整个数组,并向得分中添加 42=164^2 = 16。

在第四个测试用例中,先选择 x=1x = 1,再选择 x=2x = 2。总得分为 22+42=202^2 + 4^2 = 20。

在第六个测试用例中,首先选择 x=2x = 2。这将从数组中间删除子数组 [2,3,4,1,2][2, 3, 4, 1, 2],并向得分中添加 52=255^2 = 25。删除该子数组并将剩余元素拼接后,数组变为 [1,3,4][1, 3, 4]。随后对剩余的三个值分别进行选择,各得 11 分,因此总得分为 2828。

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

首页