Cod sursa(job #7933)

Utilizator ZeusCatalin Tiseanu Zeus Data 23 ianuarie 2007 00:41:16
Problema Patrate 3 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.51 kb

using namespace std;

#include <cstdio>
#include <algorithm>
#include <map>
#include <iostream>
#include <set>

#define pii pair<int,int>

#define EPS 1e-4

double abs( double x )
{ return x >= 0 ? x : -x; }

struct pos{
    double x, y;
    pos(){};
    pos( double _x, double _y ){ x = _x, y = _y; }          
};

bool operator < ( const pos & p1, const pos & p2 )
{
    if( abs( p1.x - p2.x ) < EPS)
        return p1.y + EPS < p2.y;
    return p1.x + EPS < p2.x;      
}

set<pos> se;

int N;
double px[1010], py[1010];

int main()
{
    freopen("patrate3.in", "r", stdin);
    freopen("patrate3.out", "w", stdout);
    
    double x, y;
    
    scanf("%d", &N);
    
    for( int i = 0; i < N; i++ )
    {
         scanf("%lf %lf", px + i, py + i);
         se.insert( pos( px[i], py[i] ) ); 
    }
    
    double x0, x1, x2, x3, y0, y1, y2, y3, mijx, mijy, dx, dy;
    int ret(0);
    /*
    
    dx = abs(mijx - x0) si dy = abs(mijy - y0). 
    Se observa ca daca y0 < y1 atunci x2 = mijx + dy, y2 = mijy - dx, x3 = mijx - dy iar y3 = mijy + dx. 
    In caz contrar, avem x2 = mijx - dy, y2 = mijy - dx, x3 = mijx + dy iar y3 = mijy + dx.
    
    */
    
    for( int i = 0; i < N; i++ )
         for( int j = i + 1; j < N; j++ )
         {
             x0 = px[i], y0 = py[i], x1 = px[j], y1 = py[j];  
         
             if( x0 > x1 )
                 swap( x0, x1 ), swap( y0, y1 );
         
             mijx = ( x0 + x1 ) * 0.5, mijy = ( y0 + y1 ) * 0.5;
             
             dx = abs( mijx - x0 ), dy = abs( mijy - y0 );
             
             if( y0 + EPS < y1 )
             {
                 x2 = mijx + dy, y2 = mijy - dx;
                 x3 = mijx - dy, y3 = mijy + dx;
             }
             else
             {
                 x2 = mijx - dy, y2 = mijy - dx;
                 x3 = mijx + dy, y3 = mijy + dx;
             }
             
//             if( i == 3 && j == 5 )
//                  cout << " # " << x0 << "," << y0 << " : " << x1 << "," << y1 << " : " << x2 << "," << y2 << " : " << x3 << "," << y3 << endl;

             
             if( se.find( pos(x2,y2) ) != se.end() )
                 if( se.find( pos(x3,y3) ) != se.end() )
                 {
//                     cout << x0 << "," << y0 << " : " << x1 << "," << y1 << " : " << x2 << "," << y2 << " : " << x3 << "," << y3 << endl;
                     ++ret;
                 }
         }
    
    printf("%d\n", ret >> 1 );
    
    return 0;    
}