303E - Random Ranking - CodeForces Solution


dp math probabilities *3000

Please click on ads to support us..

C++ Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define db double
const int MAXN=85;
const db eps=1e-8;
int n,m,Len[MAXN],b[MAXN<<1],l[MAXN],r[MAXN];
db Ans[MAXN][MAXN],le[MAXN][MAXN<<1],eq[MAXN][MAXN<<1],mo[MAXN][MAXN<<1],len[MAXN],sum[MAXN][MAXN][MAXN],dp[MAXN][MAXN][MAXN],g[MAXN][MAXN];
db LE(int x,int y){
	if(y<=l[x]) return 0.0;
	if(y>=r[x]) return 1.0;
	return 1.0*(y-l[x])/len[x];
}
void Push(int lev,int wz,int va){
	for(int i=0;i<=Len[lev];i++){
		for(int j=0;i+j<=Len[lev];j++){
			g[i][j]+=dp[lev][i][j]*mo[wz][va+1];
			g[i+1][j]+=dp[lev][i][j]*le[wz][va];
			g[i][j+1]+=dp[lev][i][j]*eq[wz][va];
		}
	}
	Len[lev]++;
	for(int i=0;i<=Len[lev];i++){
		for(int j=0;i+j<=Len[lev];j++){
			dp[lev][i][j]=g[i][j];g[i][j]=0;
		}
	}
	return;
}
void calc(int lev,int L,int R,int va){
	if(L==R){
		for(int i=0;i<=Len[lev];i++){
			for(int j=0;i+j<=Len[lev];j++){
				sum[L][i][j]+=dp[lev][i][j]*eq[L][va];
			}
		}
		return;
	}
	int mid=(L+R)>>1;
	Len[lev+1]=Len[lev];
	for(int i=0;i<=Len[lev];i++){
		for(int j=0;i+j<=Len[lev];j++){
			dp[lev+1][i][j]=dp[lev][i][j];
		}
	}
	for(int i=mid+1;i<=R;i++) Push(lev+1,i,va);
	calc(lev+1,L,mid,va);
	Len[lev+1]=Len[lev];
	for(int i=0;i<=Len[lev];i++){
		for(int j=0;i+j<=Len[lev];j++){
			dp[lev+1][i][j]=dp[lev][i][j];
		}
	}
	for(int i=L;i<=mid;i++) Push(lev+1,i,va);
	calc(lev+1,mid+1,R,va);
	return;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&l[i],&r[i]);len[i]=r[i]-l[i];b[++m]=l[i];b[++m]=r[i];
	}
	sort(b+1,b+1+m);m=unique(b+1,b+1+m)-b-1;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			le[i][j]=LE(i,b[j]);
			if(j<m) eq[i][j]=LE(i,b[j+1])-le[i][j];
			mo[i][j]=1.0-LE(i,b[j]);
//			printf("CAO:%d %d %lf %lf %lf\n",i,j,le[i][j],eq[i][j],mo[i][j]);
		}
	}
	dp[0][0][0]=1;
	for(int now=1;now<m;now++){
		calc(0,1,n,now);
	}
	for(int now=1;now<=n;now++){
		for(int i=0;i<n;i++){
			for(int j=0;i+j<n;j++){
				for(int k=0;k<=j;k++){
					Ans[now][i+k+1]+=sum[now][i][j]/(j+1);
				}
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			printf("%.9lf ",Ans[i][j]);
		}printf("\n");
	}
	return 0;
}
 		  		    	  					 	 	  			  	


Comments

Submit
0 Comments
More Questions

791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II