I am getting a runtime error (SIGSEGV) on my code and I don’t know how to fix it. Please help. Thanks.
Problem: http://www.codechef.com/problems/POINTS
My Solution: http://www.codechef.com/viewsolution/3127090
My Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
typedef pair<int, int> Coord;
bool second_descend(const pair<int, int>& i, const pair<int, int>& j) {
if (i.second < j.second) return false;
if (j.second < i.second) return true;
return j.first < i.first;
}
float get_distance(Coord a, Coord b){
return sqrt(pow(b.first - a.first, 2) + pow(b.second - a.second , 2));
}
int main() {
int t, n;
Coord coordinate_template;
cin >> t;
while (t--){
vector<Coord> points;
cin >> n;
while (n--){
cin >> coordinate_template.first >> coordinate_template.second;
points.push_back(coordinate_template);
}
sort(points.begin(), points.end());
for (int a = 0; a < points.size()-2; a++){
if (points[a].first == points[a + 1].first){
int counter = a + 1;
while (points[counter].first == points[a].first){
counter++;
}
sort(points.begin() + a, points.begin() + counter, second_descend);
}
}
float sum = 0;
for (int a = 0; a < points.size()-1; a++){
sum += get_distance(points[a], points[a + 1]);
}
cout << floor(sum * 100 + 0.5) / 100 << endl;
}
return 0;
}