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 ...
随机推荐
- PHP分页函数 学习笔记
function smarty_function_assign_debug_info($params, &$smarty){ $assigned_vars = $smarty->_tpl ...
- 显示youtube视频缩略图和Vimeo视频缩略图代码分享
这篇文章主要介绍了显示youtube视频缩略图和Vimeo视频缩略图的代码,需要的朋友可以参考下 function video_image($url){ $image_url = parse_url( ...
- 运行Delphi XE10的MongoDB例程,测试Delphi插入记录性能
Delphi XE10支持MongoDB的数据库,提供了个例子restaurants可批量导入数据. 本文对比Delphi例子与MongoDB自带的mongoimport导入批量数据的性能. 步骤: ...
- 100-Days-Of-ML-Code 评注版(Day 2)
Day2_Simple_Linear_Regression(一元线性回归) 本文引用自 Simple Linear Regression, 对其中内容进行了评注与补充说明. 回归分析是一种预测性的建模 ...
- django的Cookie-9
设置Cookie 可以通过HttpResponse对象中的set_cookie方法来设置cookie. HttpResponse.set_cookie(cookie名字, value=cookie值, ...
- Java NIO (1)
Java NIO (1) 看了下java核心技术这本书 关于nio的部分介绍比较少,而且如果自己写服务器的话nio用的还是比较多,整理一下nio的资料 java中nio主要是三个组件 Buffers ...
- Makefile:如何写目标依赖
本文并不是Makefile的教程,仅是本人学习时的感悟. Makefile的基本格式 目标:依赖文件(or 目标) [tab]命令 目标: 是要生成的或操作的命令的索引 依赖: 是生成目标依赖的文件或 ...
- D3.js和three.js
D3.js是一个数据可视化的库,看看他们的DEMO就可以知道,技术基础是SVG.兼容性是IE9+. three.js是以webgl为基础的库,封装了一些3D渲染需求中重要的工具方法与渲染循环.
- 20155310 2016-2017-2 《Java程序设计》第九周学习总结
20155310 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 JDBC入门 •数据库本身是个独立运行的应用程序 •撰写应用程序是利用通信协议对数据库进行指 ...
- DSP28335声音降噪(未完成)
1. 确定使用的模块是Webrtc-NS,采集声音的芯片TLV32AIC23,实际测试发现Webrtc-NS无法使用,所以改成FIR滤波器. 从时域特性上来看,数字滤波器还可以分为有限冲激响应数字滤波 ...