Cod sursa(job #1479727)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 1 septembrie 2015 01:20:49
Problema Triang Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.08 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <unordered_set>
#include <cmath>
using namespace std;

constexpr double epsilon = 1e-3;

double eq(const double a, const double b){
	return a-epsilon <= b && b <= a+epsilon; }

struct point{
	double x, y;
	point(): x(0), y(0){}
	point(const double X, const double Y):
		x(X), y(Y){}
	bool operator==(const point& rhs)const{
		return eq(x, rhs.x) && eq(y, rhs.y); } };

point operator+(const point a, const point b){
	return point(a.x + b.x, a.y + b.y); }

point operator-(const point a, const point b){
	return point(a.x - b.x, a.y - b.y); }

point operator*(const point a, const double b){
	return point(a.x * b, a.y * b); }

point operator/(const point a, const double b){
	return point(a.x / b, a.y / b); }

constexpr double sqrt3_pe_2 = 0.86602;

point rotate_60(const point a){
	return point(a.x/2 - a.y * sqrt3_pe_2, a.x * sqrt3_pe_2 + a.y/2); }

point rotate_minus_60(const point a){
	return point(a.x/2 + a.y * sqrt3_pe_2, -a.x * sqrt3_pe_2 + a.y/2); }

namespace std{
    template <>
    struct hash<point>{
        size_t operator()(const point& p)const{
            hash<int> hs;
            return hs(int(p.x/epsilon)) << 3 + hs(int(p.y/epsilon)); } }; }

ostream& operator<<(ostream& lhs, const point& rhs){
	lhs << rhs.x << ' ' << rhs.y;
	return lhs; }

constexpr double PI = 3.1415;

pair<point, point> make_pts(const point a, const point b){
	const point r = b-a, v1 = rotate_60(r), v2 = rotate_minus_60(r);
	return  make_pair(a + v1, a + v2); }

int main(){
	ifstream f("triang.in");
	ofstream g("triang.out");
	int n;
	f >> n;
	vector<point> puncte;
	puncte.reserve(n);
	unordered_set<point> hash_puncte;
	hash_puncte.reserve(n);
	for(int i = 0; i < n; ++i){
		double x, y;
		f >> x >> y;
		puncte.emplace_back(x, y);
		hash_puncte.emplace(x, y); }
	int rez = 0;
	for(int i = 0; i+2 < n; ++i){
		for(int j = 0; j+1 < n; ++j){
			if(i != j){
				const auto pts = make_pts(puncte[i], puncte[j]);
				rez += hash_puncte.count(pts.first);
				rez += hash_puncte.count(pts.second); } } }
	g << rez;
	return 0; }