跳到正文

题解

题解:AT_abc376_b [ABC376B] Hands on Ring (Easy)

banglee约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年10月26日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 2 分钟

版本

  1. v1当前

AT_abc376_b [ABC376B] Hands on Ring (Easy) 题解

题目传送门

某区区赛 T3。

思路

观察到 N≤100N \le 100,考虑模拟。

一开始左手在 11,右手在 22。

每次只动一只手,根据 HiH_i 决定动左手还是右手,另一只手保持不动。

每次移动从当前位置移动到目标位置,在环上有顺时针和逆时针两条路径,两次循环分别模拟要操作多少次,然后取两种操作数的最小值即可。

注意移动过程中不能经过另一只手当前所在的位置。

Code

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,q,l=1,r=2,ans;
int cal(int cur,int end,int y) {
	int s1=0,s2=0,t=cur;
	while (t!=end) {
		t++;
		if (t>n) t=1;
		s1++;
		if (t==y) {
			s1=1e9;
			break;
		}
	}
	t=cur;
	while (t!=end) {
		t--;
		if (t<1) t=n;
		s2++;
		if (t==y) {
			s2=1e9;
			break;
		}
	}
	return min(s1,s2);
}
signed main() {
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>q;
	while (q--) {
		char op;
		int x;
		cin>>op>>x;
		if (op=='L') ans+=cal(l,x,r),l=x;
		else ans+=cal(r,x,l),r=x;
	}
	cout<<ans;
	return 0;
}

提交记录

原文