题解:AT_abc376_b [ABC376B] Hands on Ring (Easy)
AT_abc376_b [ABC376B] Hands on Ring (Easy) 题解
题目传送门
某区区赛 T3。
思路
观察到
一开始左手在
每次只动一只手,根据
每次移动从当前位置移动到目标位置,在环上有顺时针和逆时针两条路径,两次循环分别模拟要操作多少次,然后取两种操作数的最小值即可。
注意移动过程中不能经过另一只手当前所在的位置。
Code
#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;
}
提交记录