POJ 1971 Parallelogram Counting
题目链接:
http://poj.org/problem?id=1971
题意:
二维空间给n个任意三点不共线的坐标,问这些点能够组成多少个不同的平行四边形。
题解:
使用的平行四边形的判断条件:对角线互相平分的四边形是平行四边形。
所以我们枚举每一条线段,如果有两条线段的中点是重合的,那么这四个顶点就能构成一个平行四边形,也就是说每条线段我们只要维护中点就可以了。
1、map维护中点:(数据比较大,t了)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<utility>
using namespace std;
typedef long long LL; const int maxn = ; int n;
int x[maxn],y[maxn];
map<pair<int,int>,int> mp; void init(){
mp.clear();
} int main(){
int tc;
scanf("%d",&tc);
while(tc--){
init();
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",x+i,y+i);
}
int ans=;
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){
ans+=mp[make_pair(x[i]+x[j],y[i]+y[j])];
mp[make_pair(x[i]+x[j],y[i]+y[j])]++;
}
}
printf("%d\n",ans);
}
return ;
}
2、用hash做(vector来建表)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<utility>
#include<vector>
using namespace std;
typedef long long LL; const int maxn = ;
//可能是因为vector<int> tab[mod]的初始化影响很大
//1e3+7跑2985MS 1e4+7跑1282MS 1e5+7跑1750MS 1e6+7跑4532MS
const int mod = 1e3+; struct Point {
int x, y, cnt;
Point(int x, int y, int cnt = ) :x(x), y(y), cnt(cnt) {}
Point() { cnt = ; }
}pt[maxn]; int n;
vector<Point> tab[mod];
int Hash(const Point& p) {
//LL tmp = (p.x) *1000000007 + p.y;
int tmp = ((p.x << ) + (p.x >> )) ^ (p.y << ); //折叠法,比上面一个稍微快一点
//注意哈希出来的要是非负数
tmp = (tmp%mod + mod) % mod;
return tmp;
} int add(const Point& p) {
int key = Hash(p);
int pos = -;
for (int i = ; i<tab[key].size(); i++) {
Point& tmp = tab[key][i];
if (p.x == tmp.x&&p.y == tmp.y) {
pos = i; break;
}
}
int ret = ;
if (pos == -) {
tab[key].push_back(Point(p.x, p.y, ));
}
else {
ret = tab[key][pos].cnt;
tab[key][pos].cnt++;
}
return ret;
} void init() {
for (int i = ; i<mod; i++) tab[i].clear();
} int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
init();
scanf("%d", &n);
for (int i = ; i<n; i++) {
scanf("%d%d", &pt[i].x, &pt[i].y);
}
int ans = ;
for (int i = ; i<n; i++) {
for (int j = i + ; j<n; j++) {
Point p = Point(pt[i].x + pt[j].x, pt[i].y + pt[j].y);
ans += add(p);
}
}
printf("%d\n", ans);
}
return ;
}
3、用邻接表做散列表,初始化可以省一些时间 954MS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<utility>
#include<vector>
using namespace std;
typedef long long LL; const int maxn = ; const int mod = 1e6+; struct Point {
int x, y, cnt, ne;
Point(int x, int y, int cnt = ) :x(x), y(y), cnt(cnt) {}
Point(int x, int y, int cnt, int ne) :x(x), y(y), cnt(cnt), ne(ne) { }
Point() { cnt = ; }
}pt[maxn],egs[maxn*maxn]; int n;
int tab[mod],tot;
int Hash(const Point& p) {
LL tmp = (p.x) * + p.y;
// int tmp = ((p.x << 2) + (p.x >> 4)) ^ (p.y << 10); //折叠法,比上面一个稍微快一点
//注意哈希出来的要是非负数
tmp = (tmp%mod + mod) % mod;
return tmp;
} int add(const Point& p) {
int key = Hash(p);
int pos = -,_p=tab[key];
while (_p != -) {
Point& e = egs[_p];
if (p.x == e.x&&p.y == e.y) {
pos = _p; break;
}
_p = e.ne;
}
int ret = ;
if (pos == -) {
egs[tot] = Point(p.x, p.y, , tab[key]);
tab[key] = tot++;
}
else {
ret = egs[pos].cnt;
egs[pos].cnt++;
}
return ret;
} void init() {
memset(tab, -, sizeof(tab));
tot = ;
} int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
init();
scanf("%d", &n);
for (int i = ; i<n; i++) {
scanf("%d%d", &pt[i].x, &pt[i].y);
}
int ans = ;
for (int i = ; i<n; i++) {
for (int j = i + ; j<n; j++) {
Point p = Point(pt[i].x + pt[j].x, pt[i].y + pt[j].y);
ans += add(p);
}
}
printf("%d\n", ans);
}
return ;
}
POJ 1971 Parallelogram Counting的更多相关文章
- POJ 1971 Parallelogram Counting (Hash)
		Parallelogram Counting Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6895 Acc ... 
