hdu 4870 rating(高斯消元求期望)

Rating

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 872    Accepted Submission(s): 545
Special Judge

Problem Description
A 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?
 
Input
There 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.
 
Output
You 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.000000
0.814700
 
Sample Output
39.000000
82.181160
 
Author
FZU
 
Source

题目大意:一个人注册两个账号,初始rating都是0,他每次拿低分的那个号去打比赛,赢了加50分,输了扣100分,胜率为p,他会打到直到一个号有1000分为止,问比赛场次的期望。

解题思路:一共有231种状态:(0,0)、(0,50)..(0,1000)、(50,50)...(50,1000)...(1000,1000)。高斯消元求期望。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std; double p,A[][];
int cnt;
struct node
{
int i,j;
node(int i=,int j=):i(i),j(j){}
}map[];
const double eps=1e-;
int dcmp(double x)
{
if(fabs(x)<eps) return ;
if(x->eps) return ;
return -;
}
void swap(int &a,int &b){int t=a;a=b;b=t;}
inline int max(int a,int b){return a>b?a:b;}
inline int min(int a,int b){return a<b?a:b;}
void init()
{
cnt=;
for(int i=;i<=;i++)
for(int j=i;j<=;j++)
map[cnt++]=node(i,j);
}
int find(int x,int y)
{
int c=;
for(int i=;i<=;i++)
for(int j=i;j<=;j++)
{
if(i==x&&j==y)
return c;
c++;
}
}
void build_matrix()
{
memset(A,,sizeof(A));
for(int i=;i<cnt;i++)
{
A[i][i]=;
if(map[i].j==){A[i][cnt]=;continue;}
int x=min(map[i].i+,),y=map[i].j;
if(x>y) swap(x,y);
int pos=find(x,y);
A[i][pos]-=p;
x=max(map[i].i-,);y=map[i].j;
pos=find(x,y);
A[i][pos]-=-p;
A[i][cnt]+=;
}
}
void gauss(int n)
{
int i,j,k,r;
for(i=;i<n;i++)
{
r=i;
for(j=i+;j<n;j++)
if(fabs(A[j][i])>fabs(A[r][i])) r=j;
if(dcmp(A[r][i])==) continue;
if(r!=i) for(j=;j<=n;j++) swap(A[r][j],A[i][j]);
for(k=;k<n;k++) if(k!=i)
for(j=n;j>=i;j--) A[k][j]-=A[k][i]/A[i][i]*A[i][j];
}
for(i=n-;i>=;i--)
{
for(j=i+;j<cnt;j++)
A[i][cnt]-=A[j][cnt]*A[i][j];
A[i][cnt]/=A[i][i];
}
}
int main()
{
init();
while(~scanf("%lf",&p))
{
build_matrix();
gauss(cnt);
printf("%.6lf\n",A[][cnt]);
}
return ;
}
上一篇:linux环境下python的pdb调试方法


下一篇:shell脚本例子集锦(习题总结)