Codeforces Round #689 (Div. 2, based on Zed Code Competition) C. Random Events

题目链接:Problem - 1461C - Codeforces

Ron is a happy owner of a permutation aa of length nn.

A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

Ron's permutation is subjected to mm experiments of the following type: (riri, pipi). This means that elements in range [1,ri][1,ri] (in other words, the prefix of length riri) have to be sorted in ascending order with the probability of pipi. All experiments are performed in the same order in which they are specified in the input data.

As an example, let's take a look at a permutation [4,2,1,5,3][4,2,1,5,3] and an experiment (3,0.63,0.6). After such an experiment with the probability of 60%60% the permutation will assume the form [1,2,4,5,3][1,2,4,5,3] and with a 40%40% probability it will remain unchanged.

You have to determine the probability of the permutation becoming completely sorted in ascending order after mm experiments.

Input

Each test contains one or more test cases. The first line contains the number of test cases tt (1≤t≤1001≤t≤100).

The first line of each test case contains two integers nn and mm (1≤n,m≤105)(1≤n,m≤105) — the length of the permutation and the number of experiments, respectively.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n)(1≤ai≤n) — contents of the permutation.

The following mm lines of each test case each contain an integer riri and a real number pipi (1≤ri≤n,0≤pi≤1)(1≤ri≤n,0≤pi≤1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 66 decimal places.

It is guaranteed that the sum of nn and the sum of mm does not exceed 105105 (∑n,∑m≤105∑n,∑m≤105).

Output

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−610−6.

Formally, let your answer be aa, and the jury's answer be bb. Your answer is accepted if and only if |a−b|max(1,|b|)≤10−6|a−b|max(1,|b|)≤10−6.

Example

input

Copy

4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1

output

Copy

0.600000
0.720000
0.989500
1.000000

Note

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)(4,0.6) experiment.

题意:给定一个长度为n的数组,给出q个操作( r , p ), 对[ 1 , r ]区间进行有序排序,排序的概率是 p,不变的概率自然就是 1 − p。问最后数组全部有序的概率是多少。

思路:

直接算全部有序的概率非常复杂,另一个想法就是这么多操作后数组还不是有序的概率是 P PP, 那么最后的答案自然是 1 − P 1 - P1−P。

这是一个结论题,因为每次都是对前缀操作, 那么真正可以影响到整个数组的,其实是 r rr 大于等于数组都最后不有序的位置。

举个例子 n = 5 ,[ 4 , 2 , 1 , 3 , 5 ],先对数组备份排序,得到另一个数组 t[ 1 , 2 , 3 , 4 , 5 ],对比可以看出最后不有序的地方是i = 4,那么对所有r < i的操作都是可以忽略的,因为这些操作不管排序成不成功,都不能改变这个位置,即不能让整个数组有序,所以就会发现对答案无贡献。

#include<bits/stdc++.h>
using namespace std;
int a[100005], b[100005];

int main(){
	ios::sync_with_stdio(false);
	int t;
	int n, m;
	int r;
	double p;
	cin >> t;
	double ans = 1;
	while(t--){
		cin >> n >> m;
		for(int i = 1; i <= n; i++){
			cin >> a[i];
			b[i] = a[i];
		}
		sort(a + 1, a + 1 +n);
		int num = n; 
		while(num > 0 && a[num] == b[num]){
			num--;
		}
		ans = 1;
		for(int i = 0; i < m; i++){
			cin >> r >> p;
			if(r >= num){
				ans *= (1 - p);
			}
		}
		if(num == 0){
			printf("1.000000\n");
		}else{
			printf("%.6f\n", 1 - ans);
		}
		
		
		
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值