CF2053H.Delicate Anti-monotonous Operations

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

I shall be looking for you who would be out of Existence.

— HyuN, Disorder

There are always many repetitive tasks in life. Iris always dislikes them, so she refuses to repeat them. However, time cannot be turned back; we only have to move forward.

Formally, Iris has an integer sequence a1,a2,…,ana_1, a_2, \ldots, a_n, where each number in the sequence is between 11 and ww, inclusive. It is guaranteed that w≥2w \geq 2.

Iris defines an operation as selecting two numbers ai,ai+1a_i, a_{i+1} satisfying ai=ai+1a_i = a_{i+1}, and then changing them to two arbitrary integers within the range [1,w][1, w]. Iris does not like equality, so she must guarantee that ai≠ai+1a_i \neq a_{i+1} after the operation. Two identical pairs ai,ai+1a_i, a_{i+1} can be selected multiple times.

Iris wants to know the maximum possible sum of all elements of aa after several (possible, zero) operations, as well as the minimum number of operations required to achieve this maximum value.

我将寻找那已不复存在的你。

—— HyuN,《Disorder》(feat. Yuri),K-SOUNDS STUDIO

生活中总有许多重复的任务。Iris 一向讨厌重复,因此她拒绝重复执行它们。然而,时光无法倒流;我们只能向前迈进。

形式化地,Iris 拥有一个整数序列 a1,a2,…,ana_1, a_2, \ldots, a_n,其中序列中每个数均在 [1,w][1, w] 范围内(含端点)。题目保证 w≥2w \geq 2。

Iris 将一次操作定义为:选取两个相邻元素 ai,ai+1a_i, a_{i+1},满足 ai=ai+1a_i = a_{i+1},然后将它们同时替换为 [1,w][1, w] 范围内的任意两个整数。由于 Iris 不喜欢相等,因此操作后必须保证 ai≠ai+1a_i \neq a_{i+1}。相同的相邻对 ai,ai+1a_i, a_{i+1} 可被多次选取。

Iris 想知道:经过若干次(可能为零次)操作后,序列 aa 所有元素之和所能达到的最大值,以及达成该最大值所需的最少操作次数。

输入格式

Each test contains multiple test cases. The first line contains an integer tt (1≤t≤1051 \leq t \leq 10^5) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and ww (1≤n≤2⋅1051 \leq n \leq 2\cdot 10^5, 2≤w≤1082 \leq w \leq 10^8) — the length of the array, and the maximum allowed value of the elements.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤w1 \leq a_i \leq w) — the elements in the array.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1051 \leq t \leq 10^5)—— 测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 ww(1≤n≤2⋅1051 \leq n \leq 2\cdot 10^5,2≤w≤1082 \leq w \leq 10^8)—— 数组的长度以及元素允许的最大值。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤w1 \leq a_i \leq w)—— 数组中的元素。

保证所有测试用例的 nn 之和不超过 10610^6。

输出格式

For each test case, output two integers — the maximum possible sum of all elements of aa and the minimum number of operations required, respectively.

对于每个测试用例,输出两个整数——数组 aa 所有元素之和的最大可能值,以及所需的最少操作次数。

输入输出样例

  • 输入#1

    2
    5 8
    1 2 3 4 5
    7 5
    3 1 2 3 4 1 1

    输出#1

    15 0
    34 6

说明/提示

In the first test case, no operation can be performed so the answers are ∑ai=15\sum a_i = 15 and 00, respectively.

In the second test case, the operations can be performed as follows:

\[3, 1, 2, 3, 4, \\underline{1, 1}\] \\rightarrow \[3, 1, 2, 3, \\underline{4, 4}, 5\] \\rightarrow \[3, 1, 2, \\underline{3, 3}, 5, 5\] \\rightarrow \[3, 1, \\underline{2, 2}, 5, 5, 5\] \\rightarrow \[3, \\underline{1, 1}, 5, 5, 5, 5\] \\rightarrow \[\\underline{3, 3}, 5, 5, 5, 5, 5\] \\rightarrow \[4, 5, 5, 5, 5, 5, 5\]

It can be shown this is optimal, so we should output ∑ai=34\sum a_i = 34 and the number of operations, 66, respectively.

在第一个测试用例中,无法执行任何操作,因此答案分别为 ∑ai=15\sum a_i = 15 和 00。

在第二个测试用例中,操作可按如下方式进行:

\[3, 1, 2, 3, 4, \\underline{1, 1}\] \\rightarrow \[3, 1, 2, 3, \\underline{4, 4}, 5\] \\rightarrow \[3, 1, 2, \\underline{3, 3}, 5, 5\] \\rightarrow \[3, 1, \\underline{2, 2}, 5, 5, 5\] \\rightarrow \[3, \\underline{1, 1}, 5, 5, 5, 5\] \\rightarrow \[\\underline{3, 3}, 5, 5, 5, 5, 5\] \\rightarrow \[4, 5, 5, 5, 5, 5, 5\]

可以证明该方案是最优的,因此我们应分别输出 ∑ai=34\sum a_i = 34 和操作次数 66。

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

首页