Cod sursa(job #1920450)

Utilizator SirbuSirbu Ioan Sirbu Data 10 martie 2017 00:49:36
Problema Patrate 3 Scor 65
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <fstream>
#include <cmath>
#include <algorithm>
#define eps 1.0 / 1e9

using namespace std;

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

struct punct {

double x;
double y;
};

bool cmp (punct a, punct b){

  if (abs(a.x - b.x) <= eps)
    return a.y < b.y;
  return a.x < b.x;

}

int n;
punct v[1002];

bool gasit (punct a){

  int st = 1;
  int dr = n;
  int mij;
  while (st <= dr){
    mij = (st+dr)/2;
    if (abs(a.x-v[mij].x) <= eps && abs(a.y-v[mij].y) <=eps) // pct egale, am gasit punctu
      return 1;
    else {
      if (a.x == v[mij].x && a.y < v[mij].y)
        dr = mij - 1;
      else if (a.x < v[mij].x)
        dr = mij - 1;
        else st =  mij + 1;
    }
  }
  return 0;
}

int main (){

  int sol = 0;
  punct trans1, trans2;
  fin >>n;
  for (int i = 1; i <= n; ++i)
    fin >> v[i].x >> v[i].y;

  sort (v+1,v+n+1,cmp);

  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j){
      /* trans1.x = x1 + y1 - y2
         trans1.y = y1 + x2 - x1
         trans2.x = x2 + y1 - y2
         trans2.y = y2 + x2 - x1
      */
      trans1.x = v[i].x + v[i].y - v[j].y;
      trans1.y = v[i].y + v[j].x - v[i].x;
      trans2.x = v[j].x + v[i].y - v[j].y;
      trans2.y = v[j].y + v[j].x - v[i].x;
      if (gasit (trans1) && gasit(trans2))
        sol ++;
    }
  fout << sol/2 << "\n";

}