Max and Distances
Time limit2sMemory limit1024 MB
Given an n by n distance matrix, restore a sorted point array x and permutations a and b that generate it, or report that none exist.
- Level
Medium7 of 10
- Topics
- Sorting, Greedy, Implementation
- Solved
- No attempts yet
Problem
Max found an array of integer points on a line, sorted in non-decreasing order. Max also has two permutations and of the numbers from to , which were given to him as a birthday present.
Max decided to play with them. He built a distance matrix where . In other words, is the distance between points and .
Before Max could finish playing, Katy cleaned up and , , and were all lost. Max only has the matrix left. He wants to restore a non-decreasing array and two permutations and that generate a matrix equal to . However, Max may have miscalculated , in which case , , and cannot be restored.
Help Max restore them.
Input
The first line contains a natural number (). The next lines contain the matrix (). All numbers in the matrix are integers.
Output
If there exist , , and that generate a matrix equal to , print <<YES>> on the first line. On the second line, print the non-decreasing array of integers (; ). On the third line, print the permutation of the numbers from to . On the fourth line, print the permutation of the numbers from to .
If no such , , and exist, print <<NO>>.
Hint
The input may be large, so it is recommended to use the fast input and output streams of your language.