Cod sursă (job #77709)

Utilizator avatar anarogoz Ana Rogoz anarogoz IP ascuns
Problemă Înfășurătoare convexă Compilator cpp | 1,77 kb
Rundă Tema 2 clasele 11-12 2014/15 Status evaluat
Dată 8 oct. 2014 11:56:38 Scor 36
#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];

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

bool cmp (st x, st y) {
    if (ccw(s[0], x, y) > 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;
    s[n] = s[0];
    top = 1;
    i = 2;
    while(i <= n) {
 /*       if (ccw(s[v[top - 1]], s[v[top]], s[i]) > 0) {
            v[++top] = i;
            i++;
        }
        else {
            if(top > 1)
                top--;
            else {
                v[top] = i;
                i++;
            }
        }*/
        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;
}