Cod sursa(job #1197106)

Utilizator diana97Diana Ghinea diana97 Data 10 iunie 2014 20:18:02
Problema Patrate 3 Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>

using namespace std;

ifstream f ("patrate3.in");
ofstream g ("patrate3.out");

const int nmax = 1001;
const double e = 0.00001;

struct punct {
    double x, y;
    bool operator == (const punct p) {
        return (fabs(p.x - x) < e && fabs(p.y - y < e));
    }
    bool operator <(const punct p) {
        if (fabs(p.x - x) < e) return y < p.y;
        else return x < p.x;
    }
    bool operator >(const punct p) {
        if (fabs(p.x - x) < e) return y > p.y;
        else return x > p.x;
    }
};

int n, sol;
punct p[nmax];

void citeste () {
    f >> n;

    for (int i = 1; i <= n; i++) {
        f >> p[i].x >> p[i].y;
    }
}

inline bool conditie (punct a, punct b) {
    return a < b;
}

bool cauta (punct a) {
    int st = 1, dr = n, mid;
    while (st <= dr) {
        mid = (st + dr) / 2;
        if (p[mid] == a) return true;
        else if (p[mid] < a) st = mid + 1;
        else dr = mid - 1;
    }
    return false;
}

void rezolva () {
    punct a, b;
    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[i].y + p[j].x - p[j].y;
                b.y = p[j].x + p[j].y - p[i].x;
                if (cauta(a) && cauta(b)) sol++;
                //cout << a.x << ' ' << a.y;
        }
}

int main () {
    citeste();
    sort(p + 1, p + n + 1, conditie);
    rezolva();
    g << sol / 2 << '\n';
}