Explanation
Due to the low bounds, we can use a brute-force approach. There are seven labels , , , , , , and . We need exactly of them, so we can iterate over all combinations of 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 consistent with the mappings. To do this, we track candidate values for , , and using sets. If each of , , and has one consistent value, we record the triple as valid.
We can always deduce a possible value for each of , , and , 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:
Python
import itertoolsfor 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 = -1for 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!