Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define llong long long
  4.  
  5. using namespace std;
  6.  
  7. typedef vector<int> big;
  8.  
  9. const int MAXN = (int) 5e3 + 7;
  10. const int INF = (int) 2e9 + 7;
  11.  
  12. int t, n;
  13. long long path[MAXN][MAXN];
  14.  
  15. double prob[MAXN][MAXN];
  16. double expect[MAXN][MAXN];
  17.  
  18. int main() {  
  19.   #ifdef LOCAL
  20.   freopen("in", "r", stdin);
  21.   #endif
  22.  
  23.   ios_base::sync_with_stdio(0);
  24.  
  25.   cin >> t;
  26.   while (t--) {
  27.     cin >> n;
  28.     for (int i = 1; i <= n + 1; i++) {
  29.       for (int j = 1; j <= n + 1; j++) {
  30.         prob[i][j] = 0.0;
  31.         expect[i][j] = 0.0;
  32.       }
  33.     }
  34.  
  35.     prob[1][1] = 1.0;
  36.     expect[1][1] = 0.0;
  37.     for (int i = 1; i <= n; i++) {
  38.       for (int j = 1; j <= i; j++) {
  39.         int d, dr;
  40.         cin >> d >> dr;
  41.         if (d < dr) {
  42.           prob[i + 1][j] += prob[i][j];
  43.           expect[i + 1][j] += expect[i][j] + prob[i][j] * d;
  44.         }
  45.         else if (dr < d) {
  46.           prob[i + 1][j + 1] += prob[i][j];
  47.           expect[i + 1][j + 1] += expect[i][j] + prob[i][j] * dr;
  48.         }
  49.         else {
  50.           double cur = expect[i][j] + prob[i][j] * d;
  51.           cur /= 2;
  52.  
  53.           prob[i + 1][j] += prob[i][j] * 0.5;
  54.           prob[i + 1][j + 1] += prob[i][j] * 0.5;
  55.  
  56.           expect[i + 1][j] += cur;
  57.           expect[i + 1][j + 1] += cur;
  58.         }
  59.       }
  60.     }
  61.     double ans = 0.0;
  62.     for (int i = 1; i <= n + 1; i++) {
  63.       ans += expect[n + 1][i];
  64.     }
  65.     cout << fixed << setprecision(10) << ans << "\n";
  66.   }
  67.   return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement