Cod sursa(job #1417434)

Utilizator depevladVlad Dumitru-Popescu depevlad Data 10 aprilie 2015 12:31:07
Problema Trapez Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

#define inFile "trapez.in"
#define outFile "trapez.out"
#define MAX_N 1001
#define ERR 1e-7
#define INF 1<<30

FILE *in = fopen(inFile, "r");
FILE *out = fopen(outFile, "w");

struct Point {
    double x;
    double y;
};

struct Line {
    Point A;
    Point B;
    double slope;
};

Point P[MAX_N];
Line L[MAX_N * MAX_N];

bool Equal(double A, double B) {
    return fabs(A-B) < ERR;
}

bool Lesser(double A, double B) {
    return B-A > ERR;
}

bool sortSlope(Line A, Line B) {
    return Lesser(A.slope, B.slope);
}

int main() {
    int N, nL = 0, i, j, sameSlope;
    long long ANS = 0;

    fscanf(in, "%d", &N);
    for(i = 1; i <= N; i++) {
        fscanf(in, "%d %d", &P[i].x, &P[i].y);
    }

    for(i = 1; i <= N; i++) {
        for(j = i+1; j <= N; j++) {
            ++nL;
            L[nL].A = P[i];
            L[nL].B = P[j];
            if(L[nL].B.x == L[nL].A.x) L[nL].slope = INF;
            else L[nL].slope = atan((L[nL].B.x - L[nL].A.x) / (L[nL].B.y - L[nL].A.y));
        }
    }
    sort(L+1, L+nL+1, sortSlope);

    for(i = 1; i <= nL; i++) {
        j = i;
        sameSlope = 0;
        while(L[i].slope == L[j].slope) {
            sameSlope++;
            i++;
        }
        i--;
        ANS += (sameSlope-1) * sameSlope / 2;
    }

    fprintf(out, "%lld\n", ANS);

    fclose(in);
    fclose(out);

    return 0;
}