- POJ 1791 Parallelogram Counting(求平行四边形数量)
		Description There are n distinct points in the plane, given by their integer coordinates. Find the n ... 
- 计算几何 + 统计 --- Parallelogram Counting
		Parallelogram Counting Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5749 Accepted: ... 
- Parallelogram Counting(平行四边形个数,思维转化)
		1058 - Parallelogram Counting PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ... 
- 1058 - Parallelogram Counting 计算几何
		1058 - Parallelogram Counting There are n distinct points in the plane, given by their integer coord ... 
- POJ 1971 统计平行四边形 HASH
		题目链接:http://poj.org/problem?id=1971 题意:给定n个坐标.问有多少种方法可以组成平行四边形.题目保证不会有4个点共线的情况. 思路:可以发现平行四边形的一个特点,就是 ... 
- POJ 2386 Lake Counting(深搜)
		Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17917 Accepted: 906 ... 
- poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)
		http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ... 
- POJ 2386 Lake Counting
		Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28966 Accepted: 14505 D ... 
随机推荐
- Python中级 —— 06SMTP发送电子邮件
			Email的历史比Web还要久远,直到现在,Email也是互联网上应用非常广泛的服务.(未完) 
- Hadoop源码学习笔记之NameNode启动场景流程三:FSNamesystem初始化源码剖析
			上篇内容分析了http server的启动代码,这篇文章继续从initialize()方法中按执行顺序进行分析.内容还是分为三大块: 一.源码调用关系分析 二.伪代码执行流程 三.代码图解 一.源码调 ... 
- 浅谈ruby中的block及yield
			今天写代码的时候遇到了block_given?,查阅了一下语法书中并没有相关的知识点,于是翻阅微博及结合工作中的实际代码,整理如下: 一.“块”: ruby的块指的是什么? 是 do~end中间的那部 ... 
- poj 2349 Arctic Network(prime)
			Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25165 Accepted: 7751 Description The ... 
- ASP.NET Web Form 与 ASP.NET MVC 区别
			Asp.net 微软提供web开发框架或者技术.分Web Form和ASP.NET MVC.下面简单说明各自优缺点及使用场景. Web Form ASP.NET Webform提供了一个类似于winf ... 
- 资源很多,你却不会使用——以不变应万变才是自学Java的正确方法
			鄙人乐于寻找学习方法,在这里提出自己的见解,希望可以帮助想玩好Java而又感觉很难上手的同学对Java不再恐惧 现状 我们的同学们除了某月,某婷等等大神以外,想必仍然存在着一大批同学根本没有摸索到学习 ... 
- 【python笔记】python中的list、tuple、set、dict用法简析
			list list是一种有序的集合(或称作列表),可以很方便地添加和删除其中的元素. >>> classmates = ['Michael', 'Bob', 'Tracy'] 可通过 ... 
- linux  冒号用法
			冒号在Linux中是一个空命令,可以认为与shell的内建命令true相同,它的返回值是0. 在while循环中 while : 与 while true 的作用是等效的 在 if/then 中可作为 ... 
- JS基础,课堂作业,计算器
			网页内的简单计算器 <script> var a = parseInt(prompt("请输入第一个数字:")); var b = parseInt(prompt(&q ... 
- kobject和kset的一些学习心得
			#include <linux/module.h> #include <linux/kernel.h> #include <linux/kobject.h> #in ... 
