题目链接:

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的更多相关文章

  1. POJ 1971 Parallelogram Counting (Hash)

          Parallelogram Counting Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6895   Acc ...

  2. POJ 1791 Parallelogram Counting(求平行四边形数量)

    Description There are n distinct points in the plane, given by their integer coordinates. Find the n ...

  3. 计算几何 + 统计 --- Parallelogram Counting

    Parallelogram Counting Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5749   Accepted: ...

  4. Parallelogram Counting(平行四边形个数,思维转化)

    1058 - Parallelogram Counting    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ...

  5. 1058 - Parallelogram Counting 计算几何

    1058 - Parallelogram Counting There are n distinct points in the plane, given by their integer coord ...

  6. POJ 1971 统计平行四边形 HASH

    题目链接:http://poj.org/problem?id=1971 题意:给定n个坐标.问有多少种方法可以组成平行四边形.题目保证不会有4个点共线的情况. 思路:可以发现平行四边形的一个特点,就是 ...

  7. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  8. 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). ...

  9. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

随机推荐

  1. Python中级 —— 02函数式编程

    函数式编程 函数是Python内建支持的一种封装,而函数式编程通俗说来就是把函数本身作为参数传入另一个函数,允许返回一个函数. 函数名其实也是变量,也可以被赋值.如果函数名被赋值为其他值,则不再指向原 ...

  2. C++中一个0xC0000005访问冲突问题

    在冯.诺依曼结构CPU(如i386,ARM A8,A9)的保护模式或者哈佛结构(如8051, ARM M0,.. M3)的CPU下,C++编译器将放置常量的内存设置为只读模式或者放入只读内存中,如果出 ...

  3. 大数据调错系列之hadoop在开发工具控制台上打印不出日志的解决方法

    (1)在windows环境上配置HADOOP_HOME环境变量 (2)在eclipse上运行程序 (3)注意:如果eclipse打印不出日志,在控制台上只显示 1.log4j:WARN No appe ...

  4. VCC、VDD、VSS以及VBAT的区别

    在STM32 的学习中,发现有几种看起来相关的名称,分别是VCC.VDD.VSS.VBAT,在经过搜索查找之后,总结如下: 1.VCC的C是Circuit的意思,是指整个供电回路的电压, 也有人说VC ...

  5. android4.4 重启的开机不播放开机铃声,按power键的开机播放开机铃声

    平台:A33Android4.4Linux3.4 功能描述:实现重启的开机不播放开机铃声,按power键的开机播放开机铃声 一,无论关机还是重启都会经过rebootOrShutdown这个方法,在方法 ...

  6. [project X] tiny210(s5pv210)上电启动流程(BL0-BL2)

    建议参考文档: S5PV210-iROM-ApplicationNote-Preliminary-20091126 S5PV210_UM_REV1.1 项目介绍参考 [project X] tiny2 ...

  7. DVWA:环境搭建

    0x01 安装PHP集成环境 我这里用的是phpstudy 2016,这个使用起来比较方便.下面是现在的最新版. http://www.phpstudy.net/phpstudy/phpStudy20 ...

  8. 20155217 2016-2017-2 《Java程序设计》第10周学习总结

    20155217 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 程序员所作的事情 ...

  9. 与Linux的第一次遭遇

    我的与linux首次遭遇战 虚拟机安装 安装虚拟机我遇到的问题个数可以缩减到1--我几乎没遇到安装虚拟机的问题!我严格按照老师给的链接去下载那个VirtualBox,尽管那个网页是全英文的,但是我像看 ...

  10. 20155304 2016-2017-2 《Java程序设计》实验二 Java面向对象程序设计

    实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验要求 没有Linux ...