CF1779C.Least Prefix Sum

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Baltic, a famous chess player who is also a mathematician, has an array a1,a2,…,ana_1,a_2, \ldots, a_n, and he can perform the following operation several (possibly 00) times:

  • Choose some index ii (1≤i≤n1 \leq i \leq n);
  • multiply aia_i with −1-1, that is, set ai:=−aia_i := -a_i.

Baltic's favorite number is mm, and he wants a1+a2+⋯+ama_1 + a_2 + \cdots + a_m to be the smallest of all non-empty prefix sums. More formally, for each k=1,2,…,nk = 1,2,\ldots, n it should hold that $$a_1 + a_2 + \cdots + a_k \geq a_1 + a_2 + \cdots + a_m.$$

Please note that multiple smallest prefix sums may exist and that it is only required that a1+a2+⋯+ama_1 + a_2 + \cdots + a_m is one of them.

Help Baltic find the minimum number of operations required to make a1+a2+⋯+ama_1 + a_2 + \cdots + a_m the least of all prefix sums. It can be shown that a valid sequence of operations always exists.

著名国际象棋选手兼数学家 Baltic 拥有一个数组 a1,a2,…,ana_1,a_2, \ldots, a_n,他可以执行以下操作若干次(可能为 00 次):

  • 选择某个下标 ii(满足 1≤i≤n1 \leq i \leq n);
  • 将 aia_i 乘以 −1-1,即令 ai:=−aia_i := -a_i。

Baltic 最钟爱的数字是 mm,他希望前 mm 项和 a1+a2+⋯+ama_1 + a_2 + \cdots + a_m 成为所有非空前缀和中最小的一个。更准确地说,对每个 k=1,2,…,nk = 1,2,\ldots, n,均需满足

a_1+a_2+cdots+a_kgeqa_1+a_2+cdots+a_m.a\_1 + a\_2 + \\cdots + a\_k \\geq a\_1 + a\_2 + \\cdots + a\_m.

请注意:可能存在多个相等的最小前缀和,而题目仅要求 a1+a2+⋯+ama_1 + a_2 + \cdots + a_m 是其中之一即可。

请帮助 Baltic 找出使 a1+a2+⋯+ama_1 + a_2 + \cdots + a_m 成为所有前缀和中最小值所需的最少操作次数。可以证明,总存在满足条件的操作序列。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10 0001 \leq t \leq 10\,000). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤m≤n≤2⋅1051 \leq m \leq n \leq 2\cdot 10^5) — the size of Baltic's array and his favorite number.

The second line contains nn integers a1,a2,…,ana_1,a_2, \ldots, a_n (−109≤ai≤109-10^9 \leq a_i \leq 10^9) — the array.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤10 0001 \leq t \leq 10\,000)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤m≤n≤2⋅1051 \leq m \leq n \leq 2\cdot 10^5)—— 分别表示 Baltic 数组的大小及其最喜爱的数字。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2, \ldots, a_n(−109≤ai≤109-10^9 \leq a_i \leq 10^9)—— 即该数组。

保证所有测试用例的 nn 值之和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, print a single integer — the minimum number of required operations.

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

输入输出样例

  • 输入#1

    6
    4 3
    -1 -2 -3 -4
    4 3
    1 2 3 4
    1 1
    1
    5 5
    -2 3 -5 1 -20
    5 2
    -2 3 -5 -5 -20
    10 4
    345875723 -48 384678321 -375635768 -35867853 -35863586 -358683842 -81725678 38576 -357865873

    输出#1

    1
    1
    0
    0
    3
    4

说明/提示

In the first example, we perform the operation a4:=−a4a_4 := -a_4. The array becomes [−1,−2,−3,4][-1,-2,-3,4] and the prefix sums, [a1, a1+a2, a1+a2+a3, a1+a2+a3+a4][a_1, \ a_1+a_2, \ a_1+a_2+a_3, \ a_1+a_2+a_3+a_4], are equal to [−1,−3,−6,−2][-1,-3,-6,-2]. Thus a1+a2+a3=−6a_1 + a_2 + a_3=-6 is the smallest of all prefix sums.

In the second example, we perform the operation a3:=−a3a_3 := -a_3. The array becomes [1,2,−3,4][1,2,-3,4] with prefix sums equal to [1,3,0,4][1,3,0,4].

In the third and fourth examples, a1+a2+⋯+ama_1 + a_2 + \cdots + a_m is already the smallest of the prefix sums — no operation needs to be performed.

In the fifth example, a valid sequence of operations is:

  • a3:=−a3a_3 := -a_3,
  • a2:=−a2a_2 := -a_2,
  • a5:=−a5a_5 := -a_5.

The array becomes [−2,−3,5,−5,20][-2,-3,5,-5,20] and its prefix sums are [−2,−5,0,−5,15][-2,-5,0,-5,15]. Note that a1+a2=−5a_1+a_2=-5 and a1+a2+a3+a4=−5a_1+a_2+a_3+a_4=-5 are both the smallest of the prefix sums (and this is a valid solution).

在第一个例子中,我们执行操作 a4:=−a4a_4 := -a_4。数组变为 [−1,−2,−3,4][-1,-2,-3,4],其前缀和 [a1, a1+a2, a1+a2+a3, a1+a2+a3+a4][a_1, \ a_1+a_2, \ a_1+a_2+a_3, \ a_1+a_2+a_3+a_4] 为 [−1,−3,−6,−2][-1,-3,-6,-2]。因此,a1+a2+a3=−6a_1 + a_2 + a_3=-6 是所有前缀和中的最小值。

在第二个例子中,我们执行操作 a3:=−a3a_3 := -a_3。数组变为 [1,2,−3,4][1,2,-3,4],其前缀和为 [1,3,0,4][1,3,0,4]。

在第三和第四个例子中,a1+a2+⋯+ama_1 + a_2 + \cdots + a_m 已经是所有前缀和中的最小值——无需执行任何操作。

在第五个例子中,一个合法的操作序列为:

  • a3:=−a3a_3 := -a_3,
  • a2:=−a2a_2 := -a_2,
  • a5:=−a5a_5 := -a_5。

数组变为 [−2,−3,5,−5,20][-2,-3,5,-5,20],其前缀和为 [−2,−5,0,−5,15][-2,-5,0,-5,15]。注意,a1+a2=−5a_1+a_2=-5 和 a1+a2+a3+a4=−5a_1+a_2+a_3+a_4=-5 均为前缀和中的最小值(这是一个合法解)。

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

首页