CF2053I1.Affectionate Arrays (Easy Version)
省选/NOI-
通过率:0%
时间限制:3.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are the beginning of the letter, the development of a poem, and the end of a fairy tale.
— ilem, Pinky Promise
This is the easy version of the problem. The difference between the versions is that in this version, you need to compute the minimum length of the arrays. You can hack only if you solved all versions of this problem.
Iris treasures an integer array a1,a2,…,an. She knows this array has an interesting property: the maximum absolute value of all elements is less than or equal to the sum of all elements, that is, max(∣ai∣)≤∑ai.
Iris defines the boredom of an array as its maximum subarray∗ sum.
Iris's birthday is coming, and Victor is going to send her another array b1,b2,…,bm as a gift. For some seemingly obvious reasons, he decides the array b1,b2,…,bm should have the following properties.
- a1,a2,…,an should be a subsequence† of b1,b2,…,bm.
- The two arrays have the same sum. That is, i=1∑nai=i=1∑mbi.
- The boredom of b1,b2,…,bm is the smallest possible.
- Among the arrays with the smallest boredom, the length of the array b (i.e., m) is the smallest possible. And in this case, Iris will understand his regard as soon as possible!
Even constrained as above, there are still too many possible gifts. So Victor asks you to compute the value of m of any array b1,b2,…,bm satisfying all the conditions above. He promises you: if you help him successfully, he will share a bit of Iris's birthday cake with you.
Note: since the input is large, you may need to optimize it for this problem.
For example, in C++, it is enough to use the following lines at the start of the main() function:
int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);}
∗An array c is a subarray of an array d if c can be obtained from d by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
†A sequence c is a subsequence of a sequence d if c can be obtained from d by the deletion of several (possibly, zero or all) element from arbitrary positions.
你是信件的开头,是诗歌的发展,也是童话的结尾。
— ilem,《粉红约定》(Pinky Promise)
本题为该问题的简单版本。两个版本的区别在于:在本版本中,你只需计算数组的最小可能长度。仅当你已解决该问题的所有版本时,才允许进行 Hack。
Iris 珍藏着一个整数数组 a1,a2,…,an。她知道该数组具有一个有趣的性质:所有元素的最大绝对值不大于所有元素之和,即 max(∣ai∣)≤∑ai。
Iris 将一个数组的无聊度(boredom)定义为它的最大子数组∗和。
Iris 的生日即将到来,Victor 打算送她另一个数组 b1,b2,…,bm 作为礼物。出于某些看似显而易见的原因,他决定数组 b1,b2,…,bm 应满足以下性质:
- a1,a2,…,an 必须是 b1,b2,…,bm 的一个子序列†;
- 两数组的元素之和相等,即 i=1∑nai=i=1∑mbi;
- 数组 b1,b2,…,bm 的无聊度应尽可能小;
- 在所有无聊度最小的数组中,数组 b 的长度(即 m)应尽可能小。此时,Iris 将能尽快领会他的心意!
即便受到上述限制,仍存在太多可能的礼物。因此 Victor 请求你计算任意一个满足上述全部条件的数组 b1,b2,…,bm 对应的 m 值。他向你承诺:只要你成功帮他完成,他将与你分享一小块 Iris 的生日蛋糕。
注意:由于输入规模较大,你可能需要为此题优化输入输出效率。
例如,在 C++ 中,只需在 main() 函数开头加入以下几行即可:
int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);}
∗ 若数组 c 可通过从数组 d 的开头删除若干(可能为零或全部)元素、再从末尾删除若干(可能为零或全部)元素而得到,则称 c 是 d 的一个子数组(subarray)。
† 若序列 c 可通过从序列 d 的任意位置删除若干(可能为零或全部)元素而得到,则称 c 是 d 的一个子序列(subsequence)。
输入格式
Each test contains multiple test cases. The first line of input contains an integer t (1≤t≤105) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n (1≤n≤3⋅106) — the length of the array a1,a2,…,an.
The second line of each test case contains n integers a1,a2,…,an (−109≤ai≤109) — the initial array. It is guaranteed that max(∣ai∣)≤∑ai.
It is guaranteed that the sum of n over all test cases does not exceed 3⋅106.
每个测试包含多个测试用例。输入的第一行包含一个整数 t(1≤t≤105),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤3⋅106),表示数组 a1,a2,…,an 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(−109≤ai≤109),表示初始数组。保证 max(∣ai∣)≤∑ai。
保证所有测试用例的 n 之和不超过 3⋅106。
输出格式
For each test case, output a single line containing an integer: the length m of a valid array b.
对于每个测试用例,输出一行,包含一个整数:有效数组 b 的长度 m。
输入输出样例
输入#1
4 4 1 2 3 4 4 2 -3 2 2 10 2 -7 6 3 -1 4 2 -5 8 -4 20 4 -2 4 3 -2 1 5 2 3 6 -5 -1 -4 -2 -3 5 -3 1 -4 1
输出#1
4 6 14 25
说明/提示
In the first test case, a=[1,2,3,4]. The only array b which satisfies all the properties above is [1,2,3,4], so we should output 4.
In the second test case, a=[2,−3,2,2]. The possible arrays b are [1,2,−3,2,−1,2] and [2,1,−3,2,−1,2], so we should output 6.
在第一个测试用例中,a=[1,2,3,4]。唯一满足上述所有性质的数组 b 是 [1,2,3,4],因此应输出 4。
在第二个测试用例中,a=[2,−3,2,2]。可能的数组 b 有 [1,2,−3,2,−1,2] 和 [2,1,−3,2,−1,2],因此应输出 6。
输入解题思路,AI测评打分。不知道怎么写?