zoukankan      html  css  js  c++  java
  • [hdu4870]高斯消元

    题意:小明有2个账号,rating都是0分,每打一场赢的概率为P,假设当前分为x,赢了分数变为min(1000,x+50),输了则分数变为max(0,x-100),小明每次都选rating小的账号打,求打到有一个账号为1000所需的场数的期望值

     思路:很明显需要把分数离散化,50分为1个单位。利用期望的可加性建立状态:dp(x,y)(x<=y))表示当前两个账号rating小的为x,大的为y,到达目标状态所需场数的期望值,则有dp(x,y)=P*dp(x+1,y)+(1-P)*dp(x-1,y){这里为了描述方便,没有考虑边界},dp(19,20)=0。建好图后由于所有的dp值都是未知的,但转移时的每一项前面的系数为常数,所以可以考虑用高斯消元来确定每个状态的值。


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    /* ******************************************************************************** */
    #include <iostream>                                                                 //
    #include <cstdio>                                                                   //
    #include <cmath>                                                                    //
    #include <cstdlib>                                                                  //
    #include <cstring>                                                                  //
    #include <vector>                                                                   //
    #include <ctime>                                                                    //
    #include <deque>                                                                    //
    #include <queue>                                                                    //
    #include <algorithm>                                                                //
    #include <map>                                                                      //
    #include <cmath>                                                                    //
    using namespace std;                                                                //
                                                                                        //
    #define pb push_back                                                                //
    #define mp make_pair                                                                //
    #define X first                                                                     //
    #define Y second                                                                    //
    #define all(a) (a).begin(), (a).end()                                               //
    #define fillchar(a, x) memset(a, x, sizeof(a))                                      //
                                                                                        //
    typedef pair<intint> pii;                                                         //
    typedef long long ll;                                                               //
    typedef unsigned long long ull;                                                     //
                                                                                        //
    #ifndef ONLINE_JUDGE                                                                //
    void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}    //
    void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>                    //
    void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;          //
    while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>      //
    void print(const T t){cout<<t<<endl;}template<typename F,typename...R>              //
    void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>   //
    void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}   //
    #endif // ONLINE_JUDGE                                                              //
    template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}        //
    template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}        //
    template<typename T>                                                                //
    void V2A(T a[],const vector<T>&b){for(int i=0;i<b.size();i++)a[i]=b[i];}            //
    template<typename T>                                                                //
    void A2V(vector<T>&a,const T b[]){for(int i=0;i<a.size();i++)a[i]=b[i];}            //
                                                                                        //
    const double PI = acos(-1.0);                                                       //
    const int INF = 1e9 + 7;                                                            //
                                                                                        //
    /* -------------------------------------------------------------------------------- */
     
    struct Gauss {
        const static int maxn = 1e3 + 7;
        double A[maxn][maxn];
        int n;
        double* operator [] (int x) {
            return A[x];
        }
        /** 要求系数矩阵可逆
        A是增广矩阵,A[i][n]是第i个方程右边的常数bi
        运行结束后 A[i][n]是第i个未知数的值 **/
        void solve() {
            for (int i = 0; i < n; i ++) {
                int r = i;
                for (int j = i + 1; j < n; j ++) {
                    if (fabs(A[j][i]) > fabs(A[r][i])) r = j;
                }
                if (r != i) for (int j = 0; j <= n; j ++) swap(A[r][j], A[i][j]);
     
                for (int j = n; j >= i; j --) {
                    for (int k = i + 1; k < n; k ++) {
                        A[k][j] -= A[k][i] / A[i][i] * A[i][j];
                    }
                }
            }
            for(int i = n - 1; i >= 0; i --) {
                for (int j = i + 1; j < n; j ++) {
                    A[i][n] -= A[j][n] * A[i][j];
                }
                A[i][n] /= A[i][i];
            }
        }
    };
    Gauss solver;
    int c, n;
    double p;
    int hsh[22][22];
     
    void add(int x, int y, int z) {
        solver[c][x] += 1;
        solver[c][y] += -p;
        solver[c ++][z] += p - 1;
    }
    void init() {
        fillchar(solver.A, 0);
        c = 0;
        for (int i = 0; i < 20; i ++) {
            for (int j = i; j < 20; j ++) {
                int win = i + 1, unwin = max(i - 2, 0);
                add(hsh[i][j], hsh[min(win, j)][max(win, j)], hsh[unwin][j]);
            }
        }
        solver[c ++][hsh[19][20]] = 1;
        for (int i = 0; i < c - 1; i ++) {
            solver[i][n] = 1;
        }
    }
    void pre_init() {
        int c = 0;
        for (int i = 0; i < 20; i ++) {
            for (int j = i; j < 20; j ++) {
                hsh[i][j] = c ++;
            }
        }
        hsh[19][20] = c ++;
        solver.n = n = c;
    }
    int main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt""r", stdin);
        //freopen("out.txt", "w", stdout);
    #endif // ONLINE_JUDGE
        pre_init();
        while (cin >> p) {
            init();
            solver.solve();
            printf("%.10f ", solver[0][n]);
        }
        return 0;
    }
    /* ******************************************************************************** */
  • 相关阅读:
    Jquery 中止正在执行的AJAX
    SQL语句添加说明
    MINIUI基本学习笔记
    ADO.NET使用事务简单实例
    通过反射将Datetable转换为泛型List<T>
    C语言七夕必备神器,待那烟花灿烂时,依旧做个单身狗
    七夕来了,请用C 语言和我交流
    C语言第一个字符串Hello,C语言基础教程之字符串
    无形细节最为致命,C语言中与零值比较那些小事儿
    C语言讨论象棋将帅问题,代码短又美!
  • 原文地址:https://www.cnblogs.com/jklongint/p/4697237.html
Copyright © 2011-2022 走看看