Isolation Centers

 


Problem Link:Codechef

As a health expert, Vinay is keeping a close watch on the ongoing pandemic of coronavirus disease (COVID-19). He thought of a different situation where there are 2626 types of viruses, named "aorona", "borona", "corona", …, "zorona".

You are given a string SS with length NN. There are NN people (numbered 11 through NN) and for each valid ii, the ii-th person is infected by exactly one type of virus named SiSiorona (i.e. "corona" with the first letter replaced by the ii-th character of SS).

You should answer QQ queries. In each query:

·       You are given an integer, CC denoting the number of available isolation centers.

·       Each isolation center has an infinite capacity, but with the restriction that two people infected with the same type of virus cannot stay in the same isolation center.

·       There is also a pending queue with an infinite capacity and there are no restrictions on which people can be in the pending queue.

·       Initially, the isolation centers and pending queue are empty.

·       Each of the NN people should be placed in either the pending queue or one of the isolation centers.

·       Since Vinay is busy finding a vaccine, he asks Swapnil to find a way to place the people in the pending queue and isolation centers such that the number of people in the pending queue are the smallest possible.

·       Help Swapnil finds the size of the pending queue in that case.

Input

·       The first line of the input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.

·       The first line of each test case contains two space-separated integers NN and QQ.

·       The second line contains a single string SS.

·       Each of the following QQ lines contains a single integer CC describing a query.

Output

For each query, print a single line containing one integer ― the minimum size of the pending queue.

Constraints

·       1≤T,N,Q≤1051≤T,N,Q≤105

·       0≤C≤1090≤C≤109

·       |S|=N|S|=N

·       SS contains only lowercase English letters

·       the sum of NN over all test cases does not exceed 105105

·       the sum of QQ over all test cases does not exceed 105105

Subtasks

Subtask #1 (20 points): T,N,Q,C≤10T,N,Q,C≤10

Subtask #2 (80 points): original constraints

Example Input

1

20 2

stayinghomesaveslife

1

3

Example Output

6

0

Explanation

Example case 1: In the pending queue for the first query, there should be 22 people with "eorona", 22 with "sorona", 11 with "aorona" and 11 with "iorona".

 

 

Solution:

t=int(input())

while(t>0):

    t-=1

    n,q=map(int,input().split())

    s=input()

    a=[0]*26

    for i in range(len(s)):

        a[ord(s[i])-97]+=1

    

    while(q>0):

        s=0

        q-=1

        c=int(input())

        for i in range(len(a)):

            if (a[i]>c):

                s+=(a[i]-c)

        print(s)

       

        



Comments

Post a Comment

Popular posts from this blog

Trouble with the Number System

Residue Arithmetic

Perfect Peak of Array