Cod sursa(job #1348021)

Utilizator BLz0rDospra Cristian BLz0r Data 19 februarie 2015 14:14:37
Problema Patrate 3 Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 1.44 kb
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
 
#define Nmax 1002
#define eps 1.0 / 1e9
 
FILE *f = fopen ( "patrate3.in", "r" );
FILE *g = fopen ( "patrate3.out", "w" );

struct point{
    double x, y;
} p[Nmax];
 
int N;
 
bool cmp ( point a, point b ){
   
	if ( fabs ( a.x - b.x ) <= eps )
        return a.y < b.y;
    else
        return a.x < b.x;
}
 
bool match ( point a, point b ){
	
	if ( fabs ( a.x - b.x ) <= eps && fabs ( a.y - b.y ) <= eps )
		return 1;
	return 0;
}

bool bSearch ( point T ){
    int st = 1, dr = N, mid;
    
    while ( st <= dr ){
        mid = ( st + dr ) >> 1;
		
        if ( match ( p[mid] , T ) )
            return 1;
        else{
			if ( cmp ( T , p[mid] ) )
				dr = mid - 1;
			else
				st = mid + 1;
		}
    }
    return 0;
}
 
int main(){
	int sol = 0;
	point a, b;
    
	fscanf ( f, "%d", &N );
    
    for ( int i = 1; i <= N; ++i )
		fscanf ( f, "%lf%lf", &p[i].x, &p[i].y );
 
    sort ( p + 1, p + N + 1, cmp );
    
    for ( int i = 1; i < N; ++i )
        for( int j = i + 1; j <= N; ++j ){
           
			a.x = p[i].x + p[i].y - p[j].y;
			a.y = p[i].y + p[j].x - p[i].x;
			
            b.x = p[j].x + p[i].y - p[j].y;
            b.y = p[j].y + p[j].x - p[i].x;
			
            if ( bSearch( a ) && bSearch( b ) )
                sol++;
        }
    
	fprintf ( g, "%d", sol >> 1 );
	
    return 0;
}