1461C.md (2983B)
1 Source: https://codeforces.com/problemset/problem/1461/C 2 3 C. Random Events 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 Ron is a happy owner of a permutation $a$ of length $n$. 22 23 A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array). 24 25  26 27 Ron's permutation is subjected to $m$ experiments of the following type: ($r_i$, $p_i$). This means that elements in range $[1, r_i]$ (in other words, the prefix of length $r_i$) have to be sorted in ascending order with the probability of $p_i$. All experiments are performed in the same order in which they are specified in the input data. 28 29 As an example, let's take a look at a permutation $[4, 2, 1, 5, 3]$ and an experiment ($3, 0.6$). After such an experiment with the probability of $60\%$ the permutation will assume the form $[1, 2, 4, 5, 3]$ and with a $40\%$ probability it will remain unchanged. 30 31 You have to determine the probability of the permutation becoming completely sorted in ascending order after $m$ experiments. 32 33 Input 34 35 Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). 36 37 The first line of each test case contains two integers $n$ and $m$ $(1 \le n, m \le 10^5)$ — the length of the permutation and the number of experiments, respectively. 38 39 The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ $(1 \le a_i \le n)$ — contents of the permutation. 40 41 The following $m$ lines of each test case each contain an integer $r_i$ and a real number $p_i$ $(1 \le r_i \le n, 0 \le p_i \le 1)$ — the length of the prefix and the probability of it being sorted. All probabilities are given with at most $6$ decimal places. 42 43 It is guaranteed that the sum of $n$ and the sum of $m$ does not exceed $10^5$ ($\sum n, \sum m \le 10^5$). 44 45 Output 46 47 For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed $10^{-6}$. 48 49 Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$. 50 51 Example 52 53 Input 54 55 ``` 56 4 57 4 3 58 4 3 2 1 59 1 0.3 60 3 1 61 4 0.6 62 5 3 63 4 2 1 3 5 64 3 0.8 65 4 0.6 66 5 0.3 67 6 5 68 1 3 2 4 5 6 69 4 0.9 70 5 0.3 71 2 0.4 72 6 0.7 73 3 0.5 74 4 2 75 1 2 3 4 76 2 0.5 77 4 0.1 78 ``` 79 80 Output 81 82 ``` 83 0.600000 84 0.720000 85 0.989500 86 1.000000 87 ``` 88 89 Note 90 91 Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the $(4, 0.6)$ experiment.