Table of Contents

ExplanationImplementation

Official Analysis (C++, Java)

Explanation

Due to the low bounds, we can use a brute-force approach. There are seven labels AA, BB, CC, A+BA + B, B+CB + C, A+CA + C, and A+B+CA + B + C. We need exactly NN of them, so we can iterate over all combinations of NN labels to decide which labels appear in this test case.

For a chosen set of labels, we need to decide which number corresponds to which label. We iterate over all permutations of the chosen labels, mapping them to the input array in order. Finally, we need to deduce the unique triple (A,B,C)(A, B, C) consistent with the mappings. To do this, we track candidate values for AA, BB, and CC using sets. If each of AA, BB, and CC has one consistent value, we record the triple as valid.

We can always deduce a possible value for each of AA, BB, and CC, because each variable appears in four of the seven labels. At most three of these labels can be missing, so we can use the remaining label to determine the variable's value.

Implementation

Time Complexity: O(T)\mathcal{O}(T)

Python

import itertools
for i in range(int(input())):
N = int(input())
arr = [int(x) for x in input().split()]
def deduce_ABC(tags):
a = b = c = ab = bc = ac = abc = -1
for i in range(N):
value = arr[i]

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!