hdu4418 Time travel 高斯+期望
Time travel
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 519 Accepted Submission(s): 82
Problem Description
Agent K is one of the greatest agents in a secret organization called Men in Black. Once he needs to finish a mission by traveling through time with the Time machine. The Time machine can take agent K to some point (0 to n-1) on the timeline and when he gets to the end of the time line he will come back (For example, there are 4 time points, agent K will go in this way 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, ...). But when agent K gets into the Time machine he finds it has broken, which make the Time machine can't stop (Damn it!). Fortunately, the time machine may get recovery and stop for a few minutes when agent K arrives at a time point, if the time point he just arrive is his destination, he'll go and finish his mission, or the Time machine will break again. The Time machine has probability Pk% to recover after passing k time points and k can be no more than M. We guarantee the sum of Pk is 100 (Sum(Pk) (1 <= k <= M)==100). Now we know agent K will appear at the point X(D is the direction of the Time machine: 0 represents going from the start of the timeline to the end, on the contrary 1 represents going from the end. If x is the start or the end point of the time line D will be -1. Agent K want to know the expectation of the amount of the time point he need to pass before he arrive at the point Y to finish his mission.
If finishing his mission is impossible output "Impossible !" (no quotes )instead.
Input
There is an integer T (T <= 20) indicating the cases you have to solve. The first line of each test case are five integers N, M, Y, X .D (0< N,M <= 100, 0 <=X ,Y < 100 ). The following M non-negative integers represent Pk in percentile.
Output
For each possible scenario, output a floating number with 2 digits after decimal point
If finishing his mission is impossible output one line "Impossible !"
(no quotes )instead.
Sample Input
2
4 2 0 1 0
50 50
4 1 0 2 1
100
Sample Output
8.14
2.00
Source
2012 ACM/ICPC Asia Regional Hangzhou Online
Recommend
liuyiding
E[x] = sigma((E[x+i]+i) * p)(i∈[1, m])
(走i步经过i个点,所以是E[x+i]+i)
展开就是这样E=E[i+1]*p1+E[i+2]*p2+……E[i+m]*pm+p[1]*1+p[2]*2……p[m]*m
[cpp]
/*
题意:一个人在数轴上来回走,以pi的概率走i步i∈[1, m],给定n(数轴长度),m,
e(终点),s(起点),d(方向),求从s走到e经过的点数期望.
题意真是晦涩难懂,误导了我好久,纠结,痛苦。
设E[x]是人从x走到e经过点数的期望值,显然对于终点有:E[e] = 0
n个点翻过去(除了头尾两个点)变为2*(n-1)个点
例如:6个点:012345 ---> 0123454321
那么显然,从5开始向右走其实就是相当于往回走
然后方向就由两个状态转化成一个状态的,然后每个点就是只有一种状态了,对每个点建立方程高斯消元即可
bfs判断是否可以到达终点
建立方程:
E[i]=E[i+1]*p1+E[i+2]*p2+……E[i+m]*pm+1*p[1]+2*p[2]……m*p[m]
*/
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<memory.h>
using namespace std;
int n,m,y,xx,d;
double p[105];
bool vis[210];
double a[210][210];
const double eps = 1e-12;
double x[230];
bool free_x[230];
void debug()
{
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
{
printf("%lf ",a[i][j]);
if(j==n) puts("");
}
}
int sgn(double x)
{
return (x>eps)-(x<-eps);
}
int gauss()
{
int i, j, k;
int max_r;
int col;
double temp;
int free_x_num;
int free_index;
int equ = n,var = n;
col = 0;
memset(free_x,true,sizeof(free_x));
for (k = 0; k < equ && col < var; k++, col++)
{
max_r = k;
for (i = k + 1; i < equ; i++)
{
if (sgn(fabs(a[i][col]) - fabs(a[max_r][col]))>0) max_r = i;
}
if (max_r != k)
{
for (j = k; j < var + 1; j++) swap(a[k][j], a[max_r][j]);
}
if (sgn(a[k][col]) == 0 )
{
k--; continue;
}
for (i = k + 1; i < equ; i++)
{
if (sgn(a[i][col])!=0)
{
double t = a[i][col] / a[k][col];
for (j = col; j < var + 1; j++)
{
a[i][j] = a[i][j] - a[k][j] * t;
}
}
}
}
for(i=k;i<equ;i++)
if(sgn(a[i][col])!=0) {return 0;}
if (k < var)
{
for (i = k - 1; i >= 0; i--)
{
free_x_num = 0;
for (j = 0; j < var; j++)
{
if ( sgn(a[i][j])!=0 && free_x[j]){
free_x_num++, free_index = j; 
补充:软件开发 , C++ ,