LOOP-Editorial

PROBLEM LINK:

Contest Division 1
Contest Division 2
Contest Division 3
Contest Division 4

Setter: Utkarsh Gupta
Tester: Manan Grover, Abhinav sharma
Editorialist: Devendra Singh

DIFFICULTY:

838

PREREQUISITES:

None

PROBLEM:

There is a circular track of length M consisting of M checkpoints and M bidirectional roads such that each road has a length of 1 unit.

Chef is currently at checkpoint A and wants to reach checkpoint B. Find the minimum length of the road he needs to travel.

EXPLANATION:

Let A\leq B (otherwise we can swap them). Chef has two choices now:
First Choice: Start moving in clockwise direction until he reaches B. Distance travelled = B-A
Second Choice: Start moving in anti-clockwise direction until he reaches B. Distance travelled = A+M-B
Thus the answer is min(B-A,A+M-B)

TIME COMPLEXITY:

O(1) for each test case.

SOLUTION:

Setter's solution
//Utkarsh.25dec
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vl vector <ll>
#define all(c) (c).begin(),(c).end()
using namespace std;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll modInverse(ll a){return power(a,mod-2);}
const int N=500023;
bool vis[N];
vector <int> adj[N];
long long readInt(long long l,long long r,char endd){
    long long x=0;
    int cnt=0;
    int fi=-1;
    bool is_neg=false;
    while(true){
        char g=getchar();
        if(g=='-'){
            assert(fi==-1);
            is_neg=true;
            continue;
        }
        if('0'<=g && g<='9'){
            x*=10;
            x+=g-'0';
            if(cnt==0){
                fi=g-'0';
            }
            cnt++;
            assert(fi!=0 || cnt==1);
            assert(fi!=0 || is_neg==false);

            assert(!(cnt>19 || ( cnt==19 && fi>1) ));
        } else if(g==endd){
            if(is_neg){
                x= -x;
            }

            if(!(l <= x && x <= r))
            {
                cerr << l << ' ' << r << ' ' << x << '\n';
                assert(1 == 0);
            }

            return x;
        } else {
            assert(false);
        }
    }
}
string readString(int l,int r,char endd){
    string ret="";
    int cnt=0;
    while(true){
        char g=getchar();
        assert(g!=-1);
        if(g==endd){
            break;
        }
        cnt++;
        ret+=g;
    }
    assert(l<=cnt && cnt<=r);
    return ret;
}
long long readIntSp(long long l,long long r){
    return readInt(l,r,' ');
}
long long readIntLn(long long l,long long r){
    return readInt(l,r,'\n');
}
string readStringLn(int l,int r){
    return readString(l,r,'\n');
}
string readStringSp(int l,int r){
    return readString(l,r,' ');
}
void solve()
{
    ll a,b,m;
    a=readInt(1,1000000000,' ');
    b=readInt(1,1000000000,' ');
    m=readInt(2,1000000000,'\n');
    assert(a>=1 && a<=m);
    assert(b>=1 && b<=m);
    assert(a!=b);
    if(a>b)
        swap(a,b);
    ll d1=(b-a);
    ll d2=a+m-b;
    cout<<min(d1,d2)<<'\n';
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    int T=readInt(1,1000,'\n');
    while(T--)
        solve();
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}
Editorialist's Solution
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define pb push_back
#define all(_obj) _obj.begin(), _obj.end()
#define F first
#define S second
#define pll pair<ll, ll>
#define vll vector<ll>
const int N = 1e5 + 11, mod = 1e9 + 7;
ll max(ll a, ll b) { return ((a > b) ? a : b); }
ll min(ll a, ll b) { return ((a > b) ? b : a); }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void sol(void)
{
    int a, b, m;
    cin >> a >> b >> m;
    if (a > b)
        swap(a, b);
    cout << min(b - a, m - b + a) << '\n';
    return;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    int test = 1;
    cin >> test;
    while (test--)
        sol();
}

1 Like

#include
#include
using namespace std;

int main() {
int T;
cin>>T;
while(T–)
{
int a,b,m,t1,t2;
cin>>a>>b>>m;
t1=abs(b-a);
t2=abs((m-b)+a);
if(t1<=t2)
cout<<t1<<endl;
else
cout<<t2<<endl;
}
return 0;
}

then what is the error in this code?

Because you have assumed that b > a.
Consider the test :
5 1 7
Your output : 4
Correct output : 3
Correction in your code : t3 = abs(m-a+b)
Now print min. of t1 ,t2 ,t3

1 Like

Check a > b instead of t1 > t2 … Refer to editorial

1 Like

thank you

int main() {
int T;
cin>>T;
while(T–)
{
int a,b,m,t1,t2,t3,temp;
cin>>a>>b>>m;
t1=abs(b-a);
t2=abs(m+a-b);
t3=abs(m-a+b);
if((t1<t2)&&(t1<t3))
cout<<t1<<endl;
if((t2<t1)&&(t2<t3))
cout<<t2<<endl;
if((t3<t1)&&(t3<t2))
cout<<t3<<endl;
}
return 0;
}
well now also its getting wrong on test case 2 (during submission) (1 & 3 are correct)

if((t1<=t2)&&(t1<=t3))
cout<<t1<<endl;
else if((t2<=t1)&&(t2<=t3))
cout<<t2<<endl;
else if((t3<=t1)&&(t3<=t2))
cout<<t3<<endl;

or you could’ve used

min({t1,t2,t3}) , please learn about the min function.

1 Like