Cod sursa(job #1074574)

Utilizator cdascaluDascalu Cristian cdascalu Data 7 ianuarie 2014 19:24:19
Problema Patrate 3 Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 1.57 kb
#include <cstdio>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <cstring>
#include <string>
#include <set>
#include <stack>
#include <unordered_map>
#define Nmax 1001
using namespace std;

struct pct
{
    long long x,y;
}vec[Nmax], a,b;
int N,sol = 0;
void read_data()
{
    ifstream f("patrate3.in");
    f>>N;
    double x,y;
    for(int i=1;i<=N;++i)
    {
        f>>x>>y;
        vec[i].x = x*100000;
        vec[i].y = y*100000;
    }
}
bool comp(pct a, pct b)
{
    if(abs(a.x - b.x) < 10)
        return (a.y < b.y);
    else
        return (a.x < b.x);
}
bool is_equal(pct&a, pct&b)
{
    return (abs(a.x - b.x) < 10 && abs(a.y - b.y) < 10);
}
bool find(pct x)
{
    int left,right,mid;
    left = 1;
    right = N;
    while(left <= right)
    {
        mid = (left+right)/2;
        if(is_equal(vec[mid], x))
            return true;

        if(comp(vec[mid], x) == true)
            left = mid+1;
        else
            right = mid-1;
    }
    return false;
}
void solve()
{
    for(int i=1;i<=N;++i)
        for(int j=i+1;j<=N;++j)
        {
            a.x = vec[i].x + vec[i].y - vec[j].y;
            a.y = vec[i].y + vec[j].x - vec[i].x;

            b.x = vec[j].x + vec[i].y - vec[j].y;
            b.y = vec[j].y + vec[j].x - vec[i].x;

            if(find(b) && find(a))
                ++sol;
        }
    sol /= 2;
}
void write()
{
    ofstream g("patrate3.out");
    g<<sol<<"\n";
    g.close();
}
int main()
{
    read_data();
    sort(vec+1, vec+N+1, comp);
    solve();
    write();
    return 0;
}