CF1661C.Water the Trees
普及+/提高
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are n trees in a park, numbered from 1 to n. The initial height of the i-th tree is hi.
You want to water these trees, so they all grow to the same height.
The watering process goes as follows. You start watering trees at day 1. During the j-th day you can:
- Choose a tree and water it. If the day is odd (e.g. 1,3,5,7,…), then the height of the tree increases by 1. If the day is even (e.g. 2,4,6,8,…), then the height of the tree increases by 2.
- Or skip a day without watering any tree.
Note that you can't water more than one tree in a day.
Your task is to determine the minimum number of days required to water the trees so they grow to the same height.
You have to answer t independent test cases.
公园里有 n 棵树,编号从 1 到 n。第 i 棵树的初始高度为 hi。
你想给这些树浇水,使它们最终都长到相同的高度。
浇水过程如下:你从第 1 天开始浇水。在第 j 天,你可以执行以下操作之一:
- 选择一棵树并为其浇水。若当天为奇数天(例如 1,3,5,7,…),则该树的高度增加 1;若当天为偶数天(例如 2,4,6,8,…),则该树的高度增加 2。
- 跳过当天,不给任何树浇水。
注意:你每天最多只能给一棵树浇水。
你的任务是确定使所有树达到相同高度所需的最少天数。
你需要回答 t 个相互独立的测试用例。
输入格式
The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases.
The first line of the test case contains one integer n (1≤n≤3⋅105) — the number of trees.
The second line of the test case contains n integers h1,h2,…,hn (1≤hi≤109), where hi is the height of the i-th tree.
It is guaranteed that the sum of n over all test cases does not exceed 3⋅105 (∑n≤3⋅105).
输入的第一行包含一个整数 t(1≤t≤2⋅104)—— 表示测试用例的数量。
每个测试用例的第一行包含一个整数 n(1≤n≤3⋅105)—— 表示树的数量。
每个测试用例的第二行包含 n 个整数 h1,h2,…,hn(1≤hi≤109),其中 hi 表示第 i 棵树的高度。
保证所有测试用例的 n 之和不超过 3⋅105(即 ∑n≤3⋅105)。
输出格式
For each test case, print one integer — the minimum number of days required to water the trees, so they grow to the same height.
对于每个测试用例,输出一个整数——使所有树生长到相同高度所需的最少天数。
输入输出样例
输入#1
3 3 1 2 4 5 4 4 3 5 5 7 2 5 4 8 3 7 4
输出#1
4 3 16
说明/提示
Consider the first test case of the example. The initial state of the trees is [1,2,4].
- During the first day, let's water the first tree, so the sequence of heights becomes [2,2,4];
- during the second day, let's water the second tree, so the sequence of heights becomes [2,4,4];
- let's skip the third day;
- during the fourth day, let's water the first tree, so the sequence of heights becomes [4,4,4].
Thus, the answer is 4.
考虑示例中的第一个测试用例。树木的初始状态为 [1,2,4]。
- 第一天,给第一棵树浇水,高度序列变为 [2,2,4];
- 第二天,给第二棵树浇水,高度序列变为 [2,4,4];
- 跳过第三天;
- 第四天,给第一棵树浇水,高度序列变为 [4,4,4]。
因此,答案为 4。
输入解题思路,AI测评打分。不知道怎么写?