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 ...
随机推荐
- windows下开启 PHP扩展Redis
Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...
- java 加载并读取Properties 文件
1 .系统自带的application.properties (以下代码仅供参考,不能粘贴复制) 假设application.properties文件有下面两个值: come.test.name = ...
- opencv移植(一)cmake安装
原文:https://blog.csdn.net/Guet_Kite/article/details/78667175?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接 ...
- arm平台的调用栈回溯(backtrace)
title: arm平台的调用栈回溯(backtrace) date: 2018-09-19 16:07:47 tags: --- 介绍 arm平台的调用栈与x86平台的调用栈大致相同,稍微有些区别, ...
- linux使用docker-compose部署软件配置
本篇将分享一些 docker-compose 的配置,可参考其总结自己的一套基于docker的开发/生产环境配置.下面话不多说了,来一起看看详细的介绍吧 安装docker及docker-compose ...
- 20155212——man指令的使用及mypwd的实现
man指令的使用及mypwd的实现 man指令的使用 一.man -k的k参数以及代表的意思 代号 代表內容 1 使用者在shell中可以操作的指令或可执行档 2 系統核心可呼叫的函数与工具等 3 一 ...
- 《图说VR入门》——googleVR 他山之玉
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/53125482 作者:car ...
- 成都Uber优步司机奖励政策(4月13日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 【转载】C/C++杂记:运行时类型识别(RTTI)与动态类型转换原理
原文:C/C++杂记:运行时类型识别(RTTI)与动态类型转换原理 运行时类型识别(RTTI)的引入有三个作用: 配合typeid操作符的实现: 实现异常处理中catch的匹配过程: 实现动态类型转换 ...
- 【LG3229】[HNOI2013]旅行
题面 洛谷 题解 勘误:新的休息点a需要满足的条件2为那一部分小于等于ans 代码 \(100pts\) #include <iostream> #include <cstdio&g ...