Problem Author: | Anna Yarosh (Anna_Yarosh) |
Prepared by: | Andriy Kholod (Connect) |
Analysis: | Andriy Kholod (Connect) |
Solution
Since the train numbering from different sides is symmetrical with respect to the center, it is obvious that the most advantageous option will be when \frac{n+1}{2}. Then we only need to calculate the possible movement when \frac{n+1}{2}.
The asymptotic complexity of the solution is .
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define endl '\n'
void solve() {
ll n , m , k;
cin>>n>>k;
k = min(k, n - k + 1);
ll mid = 0;
if (n%2) mid = (n+1)/2;
else mid = n/2 + 1;
cout<<abs(k - mid)<<endl;
}
signed main() {
speed;
int test;
test = 1;
//cin>>test;
while (test--) solve();
return 0;
}