Cod sursa(job #1197389)

Utilizator diana97Diana Ghinea diana97 Data 11 iunie 2014 20:12:51
Problema Patrate 3 Scor 65
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 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;

int n, sol;
pair <double, double> p[nmax];

void citeste () {
    f >> n;

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

bool cauta (pair <double, double> c) {
    int st = 1, dr = n, mid;

    while (st <= dr) {
        mid = (st + dr) / 2;
        if (fabs(p[mid].first - c.first) < e && fabs(p[mid].second - c.second) < e) return true;
        else if (p[mid] < c) st = mid + 1;
        else dr = mid - 1;
    }

    return false;
}


void rezolva () {
    pair <double, double> a, b;

    for (int i = 1; i < n; ++i)
        for (int j = i + 1; j <= n; ++j) {
                a.first = p[i].first + p[i].second - p[j].second;
                a.second = p[i].second + p[j].first - p[i].first;
                b.first = p[j].first + p[i].second - p[j].second;
                b.second = p[j].second + p[j].first - p[i].first;
                if (cauta(a) && cauta(b)) sol++;
        }
}

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