538C. Tourist's Notes - Codeforces Solution C++

  Problem Link : 538C. Tourist's Notes 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
 
#define ll long long int
 
int main()
{
  ll n,m,d,h,ans=0;
  cin>>n>>m;
  pair<ll,ll> arr[m];
  for(ll i=0;i<m;i++)
  {
    cin>>d>>h;
    arr[i]=make_pair(d,h);
    ans=max(ans,h);
  }
 
  ans=max(ans,arr[0].second+arr[0].first-1);
  ans=max(ans,arr[m-1].second+n-arr[m-1].first);
    bool flag=0;
  for(ll i=0;i<m-1;i++)
  {
      if(abs(arr[i+1].second-arr[i].second) > arr[i+1].first-arr[i].first)
      {
          flag=1;
          break;
      }
    ans=max(ans,(arr[i+1].first-arr[i].first+arr[i+1].second+arr[i].second)/2);
  }
  if(flag)
      cout<<"IMPOSSIBLE";
  else    
      cout<<ans;
 
}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!