Cod sursa(job #1720757)

Utilizator SburlyAndrei Florin Sburly Data 23 iunie 2016 14:15:11
Problema Trapez Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
/********************
    Created by Sburly
********************/
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

#define err 0.000001

unsigned int n;


vector<double> x(1000);
vector<double> y(1000);
vector<double> p(1000000);

inline bool aprox(double a, double b)
{
    if(-err < a-b && a-b < err)
        return true;
    return false;
}

int main()
{
    freopen("trapez.in","r",stdin);
    freopen("trapez.out","w",stdout);

    scanf("%u\n",&n);

    for(unsigned int i = 0; i != n; ++i)
    {
        scanf("%lu %lu\n", &x[i],&y[i]);
    }

    unsigned int k = 0;
    unsigned int l = 0;
    for(unsigned int i = 0; i != n-1; ++i)
    {
        for(unsigned int j = i+1; j != n; ++j)
        {
            if(x[j] == x[i])
            {
                l++;
            }
            else
            {
                p[k++] = (y[j]-y[i])/(x[j]-x[i]);
            }
        }
    }

    sort(p.begin(), p.begin()+k);

    unsigned int sol = l*(l-1)/2;
    l = 1;
    for(unsigned int i = 0; i != k-1; ++i)
    {
        if(aprox(p[i],p[i+1]))
            l++;
        else
        {
            sol+=l*(l-1)/2;
            l=1;
        }
    }
    printf("%u",sol);

    return 0;
}