Educational Codeforces Round 133 C. Robot in a Hallway

C. Robot in a Hallway

这题其实不用任何数据结构维护
显然,当一个点为终点时有且仅有一条合法路径,均为蛇形+回字的走法,唯一的区别是奇数列和偶数列走回字的方向不同。
所以本题的关键是如何在走蛇形的同时求出之后走回字的时间。这个时间其实可以\(O(1)\)求出。以偶数列为例

首先预处理出数组\(b\)\(b_k\)表示从起点\((0,1)\)走回字走到\((1,k)\)的时间。

设当前蛇形走到\((0,i)\),那么\((1,i)\)为终点,当前的时间为\(now\),答案即为\(max(now+(n-i)*2+1,b_i)\)

证明:首先,\(now\)是严格大于走回字走到\((0,i)\)的时间的(走蛇形走过的点更多),所以我们只需要考虑答案是否会大于\(b_i\)。如果答案为\(b_i\),那么肯定存在这样一个点\(u\),我们从当前点走了\(m\)步到达,并且\(b_u>now+m\),这时候要停下来等待,之后的路线时间就跟\(b\)一模一样了。如果没有这样一个点,我们是不需要停下来等待的,那么时间即为\(now+(n-i)*2+1\),就是当前的时间加上走的步数。

#include<bits/stdc++.h>
using namespace std;

#define fr first
#define se second
#define et0 exit(0);
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i ++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i --)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;

const int INF = 0X3f3f3f3f, N = 2e5 + 10, MOD = 1e9 + 7;
const double eps = 1e-7, pi = acos(-1);

LL t[2][N];
LL a[2 * N], b[2 * N], c[2 * N];

void work() {
	int n;
	cin >> n;
	rep(i, 0, 2 * n) a[i] = b[i] = c[i] = 0;
	rep(i, 1, n) cin >> t[0][i];
	rep(i, 1, n) cin >> t[1][i];
	a[1] = 0, b[1] = t[1][1] + 1;
	
	rep(i, 2, n) {
		a[i] = max(a[i - 1], t[0][i]) + 1;
		b[i] = max(b[i - 1], t[1][i]) + 1;
	}
	int pos = n + 1;
	rrep(i, n, 1) {
		a[pos] = max(a[pos - 1], t[1][i]) + 1;
		b[pos] = max(b[pos - 1], t[0][i]) + 1;
		pos++;
	}
	
	pos = 1;
	rep(i, 2, 2 * n) {
		if (i % 2 == 0 && pos == 1) c[i] = max(c[i - 1], t[1][i / 2]) + 1;
		else if (i % 2 == 0) c[i] = max(c[i - 1], t[0][i / 2]) + 1;
		else if (pos == 1) c[i] = max(c[i - 1], t[1][(i + 1) / 2]) + 1, pos--;
		else c[i] = max(c[i - 1], t[0][(i + 1) / 2]) + 1, pos++;
	}
	
	LL ans = 1e18;
	rep(i, 1, n) {
		LL u = c[2 * (i - 1) + 1];
		if (i & 1) ans = min(ans, max(u + (n - i) * 2 + 1, a[i + (n - i) * 2 + 1]));
		else ans = min(ans, max(u + (n - i) * 2 + 1, b[i + (n - i) * 2 + 1]));
	}
	
	cout << ans << endl;
}

signed main() {
	IO
	int test = 1;
	cin >> test;
	
	while (test--) {
		work();
	}
	
	return 0;
}
posted @ 2022-08-05 20:42  xhy666  阅读(152)  评论(0)    收藏  举报