# Cod sursa(job #1295251)

Utilizator Data 19 decembrie 2014 00:09:54 Patrate 3 5 cpp done Arhiva de probleme 1.8 kb
``````#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstring>

typedef std::pair<int , int> Point;
bool operator== (const Point& left, const Point& right) {
return left.first == right.first && left.second == right.second;
}

bool operator< (const Point& left, const Point& right) {
if (left.first == right.first) {
return left.second < right.second;
} else {
return left.first < right.first;
}
}

bool findp(std::vector<Point>& v, Point p) {
int li = 0, lf = v.size() - 1;
while (li <= lf) {
int m = (li + lf) / 2;
if (v[m] == p) {
return true;
} else if (v[m] < p) {
li = m + 1;
} else {
lf = m - 1;
}
}

return false;
}

int converttoint(char* p) {
int sol = 0;
while (*p) {
if (*p >= '0' && *p <= '9') {
sol = sol * 10 + (*p) - '0';
}
++p;
}

return sol;
}

int main()
{
std::ifstream in("patrate3.in");
std::ofstream out("patrate3.out");

std::string line;
std::getline(in, line);
int n;
sscanf(line.c_str(), "%d", &n);
std::vector<Point> v;
for (int i = 0; i < n; ++i) {
Point p;
std::getline(in, line);
char* pc = strtok(strdup(line.c_str()), " \t\n");
p.first = converttoint(pc);
pc = strtok(NULL, " \t\n");
p.second = converttoint(pc);
v.push_back(p);
}

std::sort(v.begin(), v.end());

int sol = 0;
for (int i = 0; i < v.size(); ++i) {
for (int j = i + 1; j < v.size(); ++j) {
if (v[j].second >= v[i].second) {
double d = v[j].second - v[i].second;
double D = v[j].first - v[i].first;
if (findp(v, Point(v[i].first - d, v[i].second + D)) &&
findp(v, Point(v[j].first - d, v[j].second + D))) {
sol++;
}
}
}
}

out << sol << std::endl;

in.close();
out.close();

return 0;
}
``````