Cod sursa(job #2067737)

Utilizator Andreiii500Andrei Puiu Andreiii500 Data 16 noiembrie 2017 19:57:39
Problema Trapez Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
#include<bits/stdc++.h>
#include<iostream>
#include<fstream>
using namespace std;

ifstream in("trapez.in");
ofstream out("trapez.out");

const double INF = 2000000001;
const int N_MAX = 1000;
int n;

double calcSlope(pair<int, int> point1, pair<int, int> point2){
    double vertical_distance = point2.second - point1.second;
    double horizontal_distance = point2.first - point1.first;

    if(horizontal_distance == 0) /// Don't divide by 0!
    {
        if(vertical_distance < 0)
            return -INF;
        else
            return INF;
    }

    /// Return the tangent of the two points
    return vertical_distance / horizontal_distance;
}

int main()
{
    unordered_map<double, int> slopeCount;
    pair<int, int> point[N_MAX];

    in>>n;
    for(int i=0; i<n; ++i)
        in>>point[i].first>>point[i].second;

    for(int i=0; i<n; ++i)
        for(int j=i+1; j<n; ++j)
        {
            double crtSlope = calcSlope(point[i], point[j]);
            ++slopeCount[crtSlope];
        }

    long long answer = 0;
    for(unordered_map<double, int>::iterator it = slopeCount.begin(); it != slopeCount.end(); ++it)
    {
        ///cout<<it->first<<" "<<it->second<<"\n";
        answer += (long long)(it->second) * (it->second-1) / 2;
    }
    out<<answer;



    return 0;
}