#BS0000342. Sói tìm kiếm trên hoán vị (Wolf)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sói tìm kiếm trên hoán vị (Wolf)
Wolf
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Given a permutation of , a query runs binary search even though the range may be unsorted. An empty range fails. At midpoint , equality succeeds, a value smaller than k moves right, and a larger value moves left. Before a query you may select d distinct positions anywhere in the permutation, none holding k, and rearrange the selected values. Minimize d so the search succeeds, or report -1. Each query starts with the original permutation.
Input
First line: . Second line: permutation . The next lines each contain .
Output
Print one integer per query, each on a new line.
Subtasks
- Subtask 1 (20 points): ; ; is a permutation of 1 through ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; is a permutation of 1 through ; ; .
Examples
Input 1
7 4
3 1 5 2 7 6 4
3 4 2
2 3 5
1 5 6
1 7 3
Output 1
2
0
-1
4
Explanation
Query 1 swaps positions 2 and 3. Query 2 already succeeds. Query 3 excludes the position of 6. Query 4 requires larger values at positions 4 and 2, both initially too small; two donor positions are also necessary.