SPOJ - #26 BSHEEP - Build the Fence

905 views Asked by At
#include <bits/stdc++.h>
using namespace std;

#define EPS 1e-9

typedef double coord_t;
typedef double coord2_t;

struct point {
    double x, y;

    point(double _x, double _y)
    {
        x = _x, y = _y;
    }

    bool operator < (point p) const{
        if(fabs(x - p.x) > EPS)
            return x < p.x;
        return y < p.x;
    }
    bool operator == (point p) const{
        return fabs(x - p.x) < EPS && fabs (x - p.y) < EPS;
    }
};

coord2_t cross(const point &O, const point &A, const point &B)
{
    return (long)(A.x - O.x) * (B.y - O.y) - (long)(A.y - O.y) * (B.x - O.x);
}

bool cmp(point a, point b)
{
    if(fabs(a.y - b.y) > EPS)
        return a.y < b.y;
    return a.x < b.x;
}

vector<point> convex_hull(vector<point> P)
{
    int n = P.size();
    vector<point> H;

    sort(P.begin(), P.end(), cmp);

    for (int i = 0; i < n; ++i)
    {
        while(H.size() >= 2 && cross(H[H.size() - 2], H[H.size() - 1], P[i]) <= 0)
            H.pop_back();
        H.push_back(P[i]);
    }

    int l = H.size() + 1;
    for (int i = n - 1; i >= 0; i--)
    {
        while(H.size() >= l && cross(H[H.size() - 2], H[H.size() - 1], P[i]) <= 0)
            H.pop_back();
        H.push_back(P[i]);
    }
    return H;
}

int main()
{
    int tc, n, x, y;
    double length;
    vector<point> P;

    scanf("%d", &tc);

    while(tc--)
    {
        length = 0;
        P.clear();
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d %d", &x, &y);
            P.push_back(point(x, y));
        }

        P = convex_hull(P);

        for (int i = 0; i < (int) P.size() - 1; i++) {
            length += sqrt(pow((P[i].x - P[i+1].x),2) + pow((P[i].y - P[i+1].y),2));
        }
        printf("%.2lf\n", length);

        for (int i = 1; i < (int) P.size(); i++) {
            printf("%lf ", P[i]);      // Problem in this line , can't print the required output
        }
        printf("\n");

    }
    return 0;
}

It's a convex hull problem and I think I have done everything alright, but can't output p1 p2 .... pk of the problem. The problem is here:

At the beginning of spring all the sheep move to the higher pastures in the mountains. If there are thousands of them, it is well worthwhile gathering them together in one place. But sheep don't like to leave their grass-lands. Help the shepherd and build him a fence which would surround all the sheep. The fence should have the smallest possible length! Assume that sheep are negligibly small and that they are not moving. Sometimes a few sheep are standing in the same place. If there is only one sheep, it is probably dying, so no fence is needed at all...

Input

t [the number of tests <= 100]

[empty line]

n [the number of sheep <= 100000]

x1 y1 [coordinates of the first sheep] ... xn yn

[integer coordinates from -10000 to 10000]

[empty line]

[other lists of sheep]

Text grouped in [ ] does not appear in the input file. Assume that sheep are numbered in the input order.

Output

o [length of circumference, 2 digits precision]

p1 p2 ... pk [the sheep that are standing in the corners of the fence; the first one should be positioned bottommost and as far to the left as possible, the others ought to be written in anticlockwise order; ignore all sheep standing in the same place but the first to appear in the input file; the number of sheep should be the smallest possible]

[empty line]

[next solutions]

1

There are 1 answers

0
Baqir Khan On

In your struct, add one more variable while will hold the position of the point.

struct point {
    double x, y, c;

    point(double _x, double _y, double _c)
    {
        x = _x, y = _y,c = _c;
    }

    bool operator < (point p) const{
        if(fabs(x - p.x) > EPS)
            return x < p.x;
        return y < p.x;
    }
    bool operator == (point p) const{
        return fabs(x - p.x) < EPS && fabs (x - p.y) < EPS;
    }
};

When you take input, pushback all three of them:

for(int i = 0; i < n; i++)
    {
        scanf("%d %d", &x, &y);
        P.push_back(point(x, y,i+1));
     }