codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

2244C.md (1714B)


      1 Source: https://codeforces.com/problemset/problem/2244/C
      2 
      3 C. Stepan and Permutation
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Stepan found a permutation $p$ of length $n$. Of course, he decided to sort it. To make the process more interesting, he chose two positive integers $x$ and $y$ $(x + y \le n)$ and defined a rule for swapping elements.
     22 
     23 In one move, Stepan can choose two indices $i$ and $j$ $(1 \le i, j \le n)$ and swap the elements $p_i$ and $p_j$ if at least one of the following conditions holds:
     24 
     25 * $|i - j| = x$
     26 * $|i - j| = y$
     27 
     28 Stepan wants to know whether it is possible to sort the permutation in ascending order using any number of such operations. Help him answer this question.
     29 
     30 Input
     31 
     32 The first line contains a single integer $t$ $(1 \le t \le 10^4)$ — the number of test cases.
     33 
     34 The first line of each test case contains three integers $n$, $x$, and $y$ $(1 \le x, y \le n \le 2 \cdot 10^5$, $x + y \le n)$ — the length of the array and the numbers chosen by Stepan.
     35 
     36 The second line of each test case contains $n$ integers $p_i$ $(1 \le p_i \le n)$ — the array $p$; it is guaranteed that $p$ is a permutation.
     37 
     38 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     39 
     40 Output
     41 
     42 For each test case, output "YES" if it is possible to sort the permutation with the given $x$ and $y$, and "NO" otherwise.
     43 
     44 You may output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted.
     45 
     46 Example
     47 
     48 Input
     49 
     50 ```
     51 4
     52 
     53 5 2 3
     54 
     55 5 4 3 2 1
     56 
     57 6 2 4
     58 
     59 2 1 4 3 6 5
     60 
     61 4 2 2
     62 
     63 1 2 3 4
     64 
     65 5 2 3
     66 
     67 1 2 3 5 4
     68 ```
     69 
     70 Output
     71 
     72 ```
     73 YES
     74 
     75 NO
     76 
     77 YES
     78 
     79 YES
     80 ```