A function optimization

[17] travel online training

( Http://zhengruioi.com/problem/1192 )
one thing, there are \ (n-\) method can be accomplished, for the first \ (I \) methods have \ (cnt_i \) steps, each step there is a probability of failure \ (P_ {I, J} \) , these steps may be performed in any order, but if one step fails, then this method can not be successful. \ (n \) methods independent of each other independently of each other, and to get this done must be one of the methods in each step must succeed. When you find this thing done, or find it impossible to complete the (attention do not have to finish each step), you can stop. You want to step overtaken the minimum number, find the expected value. \ (n-\ ^ Leq 10. 5, \ sum_ = {I}. 1 ^ n-cnt_i \ Leq. 6 10 ^ \) (title changes after a certain plane)

sol:

First consider if \ (the n-1 = \) , then the answer is. Obviously small \ (D \) should be based on the probability of failure to make inquiries from large to small, because a failure can be stopped, and we have to stop as early as possible, so you should first check a large probability of failure.
May assume \ (p_ {i, j} \) are in descending order sorted. Obviously every time one way to do it will not do half after another, the former because it will not bring any benefits. So we can assume that the order to do that \ (c_1, c_2, · · ·, c_n \) .
So we set \ (E_i \) represents only \ (C_i \) , \ (. 1 + C_ {I}, · · ·, C_N \) required for a desired number of steps, there are
\ (E_i = p_ {c_i, 1} (1 + E_ {i + 1}) + (1-p_ {c_i, 1}) p_ {c_i, 2} (2 + E_ {i + 1}) + (1-p_ {c_i, 1}) (1-p_ {c_i, 2 }) p_ {c_i, 3} (3 + E_ {i + 1}) + · · · + (1-p_ {c_i, 1}) (1-p_ {c_i, 2} ) · · · (p_ {c_i
, cnt_i}) cnt_i \) so we can write it as \ (E_i = k_ {c_i} E_ {i + 1} + b_ {c_i} \) form. Therefore, we have the equivalent of a bunch of functions, we now put them nested together to minimize the final result.
Consider two primary functions \ (y = a_i x + b_i \)And \ (Y = a_j X + b_j \) , if the \ (a_i (a_j X + b_j) + B_i ≤ a_j (a_i X + B_i) + b_j \) , then there is \ (\ frac {a_ {i -1} B_i} ≤ {} \ {J-FRAC. 1 A_ {} {}} b_j \) . Thus according to \ (\ frac {a_i-1 } {b_i} \) sorted can be nested.
But it is worth mentioning that, if there is a way to step impossible, that this method does not need to consider a whole.
Time complexity: \ (O (\ sum_ = {I}. 1 ^ + n-n-cnt_i \ log n-) \) .

#include <bits/stdc++.h>
#define ld long double
using namespace std;
const int N=505;
int n,cnt;ld ans,tmp,st[N];
struct node{ld k,b;} p[N];
inline bool cmp(const node x,const node y){return (x.k-1)/x.b<(y.k-1)/y.b;}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&cnt);tmp=1.0;
        for(int j=1;j<=cnt;j++) scanf("%Lf",&st[j]);
        sort(st+1,st+1+cnt);
        if(st[cnt]>=1-1e-9){i--;n--;continue;}
        reverse(st+1,st+1+cnt);
        while(st[cnt]<=1e-9) cnt--;
        for(int j=1;j<=cnt;j++)
            p[i].k+=st[j]*tmp,p[i].b+=st[j]*tmp*j,tmp*=(1-st[j]);
        p[i].b+=tmp*cnt;
    }
    sort(p+1,p+1+n,cmp);
    for(int i=n;i>0;i--) ans=p[i].k*ans+p[i].b;
    printf("%.10Lf\n",ans);
    return 0;
}


Guess you like

Origin www.cnblogs.com/zxynothing/p/11824370.html