Cod sursa(job #1068818)

Utilizator Teodor94Teodor Plop Teodor94 Data 28 decembrie 2013 20:03:12
Problema Patrate 3 Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 1.62 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

#define MAX_N 1001
#define x first
#define y second

typedef pair< int, int > point;

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

point v[MAX_N];

int get_int( string s ) {
    int ans = 0, sign = 1;

    if ( s[0] == '-' )
        sign = -1;

    for ( int i = 0; s[i]; ++i )
        if ( s[i] >= '0' && s[i] <= '9' )
            ans = ans * 10 + s[i] - '0';

    return ans * sign;
}

void read( int &n ) {
    fin >> n;
    for ( int i = 1; i <= n; ++i ) {
        string aux_read;

        fin >> aux_read;
        v[i].x = get_int( aux_read );

        fin >> aux_read;
        v[i].y = get_int( aux_read );
    }
}

bool found( point a, int n ) {
    int i, pas = 1 << 9;
    for ( i = 0; pas; pas >>= 1 )
        if ( i + pas <= n && v[i + pas] <= a )
            i += pas;

    return ( v[i] == a );
}

int main() {
    int n;
    read( n );

    sort( v + 1, v + n + 1 );

    int ans = 0;
    for ( int i = 1; i < n; ++i )
        for ( int j = i + 1; j <= n; ++j ) {
            int dy = v[i].x - v[j].x, dx = v[i].y - v[j].y;
            point v1, v2;

            v1 = make_pair( v[i].x + dx, v[i].y - dy );
            v2 = make_pair( v[j].x + dx, v[j].y - dy );

            if ( found( v1, n ) && found( v2, n ) )
                ++ans;

            v1 = make_pair( v[i].x - dx, v[i].y + dy );
            v2 = make_pair( v[j].x - dx, v[j].y + dy );

            if ( found( v1, n ) && found( v2, n ) )
                ++ans;
        }

    fout << ( ans / 4 );
}