1461C - Random Events - CodeForces Solution


dp math probabilities *1500

Please click on ads to support us..

Python Code:

def solve(n, m):
    a = list(map(int, input().split()))
    lastCorrectPos = n - 1
    while lastCorrectPos >= 0 and a[lastCorrectPos] == lastCorrectPos + 1:
        lastCorrectPos -= 1
    
    ans = 1.0
    if lastCorrectPos == -1:
        ans = 0.0
    
    
    for _ in range(m):
        data = input().split()
        r = int(data[0]) - 1
        p = float(data[1])
        
        if r >= lastCorrectPos:
            ans = ans * (1 - p)
        
    
    print("{:.6f}".format(1 - ans))
 
 
t = int(input())
for _ in range(t):
    data = input().split()
    solve(int(data[0]), int(data[1]))
 	 						  			  	  			  			

C++ Code:

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
#define inp(x) freopen(x".inp", "r", stdin);
#define out(x) freopen(x".out", "w", stdout);
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define bit(i,x) (x>>i)&1
#define fou(i, a, b) for(int i=a;i<=b;++i)
#define fod(i, a, b) for(int i=a;i>=b;--i)
const ll e=1e9+7;
const ll inf=1e18;
const int nmax=2e5;
const int base=311;
#define TIME (1.0*clock()/CLOCKS_PER_SEC)




int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
   //   inp("");
   // out("");
    cout<<fixed<<setprecision(6);
    int test;
    cin>>test;
    while(test--){
      int n, m;
      cin>>n>>m;
      int a[n+1];
      fou(i,1,n) cin>>a[i];
      int h=n;
      while(h>=1 && a[h]==h) h--;
      long double ans=h>0;
      while(m--){
        int r; long double p;
        cin>>r>>p;
        if(r>=h) ans*=(1-p);
      } ans=1-ans;
      cout<<ans<<endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array