【Derivation】【Mathematical Expectation】【Bubble Sort】Petrozavodsk Winter Training Camp 2018 Day 5: Grand Prix of Korea, Sunday, February 4, 2018 Problem C. Earthquake

The meaning of the question: There are n disjoint paths between the two places. The i-th path consists of a[i] bridges. Each bridge has a probability of damage. The total expected number of times is the smallest.

First of all, it is obvious that when detecting, it is a path-by-path detection, and it is meaningless to skip detection. Considering the order of a certain path that has been arranged, if the two adjacent paths j and j+1 satisfy:

(route[j].A+route[j].B)+(route[j+1].A+route[j+1].B)*(1.0-route[j].c)>
(route[j].A+route[j].B)*(1.0-route[j+1].c)+(route[j+1].A+route[j+1].B)

Just swapping their order makes the answer better.

You can scan n times with a method similar to bubbling.

A is the expected number of checks that all bridges of routej are good, that is, the product of the probabilities that all bridges are good times the number of bridges. B is the expected number of detections when routej is bad, which is equivalent to sorting the damage probability of each bridge from large to small, and then for each bridge k, the preceding k-1 bridges are all good, and its probability of being bad is multiplied by k, then sum this value. c is the product of the probabilities that all bridges are good, that is, the probability that this path is good.

When the answer is finally output, the sum of the probability *(A+B) of all the paths in front of which all paths are bad can be obtained.

#include<cstdio>
#include<algorithm>
using namespace std;
struct data{
	double A,B,c;
}bridge[1005];
int n,x[1005];
int y[1005];
bool cmp(const int &a,const int &b){
	return a>b;
}
int main(){
	//freopen("c.in","r",stdin);
	scanf("%d",&n);
	for(int i=1;i<=n;++i){
		scanf("%d",&x[i]);
		double nowhao=1.0;
		double huaiall=0;
		for(int j=1;j<=x[i];++j){
			scanf("%d",&y[j]);
		}
		sort(y+1,y+x[i]+1);
		for(int j=1;j<=x[i];++j){
			huaiall+=(double)j*nowhao*(1.0-(double)y[j]/1000.0);
			nowhao*=((double)y[j]/1000.0);
		}
		bridge[i].A=nowhao*(double)x[i];
		bridge[i].B=huaiall;
		bridge[i].c=nowhao;
	}
	for(int i=1;i<=n;++i){
		for(int j=1;j<n;++j){
			if((bridge[j].A+bridge[j].B)+(bridge[j+1].A+bridge[j+1].B)*(1.0-bridge[j].c)>
			(bridge[j].A+bridge[j].B)*(1.0-bridge[j+1].c)+(bridge[j+1].A+bridge[j+1].B)){
				swap(bridge[j],bridge[j+1]);
			}
		}
	}
	double years=0;
	double now=1.0;
	double sum=0;
	for(int i=1;i<=n;++i){
		ans+=now*(bridge[i].A+bridge[i].B);
		now*=(1.0-bridge[i].c);
	}
	printf("%.10f\n",ans);
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325148456&siteId=291194637