Cod sursă (job #77723)

Utilizator avatar anarogoz Ana Rogoz anarogoz IP ascuns
Problemă Înfășurătoare convexă Compilator cpp | 1,71 kb
Rundă Tema 2 clasele 11-12 2014/15 Status evaluat
Dată 8 oct. 2014 12:22:56 Scor 63
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int VMAX = 100005;
class st {
public:
    int x, y;
};

st s[VMAX], aux;
int v[VMAX];

long long dist(int x1, int y1, int x2, int y2)
{
    return ((long long)(x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}

int ccw (st a, st b, st c) {
    long long cp;
    cp = (long long)((b.x - a.x) * (c.y - a.y)) - (long long)((b.y - a.y) * (c.x - a.x));
    if (abs(cp) == 0)
        return 0;
    if (cp > 0)
        return 1;
    return -1;
}

bool cmp (st a, st b) {
    if(ccw(s[0],a,b)==0)
        return (dist(s[0].x,s[0].y,a.x,a.y) < dist(s[0].x,s[0].y,b.x,b.y));
    if (ccw(s[0], a, b) > 0)
        return true;
    else
        return false;
}

int main() {

    int n, i, top;

    freopen ("infasuratoare.in", "r", stdin);
    freopen ("infasuratoare.out", "w", stdout);
    scanf("%d", &n);
    scanf("%d%d", &s[0].x, &s[0].y);
    for (i = 1; i < n; i++) {
        scanf("%d%d", &s[i].x, &s[i].y);
        if ((s[i].y - s[0].y) < 0) {
            aux = s[0];
            s[0] = s[i];
            s[i] = aux;
        }
        else
        if (abs(s[i].y - s[0].y) == 0) {
            if (s[i].x - s[0].x < 0) {
                aux=s[0];
                s[0]=s[i];
                s[i]=aux;
            }
        }
    }
    sort(s + 1, s + n, cmp);
    v[0] = 0;
    v[1] = 1;
    top = 1;
    s[n] = s[0];
    i = 2;
    while(i <= n) {
        while(top >= 1 && ccw(s[v[top - 1]], s[v[top]], s[i]) <= 0) {
            top--;
        }
        v[++top] = i;
        i++;
    }
    printf("%d\n", top);
    for(i = 0; i < top; i++)
        printf("%d %d\n", s[v[i]].x, s[v[i]].y);
    return 0;
}