首页 > 其他分享 >CodeForces-1601B Frog Traveler

CodeForces-1601B Frog Traveler

时间:2022-08-20 00:01:14浏览次数:76  
标签:1601B int CodeForces include dep maxn nex Traveler now

Frog Traveler

dp + bfs

感觉又像搜索

从后往前进行状态转移,像 \(bfs\) 一样保证当前搜索的是消耗次数最少的

状态转移因为是一个连续的区间,因此考虑当前能上升到的最大距离,只有能更新这个最大值,才进行状态转,保证每个位置只被访问一次

时间复杂度 \(O(n)\)

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define pii pair<int, int>
const int maxn = 3e5 + 10;
int dep[maxn];
int a[maxn], b[maxn];
pii pre[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for(int i=1; i<=n; i++) cin >> a[i];
    for(int i=1; i<=n; i++) cin >> b[i];
    int l = n;
    queue<int>q;
    q.push(n);
    dep[n] = 1;
    while(q.size())
    {
        int now = q.front();
        q.pop();
        int tp = now - a[now];
        if(tp <= 0)
        {
            pre[0] = {0, now};
            dep[0] = dep[now] + 1;
            break;
        }
        while(l >= tp)
        {
            int nex = l + b[l];
            l--;
            if(dep[nex]) continue;
            dep[nex] = dep[now] + 1;
            pre[nex] = {l + 1, now};
            q.push(nex);
        }
    }
    if(dep[0] == 0) cout << "-1\n";
    else
    {
        cout << dep[0] - 1 << "\n";
        int now = 0;
        vector<int>ans;
        for(int i=1; i<dep[0]; i++)
        {
            ans.push_back(pre[now].first);
            now = pre[now].second;
        }
        for(int i=ans.size()-1; i>=0; i--)
            cout << ans[i] << " ";
        cout << "\n";
    }
    return 0;
}

标签:1601B,int,CodeForces,include,dep,maxn,nex,Traveler,now
From: https://www.cnblogs.com/dgsvygd/p/16606941.html

相关文章

  • CodeForces-1671E Preorder
    Preorder树型dp+思维\(dp[i]\)表示以\(i\)为根的子树通过变换有多少种不同的先序遍历状态转移方程:当左右子树不同,两个子树交换位置之后,没有重复的出现:\(dp[x]......
  • Codeforces #815 Div.2
    A—BurenkaPlayswithFractions思路:数论O(1)见题解题解:#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;typedeflonglongL......
  • Educational Codeforces Round 117 (Rated for Div. 2) CF1612
    https://codeforces.com/contest/1612VP过了A~E,感觉海星。F,G这几天补。主要是luogu有翻译拯救了英语不好的我。A一眼\(x+y\equiv0\pmod{2}\),否则无解。那么显......
  • Educational Codeforces Round 33 (Rated for Div. 2) 虚拟赛体验
    前言就只做出了\(A,B,C,D\)是不是很弱?A.ChessForThreeA,B,C三人下棋,A和B先下,每次下完棋之后由现在观战的人(例如第一局就由C)代替下输的人。每次输入一个数表示谁赢......
  • Codeforces Round #815 (Div. 2) 全解
    目录ABCD1D2EAad和cb,查看是不是相等或者倍数关系,特判0Bsort()cout<<a[n]+a[n-1]-a[1]-a[2]C查看所有的四方格一个四方格有2个0,ans=1的个数一个四方格有1个0,ans=1......
  • Codeforces Round #815 (Div. 2) (补题中)
    战绩:  打到一半被叫走,回来后断断续续打完的。。。A.BurenkaPlayswithFractions刚开始感觉被trick绕进去了,思路有点乱,就先去切B了。实际上如果要a/b=c/d,我们只......
  • Codeforces Round #815 (Div. 2) 【A~C】
    A.BurenkaPlayswithFractions题目描述Burenkacametokindergarden.Thiskindergartenisquitestrange,soeachkidtherereceivestwofractions($\frac{a}......
  • codeforces526D. Om Nom and Necklace【KMP】
    飞刀可能进不了前百,但加上小李就能进前三忙完入学的各种事终于赶去图书馆时,在校内一天只吃了一个面包和巧克力,已是二十点四十。武大规定二十二点半闭馆,我满心期待在两个......
  • Codeforces Round #814 (Div. 2)(补题中)
    战绩:  有铁头娃A.ChipGame猜了个结论,第一次猜的是n==m,第二次猜的是n+m的奇偶性。严格证明也比较简单。由于只能向右向上,我们每次移动相当于缩减问题规模。那么......
  • CodeForces-1469C Building a Fence
    BuildingaFencedp模拟?维护好可摆放的区间即可,我用的区间是指当前位置可摆放的东西的下边界区间下限:\(l_i=max(l_{i+1}-k,h_i)\),表示尽量往下放,以及在地面之上......