Cod sursa(job #2455733)

Utilizator MaraPMara P MaraP Data 12 septembrie 2019 16:46:45
Problema Trapez Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.78 kb
#include <iostream>
#include <fstream>
#include <map>
#include <algorithm>
using namespace std;

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

pair<int,int> drepte[100000];

int n;
struct punct
{
    int x,y;
    void citire()
    {
        fin>>x>>y;
    }
}Puncte[1001];

int cmmdc(int a, int b)
{
    while(b)
    {
        int r=a%b;
        a=b;
        b=r;
    }
    return a;
}

void simplifica(pair<int,int> &p)
{
    int cmm;
    if(p.first>=0&&p.second>=0)
        cmm=cmmdc(p.first,p.second);
    if(p.first<0&&p.second<0)
    {
        p.first*=-1;
        p.second*=-1;
        cmm=cmmdc(p.first,p.second);
    }
    else if(p.first<0)
        cmm=cmmdc(p.first*-1,p.second);
    else if(p.second<0)
    {
        cmm=cmmdc(p.first,p.second*-1);
        p.second*=-1;
        p.first*=-1;
    }
    p.first/=cmm;
    p.second/=cmm;
}
int cmb(int x)
{
    return x*(x-1)/2;
}
bool compar(pair<int,int> x, pair<int,int> y)
{
    if(x.first*y.second<x.second*y.first)
        return false;
    return true;
}
void solve()
{
    fin>>n;
    int k=0;
    for(int i=0;i<n;i++)
        Puncte[i].citire();
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)
        {
            pair<int,int> panta;
            panta.first=Puncte[i].y-Puncte[j].y;
            panta.second=Puncte[i].x-Puncte[j].x;
            simplifica(panta);
            drepte[k++]=panta;
        }
    sort(drepte,drepte+k,compar);
    int suma=0;
    int numar_curent=1;
    for(int i=1;i<k;i++)
    {
        if(drepte[i]==drepte[i+1])
            numar_curent++;
        else
        {
            suma+=cmb(numar_curent);
            numar_curent=1;
        }
    }
    fout<<suma;
}
int main()
{
    solve();
    return 0;
}