codeforces-scrape

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

1607D.md (2870B)


      1 Source: https://codeforces.com/problemset/problem/1607/D
      2 
      3 D. Blue-Red Permutation
      4 
      5 time limit per test
      6 
      7 1 second
      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 You are given an array of integers $a$ of length $n$. The elements of the array can be either different or the same.
     22 
     23 Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step:
     24 
     25 * either you can select any blue element and decrease its value by $1$;
     26 * or you can select any red element and increase its value by $1$.
     27 
     28 Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.
     29 
     30 Determine whether it is possible to make $0$ or more steps such that the resulting array is a permutation of numbers from $1$ to $n$?
     31 
     32 In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $a$ contains in some order all numbers from $1$ to $n$ (inclusive), each exactly once.
     33 
     34 Input
     35 
     36 The first line contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of input data sets in the test.
     37 
     38 The description of each set of input data consists of three lines. The first line contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the original array $a$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($-10^9 \leq a_i \leq 10^9$) — the array elements themselves.
     39 
     40 The third line has length $n$ and consists exclusively of the letters 'B' and/or 'R': $i$th character is 'B' if $a_i$ is colored blue, and is 'R' if colored red.
     41 
     42 It is guaranteed that the sum of $n$ over all input sets does not exceed $2 \cdot 10^5$.
     43 
     44 Output
     45 
     46 Print $t$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise.
     47 
     48 You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
     49 
     50 Example
     51 
     52 Input
     53 
     54 ```
     55 8
     56 4
     57 1 2 5 2
     58 BRBR
     59 2
     60 1 1
     61 BB
     62 5
     63 3 1 4 2 5
     64 RBRRB
     65 5
     66 3 1 3 1 3
     67 RBRRB
     68 5
     69 5 1 5 1 5
     70 RBRRB
     71 4
     72 2 2 2 2
     73 BRBR
     74 2
     75 1 -2
     76 BR
     77 4
     78 -2 -1 4 0
     79 RRRR
     80 ```
     81 
     82 Output
     83 
     84 ```
     85 YES
     86 NO
     87 YES
     88 YES
     89 NO
     90 YES
     91 YES
     92 YES
     93 ```
     94 
     95 Note
     96 
     97 In the first test case of the example, the following sequence of moves can be performed:
     98 
     99 * choose $i=3$, element $a_3=5$ is blue, so we decrease it, we get $a=[1,2,4,2]$;
    100 * choose $i=2$, element $a_2=2$ is red, so we increase it, we get $a=[1,3,4,2]$;
    101 * choose $i=3$, element $a_3=4$ is blue, so we decrease it, we get $a=[1,3,3,2]$;
    102 * choose $i=2$, element $a_2=2$ is red, so we increase it, we get $a=[1,4,3,2]$.
    103 
    104 We got that $a$ is a permutation. Hence the answer is YES.