Here is the problem which I am solving ECJAN20I Problem - CodeChef
Following is my code:
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
struct point
{
int x, y;
};
point p0;
int dist_sq(point p)
{
return (p.x-p0.x)*(p.x-p0.x) + (p.y-p0.y)*(p.y-p0.y);
}
bool cmp(point p, point q)
{
int val = (p.y-p0.y)*(q.x-p0.x) - (q.y-p0.y)*(p.x-p0.x);
if(val == 0) return dist_sq(p) < dist_sq(q);
else return (val < 0);
}
int orientation(point p, point q, point r)
{
int val = (q.x-p.x)*(r.y-p.y) - (q.y-p.y)*(r.x-p.x);
if(val == 0) return 0; //collinear
else return (val < 0) ? 1 : 2; //1->cw and 2->ccw
}
int nextToTop(stack<int>& S)
{
int p = S.top();
S.pop();
int q = S.top();
S.push(p);
return q;
}
int convexHull(vector<point>& arr)
{
int n = arr.size();
int l = 0;
for(int i=0; i<n; i++)
{
if((arr[i].y < arr[l].y)||(arr[i].y == arr[l].y && arr[i].x < arr[l].x))
l = i;
}
p0 = arr[l];
sort(arr.begin(), arr.end(), cmp);
stack<int> v;
v.push(0);
v.push(1);
for(int i=2; i<n; i++)
{
while(v.size() != 1 && orientation(arr[nextToTop(v)],arr[v.top()],arr[i]) != 2)
v.pop();
v.push(i);
}
int cnt = 0;
while(!v.empty())
{
v.pop();
cnt++;
}
return cnt;
}
int main()
{
FASTIO
int t;
cin >> t;
while(t--)
{
int n, r;
cin >> n >> r;
vector<point> arr(n);
for(int i=0; i<n; i++)
cin >> arr[i].x >> arr[i].y;
bool res = (convexHull(arr) <= r);
if(res) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
I have tried my code for the sample test case provided with the problem, along with some other test cases that I came up with. But on submitting my code I keep getting Runtime Error (SIGSEGV), and even SIGABRT sometimes. Can anyone please help me in resolving this issue?