Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<algorithm>
  4. #include<set>
  5. #include<map>
  6. #include<queue>
  7. #include<stack>
  8. #include<deque>
  9. #include<vector>
  10. #include<string>
  11. #include<math.h>
  12. using namespace std;
  13.  
  14. #define INF 1000000000
  15. #define lint long long
  16. #define pb push_back
  17. #define mp make_pair
  18. #define MOD 1000000007
  19.  
  20. lint a[1005][2006];
  21. long double d[1005][2005];
  22. long double p[1005][2005];
  23. int main()
  24. {
  25.     int tc;
  26.     scanf("%d",&tc);
  27.     while(tc--)
  28.     {
  29.         int n,i,j;
  30.         scanf("%d",&n);
  31.         for(i=1;i<=n;++i)
  32.         {
  33.             for(j=1;j<=2*i;++j)
  34.             {
  35.                 scanf("%lld",&a[i][j]);
  36.             }
  37.         }
  38.         for(i=1;i<=n+1;++i)
  39.         {
  40.             for(j=1;j<=i;++j)
  41.             {
  42.                 d[i][j]=0;
  43.                 p[i][j]=0;
  44.             }
  45.         }
  46.         d[1][1]=0;
  47.         p[1][1]=1;
  48.         for(i=1;i<=n;++i)
  49.         {
  50.             for(j=1;j<=i;++j)
  51.             {
  52.                 //cout<<i<<" x "<<j<<endl;
  53.                 //cout<<d[i][j]<<" "<<p[i][j]<<endl;
  54.  
  55.                 int len1, len2;
  56.                 len1=a[i][2*j-1];
  57.                 len2=a[i][2*j];
  58.                 double p1;
  59.                 double p2;
  60.  
  61.                 if(len1==len2)
  62.                 {
  63.                     p1=0.5;
  64.                     p2=0.5;
  65.                 }
  66.                 else if(len1<len2)
  67.                 {
  68.                     p1=1;
  69.                     p2=0;
  70.                 }
  71.                 else
  72.                 {
  73.                     p1=0;
  74.                     p2=1;
  75.                 }
  76.                 //cout <<p1<<" "<<p2<<endl;
  77.                 //cout<<len1<<" "<<len2<<endl;
  78.  
  79.                 d[i+1][j]+=p1*(p[i][j]*len1+d[i][j]);
  80.                 d[i+1][j+1]+=p2*(p[i][j]*len2+d[i][j]);
  81.                 p[i+1][j]+=p1*p[i][j];
  82.                 p[i+1][j+1]+=p2*p[i][j];
  83.                 //cout<<i+1<<" "<<j<<" "<<d[i+1][j]<<"   ";
  84.                 //cout<<i+1<<" "<<j+1<<" "<<d[i+1][j+1]<<endl;
  85.             }
  86.         }
  87.         double ans=0;
  88.         for(i=1;i<=n+1;++i)
  89.         {
  90.             ans+=d[n+1][i];
  91.             //cout << d[n+1][i]<<" ";
  92.         }
  93.         printf("%.9lf\n",ans);
  94.     }
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement