Cod sursa(job #2876304)

Utilizator mateitudordmDumitru Matei mateitudordm Data 23 martie 2022 10:40:03
Problema Trapez Scor 50
Compilator cpp-64 Status done
Runda masonii Marime 1.41 kb
#include <fstream>
#include <map>
#include <algorithm>
#define nmax 1000
#define inf 2000000000

using namespace std;

struct pct
{
    int x, y;
};
bool cmp (pct a, pct b)
{
    if (a.x == b.x)
        return a.y < b.y;
    return a.x < b.x;
}
struct panta
{
    int numa, numi;
    bool operator< (const panta& a)const
    {
        if (numa == a.numa)
            return numi < a.numi;
        return numa < a.numa;
    }
};
pct v[nmax + 1];
map<panta, int> f;

int main()
{
    ifstream cin ("trapez.in");
    ofstream cout ("trapez.out");
    int n, i, j, act, gcd;
    long long tot = 0;
    panta x;
    cin >> n;
    for (i = 1; i <= n; i++)
        cin >> v[i].x >> v[i].y;
    sort (v + 1, v + n + 1, cmp);
    for (i = 1; i <= n; i++)
        for (j = i + 1; j <= n; j++)
        {
            if (v[j].x - v[i].x == 0)
                x = {inf, inf};
            else
            {
                gcd = __gcd (v[j].y - v[i].y, v[j].x - v[i].x);
                if (gcd == 0)
                    gcd = 1;
                x = { (v[j].y - v[i].y) / gcd, (v[j].x - v[i].x) / gcd};
                if (x.numa < 0 && x.numi < 0)
                    x.numa *= -1, x.numi *= -1;
                else if (x.numi < 0)
                    x.numa *= -1, x.numi *= -1;
            }
            act = ++f[x];
            tot += act - 1;
        }
    cout << tot;
    return 0;
}