题解归档 - cf915B
本文最后由方少年更新于2026 年 6 月 28 日,已超过0天没有更新。如果文章内容或图片资源失效,请留言反馈,将会及时处理,谢谢!
题解归档 - cf915B
本文由 cf-code 本地题解库自动归档;公开内容以本地 AC/验证版本为准。
- 本地编号:
cf915B - 本地来源:
problems/cf915B/idea.md - 题目链接:https://codeforces.com/contest/915/problem/B
- 原始标题:cf915B Browser
思路
cf915B Browser
题意
n 个标签页,光标在 pos。保留 [l,r],关闭其余。每秒可:光标左右移一格,或关闭光标左侧/右侧全部标签。求最少秒数。
做法
分三种情况:
pos < l:先移到 l,关左侧,再移到 r 关右侧pos > r:先移到 r,关右侧,再移到 l 关左侧l <= pos <= r:只需关两侧 junk;一侧则直接算,两侧取「先左后右 / 先右后左」较小值
公式见 solution.cpp。
验证
三组样例 + stress.py 对拍(n<=9 BFS brute)。
代码
来源:problems/cf915B/solution.cpp
/* Author: likely
* Time: 2026-06-08 03:31:37
**/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,i,j,k,zc,pd,cur,dq,cnt,ans,pos,l,r;
int main(){
cin>>n>>pos>>l>>r;
if(l==1 and r==n){
cout<<0<<"\n";
return 0;
}
if(pos<l){
ans=l-pos;
if(l>1) ans++;
if(r<n) ans+=r-l+1;
}else if(pos>r){
ans=pos-r;
if(r<n) ans++;
if(l>1) ans+=r-l+1;
}else{
ll cl=0,cr=0;
if(l>1) cl=pos-l+1;
if(r<n) cr=r-pos+1;
if(l==1) ans=cr;
else if(r==n) ans=cl;
else ans=min(cl+r-l+1,cr+r-l+1);
}
cout<<ans<<"\n";
return 0;
}
~ ~ The End ~ ~
文章标题:题解归档 - cf915B
文章链接:https://www.fangshaonian.cn/archives/395/
最后编辑:2026 年 6 月 28 日 19:08 By 方少年
许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)