228 字
1 分钟
题解: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 longint 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;}题解:AT_abc376_b [ABC376B] Hands on Ring (Easy)
https://banglee13.github.io/posts/atabc376b/分享文章
生成精美分享图或复制链接,与更多人分享本文。
继续阅读
换条路线
从其他文章中稳定抽取
最后更新于 ,距今已过 395 天
部分内容可能已过时