4 条题解

  • 2
    @ 2025-4-13 16:38:30

    一道偏数学的if题,难度:很难偏难 #include #include #include <math.h> using namespace std; int main() { int n,p,l,r; cin>>n>>p>>l>>r; if(l1&&rn) { cout<<0; } else if(l1) { cout<<abs(r-p)+1; } else if(rn) { cout<<abs(l-p)+1; } else if(p>=l&&p<=r) { cout<<min(abs(l-p),abs(r-p))+r-l+2; } else if(p>r) { cout<<p-l+2; } else { cout<<r-p+2; } }

    • 2
      @ 2025-4-13 15:48:41

      一道挺难也挺好的一道if题

      这题偏数学一点

      本题有六种情况:

      1:

      1 2 3 4 5 6 7
      l     p     r
      

      这里直接就是0

      if(l==1&&r==n)cout<<0; 2:

      1 2 3 4 5 6 7
      l     p   r
      

      l靠左

      直接去l

      else if(l==1)cout<<abs(r-p)+1;

      3:

      1 2 3 4 5 6 7
        l   p     r
      

      r靠右

      直接去l else if(r==n)cout<<abs(l-p)+1;

      4:(最麻烦但最常见)

      1 2 3 4 5 6 7
        l   p   r
      

      l不在左且r不在右且p在r与l之间

      先判断去哪边近然后走回去另一边

      else if(p>=l&&p<=r)cout<<min(abs(l-p),abs(r-p))+r-l+2;

      5:

      1 2 3 4 5 6 7
        l     r   p
      

      p>r

      去完r后顺路去l else if(p>r)cout<<p-l+2;

      6:

      1 2 3 4 5 6 7
      p   l     r
      

      p<l

      与5:类似(其实已经不用再写if了)

      else cout<<r-p+2;

      最后汇总

      注意:本代码已开启防人机超熙与注释版

      using namespace std;
      const int N=1e5+5,INF=0x3f3f3f3f;
      typedef long long LL;
      int n,p,l,r; 
      int main()
      {
      	cin>>n>>p>>l>>r;
      	if(l==1&&r==n)cout<<0;//① 
      	else if(l==1)cout<<abs(r-p)+1;//② 
      	else if(r==n)cout<<abs(l-p)+1;//③ 
      	else if(p>=l&&p<=r)cout<<min(abs(l-p),abs(r-p))+r-l+2;//④ 
      	else if(p>r)cout<<p-l+2;//⑤ 
      	else cout<<r-p+2;//⑥ 
      	return O; 
      }
      /*
      1 2 3 4 5 6 7
      l           r ① 
      l   p     r   ② 
       l  p       r ③ 
       l  p     r   ④ 
       l      r   p ⑤ 
      p   l     r   ⑥ 
      */
      
    • 1
      @ 2025-5-16 19:16:15
      #include<iostream>
      #include<stdio.h>
      #include<iomanip>
      #include<math.h>
      #include<string.h>
      #define ll longlong
      const int N=1e5+10;
      const int INT=0x3f3f3f3f;
      using namespace std;
      int main()
      {
          int n, pos, l, r;
          cin >> n >> pos >> l >> r;
          if (l == 1 && r == n) {
              cout << 0 << endl;
              return 0;
          }
          if (l == 1) {
              int time = abs(pos - r) + 1; 
              cout << time << endl;
              return 0;
          }
          if (r == n) {
              int time = abs(pos - l) + 1; 
              cout << time << endl;
              return 0;
          }
          int option1 = abs(pos - l) + 1 + (r - l) + 1; 
          int option2 = abs(pos - r) + 1 + (r - l) + 1; 
          int minTime = min(option1, option2);
          cout << minTime << endl;
          return 0;
      }
      
      • 0
        @ 2025-3-21 20:04:11

        #include #include<stdio.h> #include #include<math.h> #include<string.h> #define ll longlong const int N=1e5+10; const int INT=0x3f3f3f3f; using namespace std; int main() {

        int n, pos, l, r;
        cin >> n >> pos >> l >> r;
        if (l == 1 && r == n) {
            cout << 0 << endl;
            return 0;
        }
        if (l == 1) {
            int time = abs(pos - r) + 1; 
            cout << time << endl;
            return 0;
        }
        if (r == n) {
            int time = abs(pos - l) + 1; 
            cout << time << endl;
            return 0;
        }
        int option1 = abs(pos - l) + 1 + (r - l) + 1; 
        int option2 = abs(pos - r) + 1 + (r - l) + 1; 
        int minTime = min(option1, option2);
        cout << minTime << endl;
        return 0;
        

        }

        • 1

        信息

        ID
        2308
        时间
        1000ms
        内存
        256MiB
        难度
        7
        标签
        递交数
        540
        已通过
        132
        上传者