Cod sursa(job #1805888)

Utilizator Vally77FMI Calinescu Valentin Gelu Vally77 Data 14 noiembrie 2016 16:57:37
Problema Trapez Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.15 kb
//#include <iostream>
#include <fstream>
#include <algorithm>
#include <math.h>
using namespace std;
ifstream ka("trapez.in");
ofstream ki("trapez.out");

const int N_MAX = 1000;

long long patrat(long long t)
{
    return t * t;
}

struct punct
{
    long long x, y;
}puncte[N_MAX + 1];

struct dreapta
{
    punct p1, p2;
    bool operator < (const dreapta arg) const
    {
        if((p1.y - p2.y) * (arg.p1.x - arg.p2.x) == (p1.x - p2.x) * (arg.p1.y - arg.p2.y))
            return sqrt(patrat(p1.y - p2.y) + patrat(p1.x - p2.x)) - sqrt(patrat(arg.p1.y - arg.p2.y) + patrat(arg.p1.x - arg.p2.x)) < 1e-7;
        else
            return (p1.y - p2.y) * (arg.p1.x - arg.p2.x) < (p1.x - p2.x) * (arg.p1.y - arg.p2.y);
    }
    bool operator == (const dreapta arg) const
    {
        return (p1.y - p2.y) * (arg.p1.x - arg.p2.x) == (p1.x - p2.x) * (arg.p1.y - arg.p2.y);
    }
}drepte[N_MAX * N_MAX + 1];

int n, marime;
long long sol;

double distanta(int i)
{
    return sqrt(patrat(drepte[i].p1.y - drepte[i].p2.y) + patrat(drepte[i].p1.x - drepte[i].p2.x));
}

int main()
{
    ka >> n;
    for(int i = 1; i <= n; i++)
        ka >> puncte[i].x >> puncte[i].y;
    for(int i = 1; i <= n; i++)
    {
        for(int j = i + 1; j <= n; j++)
        {
            dreapta d;
            d.p1 = puncte[i];
            d.p2 = puncte[j];
            drepte[++marime] = d;
        }
    }
    sort(drepte + 1, drepte + marime + 1);
    int cand = 1;
    //for(int i = 1; i <= marime; i++)
        //cout << drepte[i].p1.x << " " << drepte[i].p1.y << " " << drepte[i].p2.x << " " << drepte[i].p2.y << '\n';
    //cout << '\n';
    for(int i = 1; i <= marime; i++)
    {
        if(i > 1 && !(drepte[i] == drepte[i - 1]))
            cand = i;
        int prev = cand;
        while(prev <= n && drepte[prev] == drepte[i])
        {
            //cout << drepte[i].p1.x << " " << drepte[i].p1.y << " " << drepte[i].p2.x << " " << drepte[i].p2.y << "  " << drepte[prev].p1.x << " " << drepte[prev].p1.y << " " << drepte[prev].p2.x << " " << drepte[prev].p2.y << '\n';
            if(i != prev && distanta(prev) >= distanta(i))
                sol++;
            prev++;
        }
    }
    ki << sol;
}