简单地说下题目
有n个test case,我们已经知道这n个test case的运行时间T[0],T[1],..., T[n-1],以及每个test case成功pass的概率P[0], P[1], ... ,P[n-1];
现在要求设定test case的顺序,使得按该顺序运行这些test case的期望时间最小,
输入:
Line 1: One integer N
Line 2.. N+1 : One integer Ti and one float Pi separated by one space.
输出
Line 1: One float, the minimum expected time
Sample Input
3
3 0.1
7 0.5
9 0.2
Sample Output
4.04
可以用类似冒泡排序的算法做,比较交换两个相邻元素后期望运行时间是否减小,若是则交换,若不是则不交换;
double solve(vector<pair<int,double> > &A)
{
int n=A.size();
if(n==1) return A[0].first;
int T=A[0].first;
double P=1-A[0].second;
double r=T*P;
for(int i=1;i<n;i++){
T+=A[i].first;
P/=(1-A[i-1].second);
P*=(A[i-1].second*(1-A[i].second));
r+=T*P;
}
P = (P/(1-A[n-1].second))*A[n-1].second;
r+=T*P;
for(int i=n;i>1;i--){
T=A[0].first; P=1-A[0].second;
for(int j=1;j<i;j++){
double old = T*P + (T+A[j].first)*(P/(1-A[j-1].second))*A[j-1].second*(1-A[j].second);
double cur = (T-A[j-1].first+A[j].first)*(P/(1-A[j-1].second))*(1-A[j].second)
+(T+A[j].first)*P*A[j].second;
if(cur<old){
swap(A[j],A[j-1]); r = r-old+cur;
T+=A[j-1].first; P*=A[j-1].second;
}
else{ T+=A[j].first; P=(P/(1-A[j-1].second))*A[j-1].second*(1-A[j].second);}
}
}
return r;
}