Cod sursa(job #2753445)

Utilizator vali_27Bojici Valentin vali_27 Data 22 mai 2021 21:47:32
Problema Patrate 3 Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.04 kb
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <string>
 
std::ifstream f("patrate3.in");
std::ofstream g("patrate3.out");

// pt ca unordered map nu stie sa faca hash pt std::pair
struct HashPair {
	unsigned long long operator()(const std::pair<int, int>& p)const {
		return (std::hash<int>{}(p.first)) ^ std::hash<int>{}(p.second);
	}
};

std::unordered_map<std::pair<int, int>, bool, HashPair> map;
struct {
	int x, y;
}patrate[1001];

int n;

//void print(int x, int y)
//{
//	std::cout << "(" << x << ", " << y << ") ";
//}

int main()
{
	f >> n;
	f.get();
	for (int i = 0; i < n; ++i)
	{
		// le citesc ca string ca sunt niste erori la zecimale
		std::string s;
		std::getline(f, s);
		int pct[2] = { 0, 0 }, idx = 0, sgn = 1;
		for (int i = 0; i < s.size(); ++i)
		{
			if (isdigit(s[i]))
				pct[idx] = pct[idx] * 10 + s[i] - '0';
			else if (s[i] == ' ')
			{
				pct[0] *= sgn;
				sgn = idx = 1;
			}
			else if (s[i] == '-')sgn = -1;
		}
		pct[1] *= sgn;

		patrate[i].x = pct[0];
		patrate[i].y = pct[1];

		map[{patrate[i].x, patrate[i].y}] = 1;
	}

	int total = 0;

	for (int i = 0; i < n; ++i)
		for (int j = i + 1; j < n; ++j)
		{
			int x1 = patrate[i].x, y1 = patrate[i].y;
			int x2 = patrate[j].x, y2 = patrate[j].y;
			//std::cout << x1 << ' ' << y1 << " si " << x2 << ' ' << y2 << '\n';
			//  pct 1  ________ pct 4
			//         |	   |
			//         |       |
			//         |_______|
			//  pct 2           pct 3

			// rotesc pct 1 in jurul pct 2 cu -90 grade (in sens trigonometric), (adica din x1, y1 scad x2, y2, rotesc (x,y) -> (y, -x) , si dupa adun x2, y2 inapoi)
			int x3 = y1 - y2 + x2;
			int y3 = x2 - x1 + y2;

			// fac acelasi lucru pt pct 2 doar ca in rotesc cu 90 grade (x, y) -> (-y, x)
			int x4 = y1 - y2 + x1;
			int y4 = x2 - x1 + y1;

			total += (map[{x3, y3}] & map[{x4, y4}]);
		}

	g << total / 2; // impartit la 2 pt ca numara de doua ori acelasi patrat o data pt perechea (pct1 si pct2) si o data pt perechea (pct1 si pct4)
}