File size: 4,111 Bytes
d517dc9
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "misc-0026", "name": "Competitive Programming"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code. When solving problems, explain your reasoning step by step before providing the final solution."}}
{"type": "message", "message": {"role": "user", "content": "Chef has a cubic die with 6 faces kept on an infinite plane. Each face has a distinct integer in the range [1,6] written on it, but the exact arrangement of the numbers on the faces of the die is unknown to Chef. Curiosity gets the better of Chef and he wants to find out o(1), o(2), ..., o(6), where o(i) is the number written opposite to the number i.\n\n\tChef performs the following N-1 steps to learn the exact arrangement of the numbers on the die. In the i-th step, Chef pushes the die in some direction (there are 4 possible directions), and the die rolls 90o in this direction. The picture below demonstrates a die and the result that it produced after rolling in each of the 4 directions respectively. For this die, we have o(1)=4, o(2)=5, o(3)=6, o(4)=1, o(5)=2, o(6)=3.\n\n\tChef records N numbers A1, A2, ..., AN, where Ai is the number written on the top of the die before the i-th step. However, the information on the direction in which he pushes the die each time are lost. Can you help him determine the arrangement of 1-6? You need to output o(1), o(2), ..., o(6). If there are multiple solutions, output any one of them.\n\n-----\n\tInput-----\n- The first line of input contains a single integer T, the number of test cases.\n- Each test case consists of two lines.\n- The first line contains an integer N, and the second line contains N space-separated integers A1, A2, ..., AN.\n\n-----\n\tOutput-----\n\n\tFor each test case, output one line. If a possible solution exists, then output 6 space-separated integers o(1), o(2), ..., o(6); otherwise, output -1.\n\n-----\n\tConstraints-----\n\n- 1 ≤ T, N ≤ 500\n- 1 ≤ Ai ≤ 6\n\n\tSubtask #1 (17 points):\n\n- N ≤ 2\n\n\tSubtask #2 (83 points):\n\n- original constraints\n\n-----\n\tExample-----\nInput:\n4\n9\n1 3 4 6 1 3 4 6 1\n10\n1 2 1 3 1 4 1 5 1 6\n6\n1 2 3 4 5 6\n4\n1 3 3 1\n\nOutput:\n2 1 6 5 4 3\n-1\n4 5 6 1 2 3\n-1\n\n-----\n\tExplanation-----\nExample case 1: See below.\n\nExample case 2: You can prove that there's no solution.\nExample case 3: The solution is not unique. 3 5 1 6 2 4 is another valid solution."}}
{"type": "message", "message": {"role": "assistant", "content": "import sys\n\ndef main():\n    data = sys.stdin.read().split()\n    it = iter(data)\n    t = int(next(it))\n    out = []\n    for _ in range(t):\n        n = int(next(it))\n        a = [int(next(it)) for _ in range(n)]\n        forbidden = set()\n        possible = True\n        for i in range(n - 1):\n            x = a[i] - 1\n            y = a[i + 1] - 1\n            if x == y:\n                possible = False\n                break\n            forbidden.add((x, y))\n            forbidden.add((y, x))\n        if not possible:\n            out.append(\"-1\")\n            continue\n        opposite = [-1] * 6\n\n        def dfs(mask):\n            if mask == 63:\n                return True\n            i = 0\n            while mask & (1 << i):\n                i += 1\n            for j in range(i + 1, 6):\n                if mask & (1 << j):\n                    continue\n                if (i, j) in forbidden:\n                    continue\n                opposite[i] = j + 1\n                opposite[j] = i + 1\n                if dfs(mask | (1 << i) | (1 << j)):\n                    return True\n                opposite[i] = -1\n                opposite[j] = -1\n            return False\n\n        if dfs(0):\n            out.append(\" \".join(str(opposite[x]) for x in range(6)))\n        else:\n            out.append(\"-1\")\n    sys.stdout.write(\"\\n\".join(out) + (\"\\n\" if out else \"\"))\n\nif __name__ == \"__main__\":\n    main()"}}