Cod sursa(job #2305198)

Utilizator ValentinSavoiuFMI Savoiu Valentin-Marian ValentinSavoiu Data 19 decembrie 2018 16:16:49
Problema Patrate 3 Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.72 kb
#include <fstream>
#include <vector>
#include <cmath>
#include <algorithm>
#define EPS 1e-4
using namespace std;
ifstream f("patrate3.in");
ofstream g("patrate3.out");
struct pointttt {
    double x, y;
}A[1001], P3, P4;
vector <pointttt> Mapppp[10007];
int N, i, j;
long long sol;
double xMid, yMid, diffX, diffY;
int hash_function(pointttt P) {
    return abs((int)(P.x + P.y) ) % 10007;
}
void hash_insert(pointttt P) {
    int val = hash_function(P);
    Mapppp[val].push_back(P);
}
bool hash_find(pointttt P) {
    int val = hash_function(P);
    for (unsigned int i = 0; i < Mapppp[val].size(); ++i)
        if (fabs(P.x - Mapppp[val][i].x) < EPS && fabs(P.y - Mapppp[val][i].y) < EPS)
           return true;
    return false;
}
int cmp (pointttt P, pointttt Q) {
    if(P.x != Q.x) return P.x < Q.x;
    return P.y < Q.y;
}
int main()
{
    f >> N;
    for (i = 0; i < N; ++i) {
        f >> A[i].x >> A[i].y;
        hash_insert(A[i]);
    }
    sort (A, A + N, cmp);
    for (i = 0; i < N; ++i)
        for (j = i + 1; j < N; ++j) {
            xMid = (A[i].x + A[j].x)/ 2;
            yMid = (A[i].y + A[j].y)/ 2;
            diffX = fabs(xMid - A[i].x);
            diffY = fabs(yMid - A[i].y);
            if (A[i].y < A[j].y) {
                P3.x = xMid + diffY;
                P3.y = yMid - diffX;
                P4.x = xMid - diffY;
                P4.y = yMid + diffX;
            }
            else {
                P3.x = xMid - diffY;
                P3.y = yMid - diffX;
                P4.x = xMid + diffY;
                P4.y = yMid + diffX;
            }
            if (hash_find(P3) && hash_find(P4))
                sol++;
        }
    g << sol / 2;
    return 0;
}