Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

开放寻址hash + 枚举n^2

Posted by zhouzp15 at 2017-01-20 10:58:44 on Problem 2002
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

const int N = 1000 + 5;
const int MOD = 100007;
struct Point { int x, y; Point(int _x = 0, int _y = 0) : x(_x), y(_y) {} } star[N], ht[MOD];

int hash(const Point &pt) { return (pt.x * pt.x + pt.y * pt.y) % MOD; }

void insert(const Point &pt)
{
	int idx = hash(pt);
	while (ht[idx].x != INT_MAX) idx = (idx + 1) % MOD;
	ht[idx] = pt;
}

bool search(const Point &pt)
{
	int idx = hash(pt);
	while (ht[idx].x != INT_MAX)
	{
		if (ht[idx].x == pt.x && ht[idx].y == pt.y) return true;
		idx = (idx + 1) % MOD;
	}
	return false;
}

int main()
{
	int n;
	while (scanf("%d", &n) == 1 && n)
	{
		for (int i = 0; i < MOD; i++) ht[i].x = INT_MAX;
		for (int i = 0; i < n; i++) scanf("%d%d", &(star[i].x), &(star[i].y)), insert(star[i]);
		
		int ans = 0;
		for (int i = 0; i < n; i++)	for (int j = i + 1; j < n; j++)
		{
			int x1 = star[i].x, x2 = star[j].x, y1 = star[i].y, y2 = star[j].y;
			
			Point pt3 = Point(x1 + y1 - y2, y1 + x2 - x1),
				  pt4 = Point(x2 + y1 - y2, y2 + x2 - x1),
				  pt5 = Point(x1 + y2 - y1, y1 + x1 - x2),
				  pt6 = Point(x2 + y2 - y1, y2 + x1 - x2);
					  
			ans += search(pt3) && search(pt4);
			ans += search(pt5) && search(pt6);
		}
		printf("%d\n", ans / 4);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator