tinycodinglm / misc /misc-0362.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame
3.24 kB
opencode trace
System 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...

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.

User

Problem Description:

A 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".

Input Format:

  • The first line contains an integer m (1 ≤ m ≤ 20).
  • The second line contains m^2 integers b1, b2, ..., bm^2 (1 ≤ bi ≤ 1000).

Output Format:

  • 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.
  • If it is not possible to form such a matrix, print "NO".

Examples
Example 1:

Input:

markdown
4  
1 2 3 4 1 2 3 4 5 6 7 8 6 5 8 7

Output:

markdown
YES
1 2 3 4
5 6 7 8
8 7 6 5
4 3 2 1

Example 2:

Input:

markdown
3  
1 2 3 4 5 6 7 8 9

Output:

markdown
YES
1 2 3
4 5 6
9 8 7
Assistant

import sys
from collections import Counter, deque

def main():
data = sys.stdin.read().split()
if not data:
return
idx = 0
m = int(data[idx]); idx += 1
elements = list(map(int, data[idx:idx + mm])); idx += mm

markdown
freq = Counter(elements)
valid = True
if m % 2 == 0:
    for c in freq.values():
        if c % 2 != 0:
            valid = False
            break
    center_val = None
else:
    odd_vals = [v for v, c in freq.items() if c % 2 != 0]
    if len(odd_vals) != 1:
        valid = False
        center_val = None
    else:
        center_val = odd_vals[0]

if not valid:
    print("NO")
    return

# Build list of paired values
pairs = []
for v, c in freq.items():
    pairs.extend([v] * (c // 2))
# center value already removed from pairs count (its odd leftover)
# assign symmetric slots
mat = [[0]*m for _ in range(m)]
seen = set()
pi = 0
for i in range(m):
    for j in range(m):
        if (i, j) in seen:
            continue
        pi2, pj2 = m-1-i, m-1-j
        if (i, j) == (pi2, pj2):
            mat[i][j] = center_val
            seen.add((i, j))
        else:
            v = pairs[pi]; pi += 1
            mat[i][j] = v
            mat[pi2][pj2] = v
            seen.add((i, j))
            seen.add((pi2, pj2))

print("YES")
for row in mat:
    print(" ".join(map(str, row)))

if name == "main":
main()