当前位置: 首页 > 工具软件 > Frog CMS > 使用案例 >

B. Frog Traveler

赫连宏伯
2023-12-01

Problem - 1601B - Codeforces

题意: 一个人想要从深度为n的井中爬出去,在任意高度x上他都可以向上爬0<=i<=a[i]距离,并且在爬到to位置后,需休息并下滑bto距离,要求他最小需要爬多少次才能够到达地面。

思路:很显然需要bfs,但是直接进行bfs,由于下滑,特判开头,之后每次下滑再向上爬,并标记到达每个点之后下滑点,通过标记来进行bfs优化是不可行的,会导致在test10tle

于是,我们需要更巧妙的优化方式

(10条消息) Codeforces-1601 B: Frog Traveler_盖乌咪·A·埃迪尔的博客-CSDN博客

这位dalao图文并茂讲的很好

具体来讲,就是bfs从大到小来枚举向上跳的距离,每当跳的点超过了之前的上线之后。就需要立即break,并更新上限。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>
//#include<bits/stdc++.h>

//#define int ll
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);
#define YES {puts("YES");return;}
#define NO {puts("NO"); return;}
#define all(x) x.begin(),x.end()

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 300010, M = 2 * N, B = N, MOD = 998244353;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;

int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
//int dx[8] = { 1,2,2,1,-1,-2,-2,-1 }, dy[8] = { 2,1,-1,-2,-2,-1,1,2 };
int n, m, k;
int a[N], b[N];
int dist[N], pre[N], jump[N];
int q[N], hh = 0, tt = -1;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll a, ll b, ll MOD) {
	ll res = 1;
	while (b) {
		if (b & 1) res = res * a % MOD;
		a = a * a % MOD;
		b >>= 1;
	}
	return res;
}

inline void init() {}

bool bfs()
{
	memset(dist, 0x3f, sizeof dist);

	int upside = INF;
	dist[n] = 0; q[++tt] = n; pre[n] = n; jump[n] = n;

	while (hh<=tt) {
		int x = q[hh++];
		for (int i = a[x]; i >= 0; --i) {
			int up = x - i, v = up + b[up];
			if (up == 0) { dist[0] = min(dist[0], dist[x] + 1); pre[0] = x; return true; }
			if (up >= upside) break;
			if (dist[v] > dist[x] + 1) {
				dist[v] = dist[x] + 1; q[++tt] = v;
				pre[v] = x; jump[v] = i;
			}
		}
		upside = min(upside, x - a[x]);
	}
	return false;
}

inline void slove()
{
	cin >> n;
	for (int i = 1; i <= n; i++) si(a[i]);
	for (int i = 1; i <= n; i++) si(b[i]);

	bool suc = bfs();

	if (!suc) {
		puts("-1"); return;
	}
	vector<int> ans;
	int flag = 0;
	while (flag != n)
	{
		ans.push_back(pre[flag] - jump[flag]);
		flag = pre[flag];
	}
	reverse(all(ans));
	ans.pop_back(); ans.push_back(0);
	cout << ans.size() << endl;
	for (int v : ans) cout << v << ' ';
	puts("");
	return;
}

signed main()
{
	//IOS;
	int _ = 1;
	//si(_);
	init();
	while (_--)
	{
		slove();
	}
	return 0;
}
/*
3
0 2 2
1 1 0

*/

 类似资料:

相关阅读

相关文章

相关问答