POJ1971 Parallelogram Counting(hash)

题意:

给出一系列点的坐标,要求能组成几个不同的平行四边形。

要点:

思路很简单,找两个点的中点组成哈希表即可,我用链表写不是超时就是超空间,看来ACM还是尽量别用指针,用数组模拟链表可过。


15878931Seasonal1971Accepted36868K704MSC++1103B2016-08-03 10:01:39
#include<iostream>
using namespace std;
const int hashnum = 7345787;
int head[hashnum], nextnode[1000003];
int ans,m;

struct points
{
	int x, y;
}point[1005];
struct midpoints
{
	int x, y, cnt;
}midpoint[1000003];

int HashFunction(int x, int y) 
{
	int h;
	h = ((x << 2) + (x >> 4)) ^ (y << 10);
	h = h % hashnum;
	h = h < 0 ? h + hashnum : h;
	return h;
}
void HashInsert(int x, int y)
{
	int h = HashFunction(x, y);
	bool flag = false;
	for (int i = head[h]; i != -1; i = nextnode[i])
	{
		if (midpoint[i].x == x&&midpoint[i].y == y)
		{
			flag = true;
			ans += midpoint[i].cnt++;
		}
	}
	if (!flag)
	{
		midpoint[m].x = x;
		midpoint[m].y = y;
		midpoint[m].cnt = 1;
		nextnode[m] = head[h];//用数组模拟链表,倒序插入
		head[h] = m++;
	}

}

int main()
{
	int t, n;
	cin >> t;
	while (t--)
	{
		cin >> n;
		ans = m = 0;
		memset(head, -1, sizeof(head));
		for (int i = 0; i < n; i++)
			cin >> point[i].x >> point[i].y;
		for(int i=0; i<n; i++)
			for (int j = i+1; j < n; j++)
			{
				int x = point[i].x + point[j].x;
				int y = point[i].y + point[j].y;
				HashInsert(x, y);
			}
		cout << ans << endl;
	}
	return 0;
}


转载于:https://www.cnblogs.com/seasonal/p/10343707.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值