Cod sursa(job #2349694)

Utilizator claudiu.gatinaFMI Claudiu Gatina claudiu.gatina Data 20 februarie 2019 17:20:54
Problema Trapez Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <map>

using namespace std;

fstream f("trapez.in", ios::in);
fstream g("trapez.out", ios::out);

struct point
{
  int x, y;
  point(int a, int b) : x(a), y(b) {}
  point() {}
};

int n;
point v[1001];
map<pair<int, int>, int> slope;

void read()
{
  f >> n;
  for(int i = 0; i < n; ++i)
    f >> v[i].x >> v[i].y;
}

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

void reduce(int &a, int &b)
{
  int d = gcd(a, b);
  a /= d;
  b /= d;
}

void solve()
{
  for(int i = 0; i < n; ++i)
    for(int j = i + 1; j < n; ++j)
    {
      int mx = v[i].x - v[j].x;
      int my = v[i].y - v[j].y;
      if(mx < 0)
        mx = -mx;
      if(my < 0)
        my = -my;
      reduce(mx, my);
      pair<int, int> p = make_pair(mx, my);
      slope[p]++;
    }
  map<pair<int, int>, int>::iterator it;
  int res = 0;
  for(it = slope.begin(); it != slope.end(); ++it)
  {
    int num = it->second;
    res += num * (num - 1) / 2;
  }
  g << res;
}

int main()
{
  read();
  solve();

  return 0;
}