Cod sursa(job #1555224)

Utilizator borcanirobertBorcani Robert borcanirobert Data 22 decembrie 2015 14:22:04
Problema Patrate 3 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.1 kb
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

ifstream fin("patrate3.in");
ofstream fout("patrate3.out");

const int MAX = 1005;
const double eps = 0.00001;
struct Point{
	double x, y;
};
Point a[MAX];
int N;
int nrp;
Point p1, p2, p3, p4;
double d1, d2;

void Read();
void NrPatrate();
bool BinarySearch( Point p, int ind );
bool Comp( const Point& a, const Point& b );
double Abs( double a );

int main()
{
	Read();
	NrPatrate();
	
	fout << nrp << '\n';
	
	fin.close();
	fout.close();
	return 0;
}

void Read()
{
	int i;
	
	fin >> N;
	for ( i = 1; i <= N; i++ )
		fin >> a[i].x >> a[i].y;
}

void NrPatrate()
{
	int i, j;
	
	sort( a + 1, a + 1 + N, Comp );
	
	for ( i = 1; i <= N; i++ )
		for ( j = i + 1; j <= N; j++ )
			if ( a[j].y - a[i].y > eps || Abs(a[j].y - a[i].y) <= eps )
			{
				d1 = a[j].x - a[i].x;
				d2 = a[j].y - a[i].y;
				
				p1 = a[i], p2 = a[j];
				p3.y = a[i].y - d1;
				p3.x = a[i].x + d2;
				p4.y = a[j].y - d1;
				p4.x = a[j].x + d2;
				
				cout << p3.x << ' ' << p3.y << '\n' << p4.x << ' ' << p4.y; cin.get();
				
				if ( BinarySearch(p3, j + 1) && BinarySearch(p4, j + 1) )
				{
					nrp++;
				}
			}
		/*	else
			{
				d1 = a[j].x - a[i].x;
				d2 = a[i].y - a[j].y;
				
				p1 = a[i], p2 = a[j];
				p3.y = a[i].y + d1;
				p3.x = a[i].x + d2;
				p4.y = a[j].y + d1;
				p4.x = a[j].x + d2;
				
				if ( BinarySearch(p3, j + 1) && BinarySearch(p4, j + 1) )
				{
					nrp++;
				}
			}	*/
}

bool BinarySearch( Point p, int ind )
{
	int st = ind, dr = N, mid;
	
	while ( st <= dr )
	{
		mid = ( st + dr ) / 2;
		//cout <<p.y - a[mid].y; cin.get();
		if ( p.x - a[mid].x > eps || ( Abs( p.x - a[mid].x <= eps ) && p.y - a[mid].y > eps ) )
		{
			st = mid + 1;
		}
		else
		{
			if ( Abs( p.x - a[mid].x ) <= eps && Abs( p.y - a[mid].y ) <= eps )
				return true;
			dr = mid - 1;
		}
	}
	
	//cout << st << ' ' << dr; cin.get();
	return false;
}

bool Comp( const Point& a, const Point& b )
{
	if ( b.x - a.x > eps || ( Abs(b.x - a.x) <= eps && b.y - a.y > eps ) )
		return true;
	return false;
}

double Abs( double a )
{
	if ( a > 0 )
		return a;
	return -a;
}