Cod sursa(job #2221317)

Utilizator PruteanuTheoPruteanu Theodor PruteanuTheo Data 13 iulie 2018 18:09:44
Problema Trapez Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

const int inf = (1LL << 31) - 1;
const double e = 1e-14;

class point
{
private :
    int x, y;
public :
    point() { x = y = 0; }
    point(int a, int b) { x = a, y = b; }
    void set(int a, int b) { x = a, y = b; }
    double panta(point &other) { if(x == other.x) return inf; return (1.0 * (y - other.y) / (x - other.x)); }
};

vector<point> v;
vector<double> p;

int main()
{
    freopen("trapez.in", "r", stdin);
    freopen("trapez.out", "w", stdout);
    int n, x, y, cnt = 1;
    long long ans = 0;
    point tmp;
    double a, b;
    scanf("%d", &n);
    for(short i = 0; i < n; i++)
    {
        scanf("%d%d", &x, &y);
        tmp.set(x, y);
        v.push_back(tmp);
    }
    for(int i = 0; i < n - 1; i++)
        for(short j = i + 1; j < n; j++)
         p.push_back(v[i].panta(v[j]));
    sort(p.begin(), p.end());
    a = p[0];
    for(int i = 1; i < p.size(); i++)
    {
        b = p[i];
        if(fabs(a - b) < e) cnt++;
        else ans += cnt * (cnt - 1) / 2, cnt = 1, a = b;
    }
    ans += cnt * (cnt - 1) / 2;
    printf("%lld", ans);
    return 0;
}