0

I am receiving a runtime error in this code and I am not able to understand the reason.Can anyone help me out and could tell me the reason and the type of runtime error i am getting? I have been solving this question from codeforces,so here i am attaching the link of that submission so that you could see the actual test case in which i am getting this error.Here's the link https://codeforces.com/contest/1294/submission/113416390 .And here's the code

#include <bits/stdc++.h> // Include every standard library 
//#include <boost/math/common_factor.hpp>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
bool cmp(pair<int,int>&p1,pair<int,int>&p2)
{
    return (p1.first<=p2.first);
}
bool cmpp(pair<int,int>&p1,pair<int,int>&p2)
{
    return (p1.second<p2.second);
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  int t;
    cin >> t;
    
    while (t--) 
    {
      int n;
      cin>>n;
      string s;
      int x{},y{};
      int arr[n],brr[n];
      vector<pair<int,int>>vec;
      for(int i=0;i<n;i++)
      {
         
          cin>>arr[i]>>brr[i];
          vec.push_back({arr[i],brr[i]});
      }
        
      sort(vec.begin(),vec.end(),cmp);
      
    if(!is_sorted(vec.begin(),vec.end(),cmpp))
    cout<<"NO"<<endl;
    else
    {
        cout<<"YES"<<endl;
        sort(arr,arr+n);
        sort(brr,brr+n);
        for(int i=0;i<n;i++)
        {
          if(arr[i]!=x)
          s.append((arr[i]-x),'R');
          if(brr[i]!=y)
          s.append((brr[i]-y),'U');
          x=arr[i];
          y=brr[i];
        }
        cout<<s<<endl;
    }
    
    }
return 0;
}
  • 2
    `p1.first<=p2.first` does not adhere to the strict weak ordering requirement for a comparison function. Use `<`, not `<=`. https://stackoverflow.com/questions/979759/operator-and-strict-weak-ordering – Retired Ninja Apr 19 '21 at 01:55
  • Your `x` and `y` variables are not initialized or set to any value, so they will just take whatever value is already on their memory location. This could easily lead to negative amounts in the `s.append(...)`, which I expect is the cause of the runtime error. – olm Apr 19 '21 at 13:07

0 Answers0