Cod sursa(job #903997)

Utilizator Theorytheo .c Theory Data 3 martie 2013 15:50:08
Problema Patrate 3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.54 kb
#include<fstream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<cstring>

using namespace std;

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

const int Nmax = 1003;
const int Mod = 669013;

struct Point{
    int x; int y;

    bool operator ==(const Point P1)const{
        return (abs(P1.x - x)  < 1&& abs(P1.y - y) < 1);
    }

    void print(){

        fout <<x <<" "<<y <<" ";
    }
};

struct cmp{

    bool operator()(const Point &P1,const Point &P2)const{
        if((P1.x - P2.x) == 0)
            return P1.y < P2.y;
        return P1.x < P2.x;
    }

};

int N; vector<Point> V[Mod + 5]; int NumberSquares; Point M[Nmax]; char *s; char Parse[Nmax * 33];


bool Search(const Point &P){

    long long Value = 0LL + P.x + P.y;
    int Key = (Value) % Mod;

    if(Key < 0) Key = -Key;

    for(int i = 0 ; i < V[Key].size(); ++i){
        if(V[Key][i] == P) return true;
    }

    return false;
}

void Add(const Point &P){

    long long Value =  0LL + P.x + P.y;
    int Key = Value % Mod;

    if(Key < 0) Key = -Key;

    V[Key].push_back(P);

}

int getNumber(){

    long long Result = 0; int Sgn = 1;

    while( *s && !(*s >= '0' && *s <= '9')) {
        if(*s == '-') Sgn = -1;
        ++s;
    }

    while( *s == '.' || (*s >= '0' && *s <= '9')){
        if(*s == '.') ++s;

        Result = Result * 10 + (*s - '0'); ++s;
    }

    return Result * Sgn;
}

void Read(){

    fin >> N;

    fin.getline(Parse, Nmax * 30, '\0'); s = Parse;

    for(int i = 1; i <= N; ++i){

        M[i].x = getNumber();
        M[i].y = getNumber();
        Add(M[i]);
    }
}

void Solve(){

    sort(M + 1, M + 1 + N, cmp());

    for(int i = 1; i < N; ++i)
        for(int j = i + 1; j <= N; ++j){

            Point C; Point D;

            int  XDistance = (M[i].y - M[j].y);
            int  YDistance = (M[j].x - M[i].x);

            C.x = M[j].x + XDistance; C.y = M[j].y + YDistance;
            D.x = M[i].x + XDistance; D.y = M[i].y + YDistance;

            //C.print(); D.print();fout << '\n';


            if(Search(C) && Search(D)) ++NumberSquares;

            C.x = M[j].x - XDistance; C.y = M[j].y - YDistance;
            D.x = M[i].x - XDistance; D.y = M[i].y - YDistance ;


            //C.print(); D.print(); fout <<'\n';


            if(Search(C) && Search(D)) ++NumberSquares;
        }
}

void Print(){

    fout << NumberSquares/4;
}


int main(){

    Read(); Solve(); Print();

    return 0;
}