Cod sursa(job #1494411)

Utilizator FairPlay94George Cioroiu FairPlay94 Data 1 octombrie 2015 00:22:55
Problema Trapez Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <cstdio>
#include <iostream>
#include <set>
#include <climits>
#include <map>

using namespace std;

int modul(int a) {
    return (a < 0) ? -a : a;
}

int gcd(int a, int b) {
    if (a == 0) {
        return b;
    }
    return gcd(b % a, a);
}

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

    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    int x[1005], y[1005];
    for (int i = 1; i <= n; i++) {
        cin >> x[i] >> y[i];
    }
    map<int, map<int, int> > m;
    for (int i = 1; i < n; i++) {
        for (int j = i + 1; j <= n; j++) {
            int yc = y[i] - y[j], xc = x[i] - x[j], d = gcd(modul(yc), modul(xc));
            yc /= d;
            xc /= d;
            if (xc < 0) {
                xc = -xc;
                yc = -yc;
            }
            m[yc][xc]++;
        }
    }
    int ans = 0;
    for (map<int, map<int, int> >::iterator it = m.begin(); it != m.end(); it++) {
        for (map<int, int>::iterator jt = it->second.begin(); jt != it->second.end(); jt++) {
            ans += (jt->second) * (jt->second - 1) / 2;
        }
    }
    cout << ans;

    return 0;
}