#P16728. [GKS 2019 #B] Diverse Subarray

    ID: 19060 远端评测题 5000ms 1024MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP2019线段树Google Kick Start

[GKS 2019 #B] Diverse Subarray

Problem Description

Vanity has NN trinkets on her shelf, numbered 11, 22, ..., NN from left to right. Trinkets come in different types, which are denoted by positive integers. The ii-th trinket on her shelf is of type AiA_i.

She is going to see her family overseas today and would like to bring as many trinkets as she can. However, since she is in a hurry, Vanity must take a consecutive interval of trinkets. Formally, Vanity selects two indices, ll and rr, and takes all of the trinkets numbered ll, l+1l+1, ..., r1r-1, rr. Also, due to tax rules, airport security will throw away all trinkets of a type if Vanity has more than SS of that type in the chosen interval.

For example, suppose that S=2S = 2, and Vanity brings six trinkets: one of type 00, two of type 11, and three of type 22. She will be allowed to keep the trinket of type 00 and both trinkets of type 11, but she will lose all of the trinkets of type 22!

Vanity needs to choose ll and rr such that she can take the maximum number of trinkets for her family. What is the maximum number of trinkets she can bring?

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow. The first line of each test case contains the two integers NN and SS, the number of trinkets, and the maximum number of trinkets allowed of a single type, respectively. The second line contains NN integers. The ii-th integer gives AiA_i, the type of the ii-th trinket.

Output Format

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum number of trinkets that Vanity can bring to her family.

4
6 2
1 1 4 1 4 4
8 1
1 2 500 3 4 500 6 7
10 1
100 200 8 8 8 8 8 300 400 100
12 2
40 50 1 1 1 60 70 2 2 2 80 90
Case #1: 4
Case #2: 6
Case #3: 4
Case #4: 6

Hint

In Sample Case #1, Vanity should choose l=2l = 2 and r=5r = 5. This allows her to take 44 trinkets to the airport of types 11, 44, 11 and 44. None of them are thrown away by airport security, so she is able to bring 44 trinkets to her family.

In Sample Case #2, Vanity should choose l=1l = 1 and r=8r = 8. This allows her to take all 88 trinkets to the airport. Her trinkets of type 500500 are thrown away since she has more than S=1S = 1 of them, so she is able to bring a total of 66 trinkets to her family.

In Sample Case #3, Vanity should choose l=1l = 1 and r=9r = 9. This allows her to take 99 trinkets to the airport of types 100100, 200200, 88, 88, 88, 88, 88, 300300 and 400400. Her trinkets of type 88 are thrown away since she has more than S=1S = 1 of them, so she is able to bring a total of 44 trinkets to her family.

In Sample Case #4, Vanity should choose l=1l = 1 and r=12r = 12. This allows her to take all 1212 trinkets to the airport. Her trinkets of type 11 and 22 are thrown away since she has more than S=2S = 2 of each of them, so she is able to bring a total of 66 trinkets to her family.

Note: We do not recommend using interpreted/slower languages for this problem.

Limits

1T1001 \le T \le 100.

1Ai1051 \le A_i \le 10^5.

1SN1 \le S \le N.

Test set 1 (Visible)

1N10001 \le N \le 1000.

Test set 2 (Hidden)

1N1051 \le N \le 10^5.