tinycodinglm / misc /misc-0362.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame Contribute Delete
3.24 kB
{"type": "session", "harness": "opencode", "id": "misc-0362", "name": "Synthetic 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": "**Problem Description**:\n\nA matrix of size `m x m` is called symmetrical if it remains the same when the rows are reversed. You are given `m^2` integers. Your task is to arrange these `m^2` integers into a matrix such that each integer is used exactly once, and the resulting matrix is symmetrical. If multiple solutions are possible, print any. If it is not possible to form such a matrix, print \"NO\".\n\n**Input Format**:\n\n- The first line contains an integer `m` (1 ≤ m ≤ 20).\n- The second line contains `m^2` integers `b1, b2, ..., bm^2` (1 ≤ bi ≤ 1000).\n\n**Output Format**:\n\n- If it is possible to form a symmetrical matrix, print \"YES\" on the first line, followed by `m` lines, each containing `m` space-separated integers.\n- If it is not possible to form such a matrix, print \"NO\".\n\nExamples\n**Example 1**:\n\nInput: \n```\n4 \n1 2 3 4 1 2 3 4 5 6 7 8 6 5 8 7\n```\n\nOutput: \n```\nYES\n1 2 3 4\n5 6 7 8\n8 7 6 5\n4 3 2 1\n```\n\n**Example 2**:\n\nInput: \n```\n3 \n1 2 3 4 5 6 7 8 9\n```\n\nOutput: \n```\nYES\n1 2 3\n4 5 6\n9 8 7\n```"}}
{"type": "message", "message": {"role": "assistant", "content": "import sys\nfrom collections import Counter, deque\n\ndef main():\n data = sys.stdin.read().split()\n if not data:\n return\n idx = 0\n m = int(data[idx]); idx += 1\n elements = list(map(int, data[idx:idx + m*m])); idx += m*m\n\n freq = Counter(elements)\n valid = True\n if m % 2 == 0:\n for c in freq.values():\n if c % 2 != 0:\n valid = False\n break\n center_val = None\n else:\n odd_vals = [v for v, c in freq.items() if c % 2 != 0]\n if len(odd_vals) != 1:\n valid = False\n center_val = None\n else:\n center_val = odd_vals[0]\n\n if not valid:\n print(\"NO\")\n return\n\n # Build list of paired values\n pairs = []\n for v, c in freq.items():\n pairs.extend([v] * (c // 2))\n # center value already removed from pairs count (its odd leftover)\n # assign symmetric slots\n mat = [[0]*m for _ in range(m)]\n seen = set()\n pi = 0\n for i in range(m):\n for j in range(m):\n if (i, j) in seen:\n continue\n pi2, pj2 = m-1-i, m-1-j\n if (i, j) == (pi2, pj2):\n mat[i][j] = center_val\n seen.add((i, j))\n else:\n v = pairs[pi]; pi += 1\n mat[i][j] = v\n mat[pi2][pj2] = v\n seen.add((i, j))\n seen.add((pi2, pj2))\n\n print(\"YES\")\n for row in mat:\n print(\" \".join(map(str, row)))\n\nif __name__ == \"__main__\":\n main()"}}