Beautiful Regional Contest (Codeforces 1264A)
Tags: Greedy, implementation

So the Beautiful Regional Contest (BeRC) has come to an end! $$$n$$$ students took part in the contest. The final standings are already known: the participant in the $$$i$$$-th place solved $$$p_i$$$ problems. Since the participants are primarily sorted by the number of solved problems, then $$$p_1 \ge p_2 \ge \dots \ge p_n$$$.Help the jury distribute the gold, silver and bronze medals. Let their numbers be $$$g$$$, $$$s$$$ and $$$b$$$, respectively. Here is a list of requirements from the rules, which all must be satisfied:  for each of the three types of medals, at least one medal must be awarded (that is, $$$g>0$$$, $$$s>0$$$ and $$$b>0$$$);  the number of gold medals must be strictly less than the number of silver and the number of bronze (that is, $$$g<s$$$ and $$$g<b$$$, but there are no requirements between $$$s$$$ and $$$b$$$);  each gold medalist must solve strictly more problems than any awarded with a silver medal;  each silver medalist must solve strictly more problems than any awarded a bronze medal;  each bronze medalist must solve strictly more problems than any participant not awarded a medal;  the total number of medalists $$$g+s+b$$$ should not exceed half of all participants (for example, if $$$n=21$$$, then you can award a maximum of $$$10$$$ participants, and if $$$n=26$$$, then you can award a maximum of $$$13$$$ participants). The jury wants to reward with medals the total maximal number participants (i.e. to maximize $$$g+s+b$$$) so that all of the items listed above are fulfilled. Help the jury find such a way to award medals.

Input: The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow.The first line of a test case contains an integer $$$n$$$ ($$$1 \le n \le 4\cdot10^5$$$) — the number of BeRC participants. The second line of a test case contains integers $$$p_1, p_2, \dots, p_n$$$ ($$$0 \le p_i \le 10^6$$$), where $$$p_i$$$ is equal to the number of problems solved by the $$$i$$$-th participant from the final standings. The values $$$p_i$$$ are sorted in non-increasing order, i.e. $$$p_1 \ge p_2 \ge \dots \ge p_n$$$.The sum of $$$n$$$ over all test cases in the input does not exceed $$$4\cdot10^5$$$.

Output: Print $$$t$$$ lines, the $$$j$$$-th line should contain the answer to the $$$j$$$-th test case.The answer consists of three non-negative integers $$$g, s, b$$$.  Print $$$g=s=b=0$$$ if there is no way to reward participants with medals so that all requirements from the statement are satisfied at the same time.  Otherwise, print three positive numbers $$$g, s, b$$$ — the possible number of gold, silver and bronze medals, respectively. The sum of $$$g+s+b$$$ should be the maximum possible. If there are several answers, print any of them. 

Note: In the first test case, it is possible to reward $$$1$$$ gold, $$$2$$$ silver and $$$3$$$ bronze medals. In this case, the participant solved $$$5$$$ tasks will be rewarded with the gold medal, participants solved $$$4$$$ tasks will be rewarded with silver medals, participants solved $$$2$$$ or $$$3$$$ tasks will be rewarded with bronze medals. Participants solved exactly $$$1$$$ task won't be rewarded. It's easy to see, that in this case, all conditions are satisfied and it is possible to reward participants in this way. It is impossible to give more than $$$6$$$ medals because the number of medals should not exceed half of the number of participants. The answer $$$1$$$, $$$3$$$, $$$2$$$ is also correct in this test case.In the second and third test cases, it is impossible to reward medals, because at least one medal of each type should be given, but the number of medals should not exceed half of the number of participants.

Sample Inputs:
5
12
5 4 4 3 2 2 1 1 1 1 1 1
4
4 3 2 1
1
1000000
20
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
32
64 64 63 58 58 58 58 58 37 37 37 37 34 34 28 28 28 28 28 28 24 24 19 17 17 17 17 16 16 16 16 11

Sample Outputs:
1 2 3
0 0 0
0 0 0
2 5 3
2 6 6

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
import math

def solve(n, a):
    if n < 10 or len(set(a)) < 4:
        return [0, 0, 0]
    m = {}
    k = n // 2
    for j in range(n // 2, 0, -1):
        if a[j] != a[j - 1]:
            k = j
            break
    for i in range(k):
        if len(m) == 0:
            m[a[i]] = 1 + m.get(a[i], 0)
            g = a[i]
        elif len(m) == 1:
            if a[i] == g:
                m[a[i]] += 1
            else:
                m[a[i]] = 1 + m.get(a[i], 0)
                s = a[i]
                prev = a[i]
        elif len(m) == 2:
            if a[i] == s:
                m[a[i]] += 1
            elif m[s] <= m[g] or prev == a[i]:
                m[s] += 1
                prev = a[i]
            else:
                m[a[i]] = 1 + m.get(a[i], 0)
                mini = a[i]
        else:
            m[mini] += 1
    if len(m) < 3:
        return [0, 0, 0]
    b = sorted(list(m.keys()))
    if m[b[0]] <= m[b[2]] or m[b[1]] <= m[b[2]]:
        return [0, 0, 0]
    res = []
    for i in range(len(b) - 1, -1, -1):
        res.append(m[b[i]])
    return res
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    res2 = solve(n, a)
    print(*res2)

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    l = list(map(int, input().split()))
    l2 = []
    o = 1
    for i in range(n // 2):
        if l[i] == l[i + 1]:
            o += 1
        else:
            l2.append(o)
            o = 1
    q = len(l2)
    (g, s, b) = (0, 0, 0)
    if q > 2:
        g = l2[0]
        i = 1
        while i < q and s <= g:
            s += l2[i]
            i += 1
        for j in range(i, q):
            b += l2[j]
    if g < s and g < b:
        print(g, s, b)
    else:
        print(0, 0, 0)

Solution (Python):
from sys import stdin
a = lambda : stdin.readline().split()
for _ in range(int(stdin.readline())):
    n = int(stdin.readline())
    lst = [*map(int, a())]
    if n < 5:
        print(0, 0, 0)
        continue
    (g, s, b, item) = (0, 0, 0, n // 2)
    for i in range(n // 2 - 1, -1, -1):
        if lst[i] == lst[i + 1]:
            item -= 1
        else:
            break
    (cout, level) = (1, 1)
    for (i, x) in enumerate(lst[1:item]):
        if x == lst[i]:
            cout += 1
        elif level == 1:
            g += cout
            (cout, level) = (1, 2)
        elif level == 2:
            s += cout
            cout = 1
            if s > g:
                level = 3
        else:
            b += cout
            cout = 1
    if level == 3:
        b += cout
        if s > g and b > g:
            if g != 0 and s != 0 and (b != 0):
                print(g, s, b)
            else:
                print(0, 0, 0)
        else:
            print(0, 0, 0)
    else:
        print(0, 0, 0)