CF2165A.Cyclic Merging
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given n non-negative integers a1,a2,…,an arranged on a ring. For each 1≤i<n, ai and ai+1 are adjacent; a1 and an are adjacent.
You need to perform the following operation exactly n−1 times:
- Choose any pair of adjacent elements on the ring, let their values be x and y, and merge them into a single element of value max(x,y) with cost max(x,y).
Note that this operation will decrease the size of the ring by 1 and update the adjacent relationships accordingly.
Please calculate the minimum total cost to merge the ring into one element.
给你 n 个非负整数 a1,a2,…,an,它们按环形排列。对于每个 1≤i<n,ai 与 ai+1 相邻;a1 与 an 相邻。
你需要恰好执行以下操作 n−1 次:
- 在环上任选一对相邻元素,设其值分别为 x 和 y,将它们合并为一个值为 max(x,y) 的新元素,此次合并的代价为 max(x,y)。
注意:该操作会使环的大小减少 1,并相应地更新相邻关系。
请计算将整个环合并为单个元素所需的最小总代价。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains an integer n (2≤n≤2⋅105).
The following line contains n integers a1,a2,…,an (0≤ai≤109).
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105)。
接下来的一行包含 n 个整数 a1,a2,…,an(0≤ai≤109)。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, please print a single integer — the minimum total cost.
对于每个测试用例,请输出一个整数——最小总成本。
输入输出样例
输入#1
3 4 1 1 3 2 2 0 2 7 1 1 4 5 1 4 1
输出#1
6 2 19
说明/提示
In the first test case, we can achieve a cost of 6 on [1,1,3,2] as follows:
- Merge indexes 1 and 2 with a cost of 1, the ring becomes [1,3,2].
- Merge indexes 1 and 3 with a cost of 2, the ring becomes [3,2].
- Merge indexes 1 and 2 with a cost of 3, the ring becomes [3].
The total cost is 1+2+3=6. It can be proven that it is impossible to achieve a lower cost; thus, the answer is indeed 6.
In the second test case, the only option is to merge the two elements, with a cost of 2.
在第一个测试用例中,我们可以在序列 [1,1,3,2] 上实现总代价为 6,具体步骤如下:
- 合并下标 1 和 2,代价为 1,环变为 [1,3,2];
- 合并下标 1 和 3,代价为 2,环变为 [3,2];
- 合并下标 1 和 2,代价为 3,环变为 [3]。
总代价为 1+2+3=6。可以证明无法获得更低的代价;因此答案确为 6。
在第二个测试用例中,唯一的选择是合并两个元素,代价为 2。
输入解题思路,AI测评打分。不知道怎么写?