abc376B Hands on Ring (Easy)

chenfy27的刷题记录 / 2024-10-29 / 原文

由1~N的块构成的环,最初左手在1号、右手在2号,接下来有Q组操作{H, T},H可以是L或者R,分别表示左手和右手,T是目标位置,移动时要求左右手不能在同一个块上。
3<=N<=100, 1<=Q<=100

分析:模拟,关键在于如何判断是顺时针移动还是逆时针移动。

#include <bits/stdc++.h>
using i64 = long long;

void solve() {
	int N, Q;
	std::cin >> N >> Q;
	int ans = 0;
	int a[2] = {1, 2};

	auto work = [&](int x, int t) {
		if ((a[x] <= a[!x] && a[!x] <= t) || (t <= a[!x] && a[!x] <= a[x])) {
			ans += N - std::abs(a[x] - t);
		} else {
			ans += std::abs(a[x] - t);
		}
		a[x] = t;
	};

	for (int i = 1; i <= Q; i++) {
		std::string H;
		int T;
		std::cin >> H >> T;
		work(H == "L" ? 0 : 1, T);
	}
	std::cout << ans << "\n";
}

int main() {
	std::cin.tie(0)->sync_with_stdio(0);
	int t = 1;
	while (t--) solve();
	return 0;
}