Cod sursa(job #1076899)

Utilizator razvan9310FMI - Razvan Damachi razvan9310 Data 10 ianuarie 2014 18:19:54
Problema Patrate 3 Scor 0
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 1.89 kb
#include <fstream>
#include <unordered_set>
#define MAXN 1000
using namespace std;

struct Punct {
	int x, y;
}puncte[MAXN];

int N, patrate;
unordered_set<int> hashX, hashY;


void input() {
	ifstream in("patrate3.in");
	in >> N;
	string numere;
	getline(in, numere);

	for (int i = 0; i < N; ++i) {
		getline(in, numere);

		int size = numere.size();
		bool spatiu = 0;
		puncte[i].x = puncte[i].y = 0;
		int semn1 = 1, semn2 = 1;

		for (int j = 0; j < size; ++j) {
			if (numere[j] == '.') {
				continue;
			}

			if (numere[j] == '-') {
				if (!spatiu) {
					semn1 = -1;
				} else {
					semn2 = -1;
				}
				continue;
			}

			if (numere[j] == ' ') {
				spatiu = 1;
			} else {
				if (!spatiu) {
					puncte[i].x = puncte[i].x * 10 + numere[j] - '0';
				} else {
					puncte[i].y = puncte[i].y * 10 + numere[j] - '0';
				}
			}
		}

		puncte[i].x *= 10, puncte[i].y *= 10;
		puncte[i].x *= semn1, puncte[i].y *= semn2;
		hashX.insert(puncte[i].x), hashY.insert(puncte[i].y);
	}

	in.close();
}


void solve() {
	unordered_set<int>::iterator Xnot_found = hashX.end();
	unordered_set<int>::iterator Ynot_found = hashY.end();

	for (int i = 0; i < N - 1; ++i) {
		for (int j = i + 1; j < N; ++j) {
			if (puncte[i].x <= puncte[j].x || puncte[i].y <= puncte[j].y) {
				continue;
			}

			int x1 = puncte[i].x, y1 = puncte[i].y, x2 = puncte[j].x, y2 = puncte[j].y;
			int xc = (x1 + x2) / 2, yc = (y1 + y2) / 2, xd = (x1 - x2) / 2, yd = (y1 - y2) / 2;
			int x3 = (xc - yd), y3 = yc + xd, x4 = xc + yd, y4 = (yc - xd);

			if (hashX.find(x3) != Xnot_found && hashY.find(y3) != Ynot_found
				&& hashX.find(x4) != Xnot_found && hashY.find(y4) != Ynot_found) {
				++patrate;
			}
		}
	}
}

inline void output() {
	ofstream out("patrate3.out");
	out << patrate;
	out.close();
}

int main() {
	input();
	solve();
	output();
	return 0;
}