hdu4870 Rating (高斯消元或者dp),hdu4870rating

來源:互聯網
上載者:User

hdu4870 Rating (高斯消元或者dp),hdu4870rating
RatingTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 213 Accepted Submission(s): 126
Special Judge


Problem DescriptionA little girl loves programming competition very much. Recently, she has found a new kind of programming competition named "TopTopTopCoder". Every user who has registered in "TopTopTopCoder" system will have a rating, and the initial value of rating equals to zero. After the user participates in the contest held by "TopTopTopCoder", her/his rating will be updated depending on her/his rank. Supposing that her/his current rating is X, if her/his rank is between on 1-200 after contest, her/his rating will be min(X+50,1000). Her/His rating will be max(X-100,0) otherwise. To reach 1000 points as soon as possible, this little girl registered two accounts. She uses the account with less rating in each contest. The possibility of her rank between on 1 - 200 is P for every contest. Can you tell her how many contests she needs to participate in to make one of her account ratings reach 1000 points?
InputThere are several test cases. Each test case is a single line containing a float number P (0.3 <= P <= 1.0). The meaning of P is described above.
OutputYou should output a float number for each test case, indicating the expected count of contest she needs to participate in. This problem is special judged. The relative error less than 1e-5 will be accepted.
Sample Input

1.0000000.814700

Sample Output
39.00000082.181160


一場比賽,贏了可以得50分,輸了扣100分,分數會超過1000和不會小於0.有個人用2個帳號,始終用分數低的號比賽。已知贏一場比賽的機率為p求打到1000分的場數的期望值。

1、可以用高斯消元,

令E(X,Y)為帳號分數為x,y打到1000的數學期望。則有:

E(X,Y)=PE(X1,Y1)+(1-P)E(X2,Y2)+1,X1,Y1是XY分數贏了的分數,X2,Y2對應是輸了的分數,假設X>=Y,每次比賽用Y的帳號,就會有210條方程,用mark數組標記XY分數對應的係數的索引。


代碼:

#include <iostream>#include <cmath>#include <stdio.h>#include <algorithm>#include <ctime>#include <vector>#include <cstring>#include <map>#include <string>#include <queue>using namespace std;#define LL long long#define ULL unsigned long long//#define REP(i,n) for(int i=0;i<n;++i)#define REP(i,a,b) for(int i=a;i<=b;++i)#define INFLL (1LL)<<62#define mset(a) memset(a,0,sizeof a)#define FR(a) freopen(a,"r",stdin)#define FW(a) freopen(a,"w",stdout)#define PI 3.141592654const LL MOD = 1000000007;const int maxn=222;const double eps=1e-9;double a[maxn][maxn];int mark[25][25];int cnt;double gauss(){    int m=211;    int n=210;    for(int i=0;i<n;i++)    {        int k=i;        for(;k<n;++k)            if(fabs(a[k][i])>eps)    break;        if(i!=k)            for(int j=0;j<=n;++j)                swap(a[i][j],a[k][j]);        for(int j=0;j<n;++j)        {            if(i==j)    continue;            if(fabs(a[j][i])<eps)    continue;            double x=a[j][i]/a[i][i];            for(k=i;k<m;++k)                a[j][k]-=a[i][k]*x;        }    }    return a[0][n]/a[0][0];}void makeMat(double p){    mset(a);    int m=211;    int x=0,y=0;    for(y=0;y<20;++y){        for(x=0;x<y;++x)        {            int temp=mark[y][x];            a[temp][temp]=1;            a[temp][m-1]=1;            int temp2=mark[y][max(0,x-2)];            a[temp][temp2]-=1-p;            temp2=mark[y][x+1];            a[temp][temp2]-=p;        }        int t=mark[y][y];        a[t][t]=1;        a[t][m-1]=1;        int tt=mark[y][max(0,x-2)];        a[t][tt]-=1-p;        tt=mark[x+1][x];        a[t][tt]-=p;    }}int main(){    double p;    cnt=0;    mset(mark);    REP(i,0,20)        REP(j,0,i)            mark[i][j]=cnt++;    while (cin>>p)    {        makeMat(p);        printf("%.6lf\n",gauss());    }}



2、用dp

首先離散化,因為每場比賽分數的變化都是50的倍數,令每場贏了得1分,輸了扣2分。

dp[i]表示單場比賽從i分數提高到i+1的分數的期望值,

則有:dp[i]=p+(1-p)(dp[i-2]+dp[i-1]+dp[i]+1)  

 ==>dp[i]=1/p+(1-p)/p*(dp[i-2]+dp[i-1]);dp[0]=1/p,dp[1]=1/p/p;

用ans[i][i]表示兩個帳號分數從0打到ii的期望,對於帳號分數的上升,他們是交錯上升的,意思是當他們分數一樣的時候,前面的贏一分,當前面的贏了一分之後,下一場就後面的贏,所以只需要維護ans[i+1][i] 和ans[i+1][i+1],且

ans[i+1][i]=ans[i][i]+dp[i],ans[i+1][i+1]=ans[i+1][i]+dp[i],


代碼:


#include <iostream>#include <cmath>#include <stdio.h>using namespace std;double dp[22];double ans[22][22];int main(){    double p;    while (cin>>p)    {        dp[0]=1/p;        dp[1]=1/p/p;        for(int i=2;i<20;++i)            dp[i] = 1+(1-p)/p*(dp[i-2]+dp[i-1]+1);         ans[0][0]=0;        for (int i=0;i<20;++i)        {            ans[i+1][i]=ans[i][i]+dp[i];            ans[i+1][i+1]=ans[i+1][i]+dp[i];        }        printf("%.6lf\n",ans[20][19]);    }}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.