Sorting Numbers with Perfect Squares
Time limit2.5sMemory limit1024 MB
Given N integers up to 10^18, decide whether swapping pairs whose product is a perfect square can sort the array into non-decreasing order.
- Level
Medium6 of 10
- Topics
- Number theory, Sorting, Hash map
- Solved
- No attempts yet
Problem
You are given a sequence of positive integers . You may repeat the following operation as many times as you want.
Choose two indices and with and such that is a perfect square, then swap the values of and .
Determine whether you can repeat the operation as many times as you want to make the sequence non-decreasing, that is, .
Input
The first line contains . ()
The second line contains the integers separated by spaces. ()
Output
If you can sort the sequence into non-decreasing order by repeating the operation as many times as you want, print YES. Otherwise, print NO.