#DSU0000038. Sắp xếp bằng phép đổi chỗ có điều kiện (pSort)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sắp xếp bằng phép đổi chỗ có điều kiện (pSort)
pSort
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
There are positions numbered from to . Initially, position contains value , so the starting array is . Every position has a fixed preferred distance .
In one operation, choose positions and swap the values currently stored in them, provided . This condition depends on the position indices and the fixed distance , not on the values currently stored there. Operations may be repeated in any order.
Given a target permutation , determine whether the initial array can be transformed into exactly using only allowed swaps.
Input
The first line contains . The second contains the permutation . The third contains .
Output
Print YES if reachable, otherwise NO.
Subtasks
- Subtask 1 (20 points): ; is a permutation of ; .
- Subtask 2 (30 points): ; is a permutation of ; .
- Subtask 3 (50 points): ; is a permutation of ; .
Examples
Input
5
5 4 3 2 1
1 1 1 1 1
Output
YES
Explanation
Since every equals , the values at any two adjacent positions can be swapped. For example, value can be moved successively left from position to position , and the other values can be arranged similarly. Hence the target permutation is reachable, so print YES.