题解归档 - cf915B

题解归档 - cf915B

本文由 cf-code 本地题解库自动归档;公开内容以本地 AC/验证版本为准。

思路

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  ~  ~


 赏 
感谢您的支持,我会继续努力哒!
支付宝收款码
tips
文章二维码 分类标签:归档TypechoAutoUpload
文章标题:题解归档 - cf915B
文章链接:https://www.fangshaonian.cn/archives/395/
最后编辑:2026 年 6 月 28 日 19:08 By 方少年
许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
(*) 8 + 7 =
快来做第一个评论的人吧~