PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GD0000162. Hoán vị huyền bí (Mystic Permutation)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Hoán vị huyền bí (Mystic Permutation)
Mystic Permutation
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Given a permutation of , find the lexicographically smallest permutation satisfying for every index . If none exists, output -1.
Input
The first line contains . The second contains a permutation .
Output
Print -1 if impossible; otherwise print the lexicographically smallest permutation avoiding at every position.
Subtasks
- Subtask 1 (20 points): ; là hoán vị của .
- Subtask 2 (30 points): ; là hoán vị của .
- Subtask 3 (50 points): ; là hoán vị của .
Examples
Input
4
2 3 1 4
Output
1 2 4 3
Explanation
The smallest valid permutation is . Each of its four positions differs from . Keeping the smallest possible prefix , choosing next would force the forbidden value at the last position, so is required.