Cod sursa(job #2304437)

Utilizator florin_salamFlorin Salam florin_salam Data 18 decembrie 2018 00:48:55
Problema Trapez Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <algorithm>

#define mp make_pair

using namespace std;

const int NMAX = 1e3;

int n;
pair <int, int> v[NMAX + 5];
double a[NMAX * NMAX + 5];

inline int gcd(int a, int b)
{
	return b ? gcd(b, a % b) : a;
}

int main()
{
	ifstream fin("trapez.in");
	ofstream fout("trapez.out");
	fin >> n;
	int x, y, p = 0;
	for (int i = 1;i <= n;++i)
	{
		fin >> x >> y;
		v[i] = mp(x, y);
	}
	long long ans = 0;
	for (int i = 1;i < n;++i)
		for (int j = i + 1;j <= n;++j)
		{
			double currSlope;
			if (v[j].first - v[i].first != 0)
			{
				currSlope = (double)(v[j].second - v[i].second) / (v[j].first - v[i].first);
				a[++p] = currSlope;
			}
			else
				++ans;

		}
	ans = ans * (ans - 1) / 2;
	sort(a + 1, a + p + 1);
	int k = 1;
	for (int i = 2;i <= p + 1;++i)
	{
		if (a[i] == a[i - 1])
			++k;
		else
		{
			ans += 1LL * k * (k - 1) / 2;
			k = 1;
		}
	}
	fout << ans << "\n";
	fin.close();
	fout.close();
	return 0;
}