Cod sursa(job #1773343)

Utilizator ArkinyStoica Alex Arkiny Data 7 octombrie 2016 19:05:28
Problema Triang Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.1 kb
#include<fstream>
#include<algorithm>
#include<unordered_map>
#include<math.h>
using namespace std;

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

#define EPS 0.01

#define x first
#define y second

struct pair_hash {
	template <class T1, class T2>
	std::size_t operator () (const std::pair<T1, T2> &p) const {
		auto h1 = std::hash<T1>{}(floor(p.first));
		auto h2 = std::hash<T2>{}(floor(p.second));

		// Mainly for demonstration purposes, i.e. works but is overly simple
		// In the real world, use sth. like boost.hash_combine
		return h1*h2;
	}
};

class EqualFn
{
public:
	bool operator() (pair<long double, long double> const& t1, pair<long double, long double> const& t2) const
	{
		return fabs(t1.x - t2.x) < EPS && fabs(t1.y - t2.y) < EPS;
	}
};

typedef pair<long double, long double> POINT2D;

POINT2D points[2000];

unordered_map<POINT2D,int,pair_hash,EqualFn> mp;

int main()
{

	int N;

	in >> N;

	for (int i = 1;i <= N;++i)
		in >> points[i].x >> points[i].y,mp[POINT2D(points[i].x,points[i].y)]=1;
	int nr = 0;
	for (int i = 1;i <= N;++i)
		for (int j = i + 1;j <= N;++j)
		{
			long double xm=(points[i].x + points[j].x)/2, ym = (points[i].y + points[j].y) / 2;

			long double x1, y1, x2, y2;

			long double distance = (points[j].x - points[i].x)*(points[j].x - points[i].x) + (points[j].y - points[i].y)*(points[j].y - points[i].y);

			long double slope;
			
			if (fabs(points[j].y - points[i].y) < EPS)
			{
				x1 = x2 = xm;
				y1 = ym + sqrt(3)*sqrt(distance) / 2;
				y2 = ym - sqrt(3)*sqrt(distance) / 2;
			}
			else
			{
				if (fabs(points[j].x - points[i].x) < EPS)
				{
					slope = 0;
				}
				else
				{
					slope = (points[j].y - points[i].y) / (points[j].x - points[i].x);
					slope = -1 / slope;
				}

				x1 = (2 * xm + sqrt(3* distance / (slope*slope + 1)))/2;
				x2 = (2 * xm - sqrt(3* distance / (slope*slope + 1)))/2;

				y1 = x1*slope + ym;
				y2 = x2*slope + ym;

			}

			if (mp[POINT2D(x1, y1)] == 1)
				++nr;
			if (mp[POINT2D(x2, y2)] == 1)
				++nr;

			
		}

	out << nr;

	return 0;
}