CodeForces 1860E Fast Travel Text Editor

发布时间 2023-08-19 07:19:39作者: zltzlt

洛谷传送门

CF 传送门

没场切。/ng

考虑将原问题转成:

给长度为 \(n\) 的序列 \(a_{1 \sim n}\),你要从 \(x\) 位置走到 \(y\) 位置,每次你可以走到相邻位置,或走到与当前位置数字相同的位置,求最少步数。

这是一类经典问题,考虑建图后最短路解决。给每个位置建点 \(p_i\),值域上每个数建点 \(q_i\),然后连 \(p_{i - 1} \leftrightarrow p_i\),边权为 \(1\);连 \(p_i \to q_{a_i}\),边权为 \(1\)\(q_{a_i} \to p_i\),边权为 \(0\)。图上 \(p_x \to p_y\) 的最短路就是答案。

但是现在有 \(m\) 组询问。对每个起点都跑一遍最短路显然不现实。观察到 \(n, m\) 同阶,且 \(a_i\) 值域较小(\(a_i \le V = 26^2\)),可以考虑 meet-in-the-middle 的思想。

我们发现,\(p_x \to p_y\) 要么是只通过相邻位置走过去(此时步数为 \(|x - y|\)),要么必须经过至少一个 \(q_i\)。考虑对于 \(\forall i \in [1, V], j \in [1, n]\),预处理出 \(q_i \to p_j\) 的最短路 \(f_{i, j}\)\(p_j \to q_i\) 的最短路 \(g_{i, j}\),询问时我们枚举经过了哪个 \(q_i\),答案就是 \(\min(|x - y|, \min\limits_{i = 1}^V g_{i, x} + f_{i, y} + 1)\)

如果使用 01bfs 求最短路,时间复杂度就是 \(O(nV)\)

code
// Problem: E. Fast Travel Text Editor
// Contest: Codeforces - Educational Codeforces Round 153 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1860/problem/E
// Memory Limit: 512 MB
// Time Limit: 5000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<int, int> pii;

const int maxn = 51000;

int n, m, id[26][26], ntot, f[680][maxn], g[680][maxn];
int q[maxn * 2];
char s[maxn];
vector<pii> G[maxn], T[maxn];

void solve() {
	scanf("%s%d", s + 1, &m);
	n = strlen(s + 1);
	ntot = n;
	for (int i = 0; i < 26; ++i) {
		for (int j = 0; j < 26; ++j) {
			id[i][j] = ++ntot;
		}
	}
	for (int i = 1; i < n; ++i) {
		if (i > 1) {
			G[i].pb(i - 1, 1);
			T[i].pb(i - 1, 1);
		}
		if (i + 1 < n) {
			G[i].pb(i + 1, 1);
			T[i].pb(i + 1, 1);
		}
		G[i].pb(id[s[i] - 'a'][s[i + 1] - 'a'], 1);
		T[id[s[i] - 'a'][s[i + 1] - 'a']].pb(i, 1);
		G[id[s[i] - 'a'][s[i + 1] - 'a']].pb(i, 0);
		T[i].pb(id[s[i] - 'a'][s[i + 1] - 'a'], 0);
	}
	mems(f, 0x3f);
	mems(g, 0x3f);
	for (int S = n + 1; S <= ntot; ++S) {
		int hd = maxn, tl = maxn - 1, k = S - n;
		q[++tl] = S;
		f[k][S] = 0;
		while (hd <= tl) {
			int u = q[hd++];
			for (pii p : G[u]) {
				int v = p.fst, d = p.scd;
				if (f[k][v] > f[k][u] + d) {
					f[k][v] = f[k][u] + d;
					if (d) {
						q[++tl] = v;
					} else {
						q[--hd] = v;
					}
				}
			}
		}
		hd = maxn;
		tl = maxn - 1;
		q[++tl] = S;
		g[k][S] = 0;
		while (hd <= tl) {
			int u = q[hd++];
			for (pii p : G[u]) {
				int v = p.fst, d = p.scd;
				if (g[k][v] > g[k][u] + d) {
					g[k][v] = g[k][u] + d;
					if (d) {
						q[++tl] = v;
					} else {
						q[--hd] = v;
					}
				}
			}
		}
	}
	while (m--) {
		int x, y;
		scanf("%d%d", &x, &y);
		int ans = abs(x - y);
		for (int i = 1; i <= 676; ++i) {
			ans = min(ans, g[i][x] + f[i][y] + 1);
		}
		printf("%d\n", ans);
	}
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}