Codeforces Round #751 (Div. 2)

D

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 3e5 + 10, mod = 1e7 + 10;
int a[N], b[N], c[N], d[N];
int n;
void print(int x)
{
    if(x == n) return ;
    print(c[x]);
    printf("%d ", x);
}

int main()
{
    cin >> n;
    LL sum = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i ++) {
        cin >> b[i];
    }
    d[n] = 0;
    queue<int> q;
    int dis = n;
    q.push(n);
    while (!q.empty()) {
        int x = q.front();//当前位置
        q.pop();
        if (x <= 0) {
            printf("%d\n", d[x]);
            print(x);
            return 0;
        }
        int fall = x + b[x];//滑下落之后
        int jump = fall - a[fall];//滑下去之后继续向上跳的位置
        for (int i = min(dis - 1, fall - 1); i >= jump; i --) {//
            d[i] = d[x] + 1;//这些位置跳的次数加一
            c[i] = x;//记录跳跃后的位置
            q.push(i);
        }
        dis = min(dis, jump);//跳的过程中最小之值,只有之后搜的比这个小才有搜的必要性
    }
    cout << -1 << endl;
    return 0;
}
/*
2 2 2 3 3
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值