AtCoder Beginner Contest 189

题目: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);
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值