题目:F - Sugoroku2
分析:
这道题不算太难,就是个解方程,还有就是浮点精度。
f[i]表示从i到终点的期望步数
x[i], y[i] = sumf(i +1, … i + m) / m + 1 = x * f[0] + y
每次维护滑动窗口内的x和y,最后根据f[0] = x * f[0] + y即可求出f[0]
对于无解的情况,要用eps判断。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;//三年竞赛一场空,不开long long见祖宗
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define x first
#define y second
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const int maxn = 4e5 + 10;
const int inf = 0x3f3f3f3f;
double a[maxn], b[maxn];
const double eps=1e-6;
int vis[maxn];
int n, m, k;
inline int sgn(double x)
{
if(x>-eps && x<eps)
return 0;
if(x>0) return 1;
else return -1;
}
int main()
{
cin >> n >> m >> k;
for(int i = 1; i <= k; i++)
{
int x; cin >> x; vis[x] = 1;
}
double suma = 0, sumb = 0;
for(int i = n - 1; i >= 0; i--)
{
if(vis[i])
{
a[i] = 1, b[i] = 0;
}
else
{
a[i] = suma / m, b[i] = sumb / m + 1;
}
suma += a[i], suma -= a[i + m];
sumb += b[i], sumb -= b[i + m];
}
//cout << a[1] << " " << b[1] << endl;
if(sgn(a[0] - 1) == 0) cout << -1 << endl;
else
{
double res = b[0] / (1 - a[0]);
printf("%.4f\n", res);
}
}