CF1741C.Minimize the Thickness

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a sequence a=[a1,a2,…,an]a=[a_1,a_2,\dots,a_n] consisting of nn positive integers.

Let's call a group of consecutive elements a segment. Each segment is characterized by two indices: the index of its left end and the index of its right end. Denote by a[l,r]a[l,r] a segment of the sequence aa with the left end in ll and the right end in rr, i.e. a[l,r]=[al,al+1,…,ar]a[l,r]=[a_l, a_{l+1}, \dots, a_r].

For example, if a=[31,4,15,92,6,5]a=[31,4,15,92,6,5], then a[2,5]=[4,15,92,6]a[2,5]=[4,15,92,6], a[5,5]=[6]a[5,5]=[6], a[1,6]=[31,4,15,92,6,5]a[1,6]=[31,4,15,92,6,5] are segments.

We split the given sequence aa into segments so that:

  • each element is in exactly one segment;
  • the sums of elements for all segments are equal.

For example, if aa = [55,45,30,30,40,10055,45,30,30,40,100], then such a sequence can be split into three segments: a[1,2]=[55,45]a[1,2]=[55,45], a[3,5]=[30,30,40]a[3,5]=[30, 30, 40], a[6,6]=[100]a[6,6]=[100]. Each element belongs to exactly segment, the sum of the elements of each segment is 100100.

Let's define thickness of split as the length of the longest segment. For example, the thickness of the split from the example above is 33.

Find the minimum thickness among all possible splits of the given sequence of aa into segments in the required way.

给你一个由 nn 个正整数组成的序列 a=[a1,a2,…,an]a=[a_1,a_2,\dots,a_n]。

我们将连续元素组成的子序列称为段(segment)。每个段由两个下标刻画:左端点下标与右端点下标。记 a[l,r]a[l,r] 为序列 aa 中左端点为 ll、右端点为 rr 的段,即 a[l,r]=[al,al+1,…,ar]a[l,r]=[a_l, a_{l+1}, \dots, a_r]。

例如,若 a=[31,4,15,92,6,5]a=[31,4,15,92,6,5],则 a[2,5]=[4,15,92,6]a[2,5]=[4,15,92,6],a[5,5]=[6]a[5,5]=[6],a[1,6]=[31,4,15,92,6,5]a[1,6]=[31,4,15,92,6,5] 均为段。

我们需将给定序列 aa 划分为若干段,使得:

  • 每个元素恰好属于一个段;
  • 所有段的元素之和均相等。

例如,若 a=[55,45,30,30,40,100]a = [55,45,30,30,40,100],则该序列可划分为三个段:a[1,2]=[55,45]a[1,2]=[55,45],a[3,5]=[30,30,40]a[3,5]=[30, 30, 40],a[6,6]=[100]a[6,6]=[100]。每个元素恰好属于一个段,且每段元素之和均为 100100。

我们定义该划分的**厚度(thickness)**为所有段中最长段的长度。例如,上述例子中划分的厚度为 33。

请找出在满足上述条件的所有可能划分中,最小的厚度值。

输入格式

The first line contains a single integer tt (1≤t≤1001 \le t \le 100) — the number of test cases.

Each test case is described by two lines.

The first line of each test case contains a single integer nn (1≤n≤20001 \le n \le 2000) — the length of the sequence aa.

The second line of each test case contains exactly nn integers: a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1061 \le a_i \le 10^6) — elements of the sequence aa.

It is guaranteed that the sum of nn for all test cases does not exceed 20002000.

第一行包含一个整数 tt(1≤t≤1001 \le t \le 100)—— 测试用例的数量。

每个测试用例由两行描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤20001 \le n \le 2000)—— 序列 aa 的长度。

每个测试用例的第二行包含恰好 nn 个整数:a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1061 \le a_i \le 10^6)—— 序列 aa 的元素。

保证所有测试用例的 nn 之和不超过 20002000。

输出格式

For each test case, output one integer — the minimum possible thickness of a split of the sequence aa into segments.

Note that there always exist a split, you can always consider whole sequence as one segment.

对于每个测试用例,输出一个整数——将序列 aa 分割为若干段时可能的最小厚度。

注意:这样的分割总是存在的,你可以始终将整个序列视为一段。

输入输出样例

  • 输入#1

    4
    6
    55 45 30 30 40 100
    4
    10 23 7 13
    5
    10 55 35 30 65
    6
    4 1 1 1 1 4

    输出#1

    3
    4
    2
    3

说明/提示

The split in the first test case is explained in the statement, it can be shown that it is optimal.

In the second test case, it is possible to split into segments only by leaving a single segment. Then the thickness of this split is equal to the length of the entire sequence, that is, 44.

In the third test case, the optimal split will be [10,55],[35,30],[65][10, 55], [35, 30], [65]. The thickness of the split equals to 22.

In the fourth test case possible splits are:

  • [4]+[1,1,1,1]+[4][4] + [1, 1, 1, 1] + [4];
  • [4,1,1]+[1,1,4][4, 1, 1] + [1, 1, 4].

第一个测试用例中的分割方式在题面中已作解释,可以证明该分割是最优的。

第二个测试用例中,仅能将序列划分为一个单独的段。此时该分割的厚度等于整个序列的长度,即 44。

第三个测试用例中,最优分割为 [10,55],[35,30],[65][10, 55], [35, 30], [65],该分割的厚度为 22。

第四个测试用例中,可能的分割方式有:

  • [4]+[1,1,1,1]+[4][4] + [1, 1, 1, 1] + [4];
  • [4,1,1]+[1,1,4][4, 1, 1] + [1, 1, 4]。

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

首页