Looking at this problem, I need to place n houses on an n×n grid such that:
1. Each house is in a unique column (distinct x-coordinates)
2. For each wizard i, there exists another house exactly a_i Manhattan distance away
3. a_1 = 0 (wizard 1 can visit their own house)
Key observations:
- Since a_1 = 0, wizard 1 visits their own house.
- For other wizards with a_i > 0, we need some house at exactly that Manhattan distance.
- Each column has at most one house, and we use exactly n columns for n houses.
Strategy: Place wizard 1 at some central position. Then try to place other wizards such that their distance requirement is satisfied (possibly by being at distance a_i from wizard 1 or from another wizard).
Let me think about a construction. Place all houses in a single row (same y-coordinate) except when we need to adjust distances. If houses are at (i, 1) for wizard placed at column i, then distances are just |x_i - x_j|.
Better approach: Place wizard 1 at position (1, 1). For each wizard i with a_i, try to place them so that there's a house at distance a_i. The simplest is to make each wizard i visit wizard 1's house (or some designated house).
Construction: Place wizard 1 at (1,1). For wizard i with distance a_i, place them at a position that is a_i away from wizard 1. The position (1 + dx, 1 + dy) where dx + dy = a_i, dx ≥ 0, dy ≥ 0, and x-coordinates are distinct, 1 ≤ x,y ≤ n.
So for each wizard i (i≥2), we want to place them at column c_i (distinct columns, all different from 1) with some y-coordinate, such that |c_i - 1| + |y_i - 1| = a_i. So y_i = 1 + a_i - (c_i - 1) if c_i > 1 and c_i - 1 ≤ a_i, meaning y_i = 1 + a_i - c_i + 1 = 2 + a_i - c_i.
We need 1 ≤ y_i ≤ n, so 1 ≤ 2 + a_i - c_i ≤ n → c_i ≤ 1 + a_i and c_i ≥ 2 + a_i - n.
For each wizard i, they target wizard 1. We need to assign distinct columns from {1,...,n} where wizard 1 gets column 1, and for each other wizard i, column c_i ∈ [max(2, 2+a_i-n), min(n, 1+a_i)].
This is an assignment problem. Sort wizards by their feasible intervals and greedily assign columns.
```go
package main
import (
"bufio"
"fmt"
"os"
"sort"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscan(reader, &n)
a := make([]int, n)
for i := 0; i < n; i++ {
fmt.Fscan(reader, &a[i])
}
// Wizard 1 (index 0) at column 1, y=1. a[0]=0, visits self.
// For wizard i (i>=1), target wizard 0. Need column c_i != 1, with
// |c_i - 1| + |y_i - 1| = a[i], y_i in [1,n], c_i in [1,n]
// Use c_i >= 1: dist_x = c_i - 1 (if c_i>=1), y_i = 1 + a[i] - (c_i-1) = 2+a[i]-c_i
// Need 1 <= y_i <= n: c_i >= 2+a[i]-n, c_i <= 1+a[i], c_i >= 2 (not 1)
type wiz struct{ idx, lo, hi int }
ws := make([]wiz, 0, n-1)
for i := 1; i < n; i++ {
lo := max(2, 2+a[i]-n)
hi := min(n, 1+a[i])
if lo > hi { fmt.Fprintln(writer, "NO"); return }
ws = append(ws, wiz{i, lo, hi})
}
sort.Slice(ws, func(i, j int) bool { return ws[i].hi < ws[j].hi })
used := make([]bool, n+1)
used[1] = true
col := make([]int, n)
col[0] = 1
// BIT or sorted set for next available
avail := make([]int, n+2)
for i := 1; i <= n; i++ { if i != 1 { avail[i] = i } }
// simple greedy with sorted order
set := make([]int, 0, n-1)
for i := 2; i <= n; i++ { set = append(set, i) }
ptr := 0
for _, w := range ws {
for ptr < len(set) && set[ptr] < w.lo { ptr++ }
if ptr >= len(set) || set[ptr] > w.hi { fmt.Fprintln(writer, "NO"); return }
col[w.idx] = set[ptr]
set = append(set[:ptr], set[ptr+1:]...)
}
fmt.Fprintln(writer, "YES")
for i := 0; i < n; i++ {
y := 2 + a[i] - col[i]
fmt.Fprintln(writer, col[i], y)
}
for i := 0; i < n; i++ {
if i > 0 { fmt.Fprint(writer, " ") }
if a[i] == 0 { fmt.Fprint(writer, i+1) } else { fmt.Fprint(writer, 1) }
}
fmt.Fprintln(writer)
}
func max(a, b int) int { if a > b { return a }; return b }
func min(a, b int) int { if a < b { return a }; return b }
```