QOJ.ac

QOJ

時間限制: 1.0 s 記憶體限制: 1024 MB 總分: 100 可 Hack ✓

#17818. Increase or Smash

统计

Geumjae 有一个由 $n$ 个零组成的数组 $a$。他的目标是用最少的操作次数将其转换为给定的目标数组。

他可以按任意顺序执行以下两种类型的操作任意次:

  • Increase(增加):选择任意正整数 $x$。将数组 $a$ 的所有元素增加 $x$。换句话说,他选择一个正整数 $x$,对于每个 $i$($1 \le i \le n$),将 $a_i$ 替换为 $a_i + x$。
  • Smash(粉碎):将数组 $a$ 的某些元素(可以是零个、部分或全部)设为 $0$。换句话说,对于每个 $i$($1 \le i \le n$),他要么将 $a_i$ 替换为 $0$,要么保持原样。

给定数组 $a$ 的最终目标状态,求 Geumjae 需要执行的最少总操作次数(包括 IncreaseSmash)。

可以证明,对于任何给定的最终目标数组,总是存在一种操作序列。

输入格式

每个测试点包含多个测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 1000$)。接下来是测试用例的描述。

每个测试用例的第一行包含一个整数 $n$($1 \le n \le 100$)— 数组 $a$ 中的元素个数。

第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$($1 \le a_i \le 100$)— 目标数组 $a$ 的元素。

输出格式

对于每个测试用例,输出一个整数 — 所需的最少操作次数。

样例

输入样例 1

3
3
1 1 3
1
100
9
9 9 3 2 4 4 8 5 3

输出样例 1

3
1
11

说明

第一个测试用例的解释:

目标数组为 $[1, 1, 3]$。一种可行且操作次数最少(共 3 次)的操作序列如下:

  1. 初始时,数组为 $[0, 0, 0]$。执行一次 $x = 2$ 的 Increase 操作后,数组变为 $[2, 2, 2]$。
  2. 接着,对前两个元素执行 Smash 操作,数组变为 $[0, 0, 2]$。
  3. 最后,执行一次 $x = 1$ 的 Increase 操作后,数组变为 $[1, 1, 3]$。

我们共使用了 2 次 Increase 操作和 1 次 Smash 操作,总计 3 次操作。

第二个测试用例的解释:

目标数组为 $[100]$。只需执行一次 $x = 100$ 的 Increase 操作即可得到目标数组。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.