CF1858D.Trees and Segments

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The teachers of the Summer Informatics School decided to plant nn trees in a row, and it was decided to plant only oaks and firs. To do this, they made a plan, which can be represented as a binary string ss of length nn. If si=0s_i = 0, then the ii-th tree in the row should be an oak, and if si=1s_i = 1, then the ii-th tree in the row should be a fir.

The day of tree planting is tomorrow, and the day after tomorrow an inspector will come to the School. The inspector loves nature very much, and he will evaluate the beauty of the row as follows:

  • First, he will calculate l0l_0 as the maximum number of consecutive oaks in the row (the maximum substring consisting of zeros in the plan ss). If there are no oaks in the row, then l0=0l_0 = 0.
  • Then, he will calculate l1l_1 as the maximum number of consecutive firs in the row (the maximum substring consisting of ones in the plan ss). If there are no firs in the row, then l1=0l_1 = 0.
  • Finally, he will calculate the beauty of the row as a⋅l0+l1a \cdot l_0 + l_1 for some aa — the inspector's favourite number.

The teachers know the value of the parameter aa, but for security reasons they cannot tell it to you. They only told you that aa is an integer from 11 to nn.

Since the trees have not yet been planted, the teachers decided to change the type of no more than kk trees to the opposite (i.e., change sis_i from 00 to 11 or from 11 to 00 in the plan) in order to maximize the beauty of the row of trees according to the inspector.

For each integer jj from 11 to nn answer the following question independently:

  • What is the maximum beauty of the row of trees that the teachers can achieve by changing the type of no more than kk trees if the inspector's favourite number aa is equal to jj?

暑期信息学学校(SIS)的老师们决定在一排种植 nn 棵树,且只种植橡树和冷杉。为此,他们制定了一份计划,该计划可用一个长度为 nn 的二进制字符串 ss 表示:若 si=0s_i = 0,则第 ii 棵树应为橡树;若 si=1s_i = 1,则第 ii 棵树应为冷杉。

植树日是明天,而大后天将有检查员来校视察。检查员非常热爱自然,他将按如下方式评估这一排树木的“美观度”:

  • 首先,他计算 l0l_0,即该排中连续橡树的最大数目(即计划字符串 ss 中最长的全 00 子串的长度)。若该排中没有橡树,则 l0=0l_0 = 0。
  • 然后,他计算 l1l_1,即该排中连续冷杉的最大数目(即计划字符串 ss 中最长的全 11 子串的长度)。若该排中没有冷杉,则 l1=0l_1 = 0。
  • 最后,他将该排的美观度定义为 a⋅l0+l1a \cdot l_0 + l_1,其中 aa 是检查员最钟爱的一个数。

老师们知道参数 aa 的值,但出于安全原因,他们不能告诉你。他们仅告知你:aa 是介于 11 到 nn(含)之间的整数。

由于树木尚未种植,老师们决定最多将 kk 棵树的种类改为相反类型(即在计划中将 sis_i 由 00 改为 11 或由 11 改为 00),以使该排树木在检查员眼中达到最大美观度。

对每个从 11 到 nn 的整数 jj,请独立回答以下问题:

  • 若检查员最钟爱的数 aa 恰好等于 jj,那么老师们最多更改 kk 棵树的种类所能达到的最大美观度是多少?

输入格式

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

The first line of each test case contains two integers nn and kk (1≤n≤30001 \le n \le 3000, 0≤k≤n0 \le k \le n) — the number of trees in the row and the maximum number of changes.

The second line contains a string ss of length nn, consisting of zeros and ones — the plan description.

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

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

每个测试用例的第一行包含两个整数 nn 和 kk(1≤n≤30001 \le n \le 3000,0≤k≤n0 \le k \le n)—— 行中树的棵数以及最多允许的修改次数。

每个测试用例的第二行包含一个长度为 nn 的字符串 ss,由字符 0 和 1 组成 —— 计划描述。

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

输出格式

For each test case, print nn integers, the jj-th (1≤j≤n1 \le j \le n) of which is the maximum beauty of the row of trees after no more than kk changes if a=ja = j is used to calculate the beauty.

对于每个测试用例,输出 nn 个整数,其中第 jj 个整数(1≤j≤n1 \le j \le n)表示:当使用 a=ja = j 计算美观度时,在最多进行 kk 次修改后,树行所能达到的最大美观度。

输入输出样例

  • 输入#1

    5
    3 0
    111
    4 1
    0110
    5 0
    10000
    6 2
    101101
    7 1
    0001101

    输出#1

    3 3 3 
    4 5 7 9 
    5 9 13 17 21 
    6 9 13 17 21 25 
    7 10 13 17 21 25 29

说明/提示

In the first test case no changes are allowed, so l0=0l_0 = 0 and l1=3l_1 = 3 always hold. Thus, regardless of the value of aa, the beauty of the row of trees will be 33.

In the second test case for a∈1,2a \in {1, 2} the teachers can, for example, change the plan ss to 01110111 (by changing s4s_4), and for a∈3,4a \in {3, 4} — to 00100010 (by changing s2s_2). In this case, the beauty of the row for each aa is calculated as follows:

  • For a=1a = 1: l0=1l_0 = 1, l1=3l_1 = 3. The beauty of the row is 1⋅1+3=41\cdot 1 + 3 = 4.
  • For a=2a = 2: l0=1l_0 = 1, l1=3l_1 = 3. The beauty of the row is 2⋅1+3=52\cdot 1 + 3 = 5.
  • For a=3a = 3: l0=2l_0 = 2, l1=1l_1 = 1. The beauty of the row is 3⋅2+1=73\cdot 2 + 1 = 7.
  • For a=4a = 4: l0=2l_0 = 2, l1=1l_1 = 1. The beauty of the row is 4⋅2+1=94\cdot 2 + 1 = 9.

It can be shown that the changes described above are optimal for all aa from 11 to 44.

在第一个测试用例中,不允许进行任何修改,因此始终有 l0=0l_0 = 0 且 l1=3l_1 = 3。于是,无论 aa 取何值,该排树木的美观度恒为 33。

在第二个测试用例中,当 a∈{1,2}a \in \{1, 2\} 时,教师们可以(例如)将计划字符串 ss 修改为 01110111(通过修改 s4s_4);而当 a∈{3,4}a \in \{3, 4\} 时,则可将其修改为 00100010(通过修改 s2s_2)。此时,对每个 aa 值,该排树木的美观度计算如下:

  • 当 a=1a = 1 时:l0=1l_0 = 1,l1=3l_1 = 3,该排树木的美观度为 1⋅1+3=41\cdot 1 + 3 = 4。
  • 当 a=2a = 2 时:l0=1l_0 = 1,l1=3l_1 = 3,该排树木的美观度为 2⋅1+3=52\cdot 1 + 3 = 5。
  • 当 a=3a = 3 时:l0=2l_0 = 2,l1=1l_1 = 1,该排树木的美观度为 3⋅2+1=73\cdot 2 + 1 = 7。
  • 当 a=4a = 4 时:l0=2l_0 = 2,l1=1l_1 = 1,该排树木的美观度为 4⋅2+1=94\cdot 2 + 1 = 9。

可以证明,上述修改方案对所有 a∈{1,2,3,4}a \in \{1, 2, 3, 4\} 均是最优的。

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

首页