Cod sursa(job #2876456)

Utilizator YusyBossFares Yusuf YusyBoss Data 23 martie 2022 11:48:47
Problema Trapez Scor 20
Compilator cpp-64 Status done
Runda masonii Marime 1.27 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#define NMAX 1000

using namespace std;

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

map <pair<int, int>, int> mp;
pair <int, int> v[NMAX + 1];
vector <pair<int, int>> vfractii;

bool cmp(pair <int, int> A, pair <int, int> B) {
  return (long long)A.first * B.second < (long long)A.second * B.first;
}

bool is_equal(pair <int, int> A, pair <int, int> B) {
  return (long long)A.first * B.second == (long long)A.second * B.first;
}

int main() {
  int n, i, j, sol, a, b, gcd, val, cnt;
  cin >> n;

  for (i = 0; i < n; i++)
    cin >> v[i].first >> v[i].second;

  cnt = 0;
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      a = v[i].second - v[j].second;
      b = v[i].first - v[j].first;

      if (b == 0)
        cnt++;
      else
        vfractii.push_back({a, b});
    }
  }

  sol = 0;
  if (cnt == 2)
    sol++;

  sort(vfractii.begin(), vfractii.end(), cmp);
  cnt = 1;
  for (i = 1; i < vfractii.size(); i++) {
    if (is_equal(vfractii[i], vfractii[i - 1]))
      cnt++;
    else {
      sol += cnt * (cnt - 1) / 2;
      cnt = 1;
    }
  }

  sol += cnt * (cnt - 1) / 2;

  cout << sol;
  return 0;
}