Minimum Ties (Codeforces 1487C)
Tags: Greedy, dfs and similar

A big football championship will occur soon! $$$n$$$ teams will compete in it, and each pair of teams will play exactly one game against each other.There are two possible outcomes of a game:  the game may result in a tie, then both teams get $$$1$$$ point;  one team might win in a game, then the winning team gets $$$3$$$ points and the losing team gets $$$0$$$ points. The score of a team is the number of points it gained during all games that it played.You are interested in a hypothetical situation when all teams get the same score at the end of the championship. A simple example of that situation is when all games result in ties, but you want to minimize the number of ties as well.Your task is to describe a situation (choose the result of each game) so that all teams get the same score, and the number of ties is the minimum possible.

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases.Then the test cases follow. Each test case is described by one line containing one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the number of teams.

Output: For each test case, print $$$\frac{n(n - 1)}{2}$$$ integers describing the results of the games in the following order: the first integer should correspond to the match between team $$$1$$$ and team $$$2$$$, the second — between team $$$1$$$ and team $$$3$$$, then $$$1$$$ and $$$4$$$, ..., $$$1$$$ and $$$n$$$, $$$2$$$ and $$$3$$$, $$$2$$$ and $$$4$$$, ..., $$$2$$$ and $$$n$$$, and so on, until the game between the team $$$n - 1$$$ and the team $$$n$$$.The integer corresponding to the game between the team $$$x$$$ and the team $$$y$$$ should be $$$1$$$ if $$$x$$$ wins, $$$-1$$$ if $$$y$$$ wins, or $$$0$$$ if the game results in a tie.All teams should get the same score, and the number of ties should be the minimum possible. If there are multiple optimal answers, print any of them. It can be shown that there always exists a way to make all teams have the same score.

Note: In the first test case of the example, both teams get $$$1$$$ point since the game between them is a tie.In the second test case of the example, team $$$1$$$ defeats team $$$2$$$ (team $$$1$$$ gets $$$3$$$ points), team $$$1$$$ loses to team $$$3$$$ (team $$$3$$$ gets $$$3$$$ points), and team $$$2$$$ wins against team $$$3$$$ (team $$$2$$$ gets $$$3$$$ points).

Sample Inputs:
2
2
3

Sample Outputs:
0 
1 -1 1

Solution (Python):
def eqscore(n):
    answer = ''
    if n % 2:
        wins = (n - 1) // 2
        table = ['1'] * wins + ['-1'] * wins
        for _ in range(n - 1):
            answer += ' '.join(table) + ' '
            table.pop()
    else:
        wins = (n - 2) // 2
        table = ['1'] * wins + ['0'] + ['-1'] * wins
        for _ in range(n - 1):
            answer += ' '.join(table) + ' '
            table.pop()
    return answer if answer else 0
if __name__ == '__main__':
    t = int(input())
    while t:
        print(eqscore(int(input())))
        t -= 1

Solution (Python):
t = int(input())
for _ in range(t):
    n = int(input())
    for i in range(1, n):
        if n % 2:
            if i % 2:
                print('1 -1 ' * ((n - i) // 2), end='')
            else:
                print('1 -1 ' * ((n - i - 1) // 2) + '1 ', end='')
        elif i % 2:
            print('0 ' + '1 -1 ' * ((n - i - 1) // 2), end='')
        else:
            print('-1 1 ' * ((n - i) // 2), end='')
    print()

Solution (Python):
def main():
    for _ in range(int(input())):
        n = int(input())
        odd = n % 2
        (le, res) = ((n - 2 + odd) // 2, [])
        ptrn = ['1'] * le
        if not odd:
            ptrn.append('0')
        ptrn += ['-1'] * le
        for _ in range(n - 1):
            res += ptrn
            del ptrn[-1]
        print(' '.join(res))
if __name__ == '__main__':
    main()