题意:http://www.cnblogs.com/zhyfzy/p/4436017.html

题解:概率DP,算第几次拿到i的概率 ,把总和加起来再乘以 d[i]就行了

解题代码:

  1 // BEGIN CUT HERE
  2 /*
  3 
  4 */
  5 // END CUT HERE
  6 #line 7 "RandomPancakeStack.cpp"
  7 #include <cstdlib>
  8 #include <cctype>
  9 #include <cstring>
 10 #include <cstdio>
 11 #include <cmath>
 12 #include <algorithm>
 13 #include <vector>
 14 #include <string>
 15 #include <iostream>
 16 #include <sstream>
 17 #include <map>
 18 #include <set>
 19 #include <queue>
 20 #include <stack>
 21 #include <fstream>
 22 #include <numeric>
 23 #include <iomanip>
 24 #include <bitset>
 25 #include <list>
 26 #include <stdexcept>
 27 #include <functional>
 28 #include <utility>
 29 #include <ctime>
 30 using namespace std;
 31 
 32 #define PB push_back
 33 #define MP make_pair
 34 
 35 #define REP(i,n) for(i=0;i<(n);++i)
 36 #define FOR(i,l,h) for(i=(l);i<=(h);++i)
 37 #define FORD(i,h,l) for(i=(h);i>=(l);--i)
 38 
 39 typedef vector<int> VI;
 40 typedef vector<string> VS;
 41 typedef vector<double> VD;
 42 typedef long long LL;
 43 typedef pair<int,int> PII;
 44 
 45 
 46 double dp[260][260];
 47 double add[260];
 48 class RandomPancakeStack
 49 {
 50         public:
 51         double expectedDeliciousness(vector <int> d)
 52         {
 53             memset(dp,0,sizeof(dp));
 54             int n = d.size();
 55             for(int i = 0 ;i < n;i ++)
 56                 dp[0][i] = 1.0/n ; 
 57 
 58             for(int i = 1 ;i < n ;i ++){
 59                  for(int j = 0 ;j < n;j ++){
 60                         dp[i][j] = 0 ; 
 61                     for(int k = j + 1; k < n;k++){
 62                         dp[i][j] += dp[i-1][k] * 1./(n-i);            
 63                     }
 64                  }    
 65             }
 66             double ans = 0 ; ;
 67             for(int i = 0 ;i < n;i ++)
 68             {
 69                 double tmp  =0 ; 
 70                 for(int j = 0;j <= n;j ++)
 71                     tmp += dp[j][i];
 72                 ans += tmp * d[i];
 73             }
 74             return ans; 
 75         }
 76         
 77 // BEGIN CUT HERE
 78     public:
 79     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
 80     private:
 81     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
 82     void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
 83     void test_case_0() { int Arr0[] = {1,1,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 1.6666666666666667; verify_case(0, Arg1, expectedDeliciousness(Arg0)); }
 84     void test_case_1() { int Arr0[] = {3,6,10,9,2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 9.891666666666667; verify_case(1, Arg1, expectedDeliciousness(Arg0)); }
 85     void test_case_2() { int Arr0[] = {10,9,8,7,6,5,4,3,2,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 10.999999724426809; verify_case(2, Arg1, expectedDeliciousness(Arg0)); }
 86     void test_case_3() { int Arr0[] = {1,2,3,4,5,6,7,8,9,10}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 7.901100088183421; verify_case(3, Arg1, expectedDeliciousness(Arg0)); }
 87     void test_case_4() { int Arr0[] = {2,7,1,8,2,8,1,8,2,8,4,5,90,4,5,2,3,5,60,2,8,74,7,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 19.368705050402465; verify_case(4, Arg1, expectedDeliciousness(Arg0)); }
 88     void test_case_5() { int Arr0[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
 89  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 1.718281828459045; verify_case(5, Arg1, expectedDeliciousness(Arg0)); }
 90 
 91 // END CUT HERE
 92 
 93 };
 94 
 95 // BEGIN CUT HERE
 96 int main()
 97 {
 98         RandomPancakeStack ___test;
 99         ___test.run_test(-1);
100         return 0;
101 }
102 // END CUT HERE
View Code

 

posted on 2015-04-17 22:27  dark_dream  阅读(341)  评论(0编辑  收藏  举报