POJ 1971 统计平行四边形 HASH

题目链接:http://poj.org/problem?id=1971

题意:给定n个坐标。问有多少种方法可以组成平行四边形。题目保证不会有4个点共线的情况。

思路:可以发现平行四边形的一个特点,就是对角线相交后得到的点。如果两点线的中点相交,那么这两条线就可以组成一个平行四边形[不需去排除4点共线],所以枚举两两组合的点对HASH成中点。然后判断所有中点,如果某个中点出现了K次,那么可以组成K*(K-1)/2个平行四边形。 用map来判断某个中点出现次数会TLE,所以可以对中点进行排序后判重复次数。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
typedef long long int LL;
typedef unsigned int uint;
#define mp(x,y) make_pair(x,y)
const int MAXN=1000+5;
struct Point{
    double x,y;
    Point(double a=0,double b=0):x(a),y(b){};
};
Point P[MAXN],MP[MAXN*MAXN];
Point MidP(Point a,Point b){
    return Point((a.x+b.x)/2,(a.y+b.y)/2);
}
bool cmp(Point a,Point b){
    if(a.x==b.x){
        return a.y<b.y;
    }
    return a.x<b.x;
}
int main(){
#ifdef kirito
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    int n,t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        int ans=0,cnt=0;
        for(int i=0;i<n;i++){
            scanf("%lf %lf",&P[i].x,&P[i].y);
        }
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                Point midP=MidP(P[i],P[j]);
                MP[cnt++]=midP;
            }
        }
        sort(MP,MP+cnt,cmp);
        int idx=0,tmp=1;
        for(int i=1;i<cnt;i++){
            if(MP[i].x==MP[idx].x&&MP[i].y==MP[idx].y){
                tmp++;
            }
            else{
                ans+=tmp*(tmp-1)/2;
                idx=i; tmp=1;
            }
        }
        ans+=tmp*(tmp-1)/2;
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-07 06:47:56

POJ 1971 统计平行四边形 HASH的相关文章

POJ 2002 统计正方形 HASH

题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边]另外两个点的左边.例如 已知:(x1,y1) (x2,y2)则x3=x1+(y1-y2) y3= y1-(x1-x2) x4=x2+(y1-y2) y4= y2-(x1-x2)或x3=x1-(y1-y2) y3= y1+(x1-x2) x4=x2-(y1-y2) y4= y2+(x1-x2) 枚举两

POJ 2002 点的hash

Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 15489   Accepted: 5864 Description A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating abou

poj 3286 统计0的个数

1 #include <iostream> 2 3 using namespace std; 4 long long p; 5 long long a[20]; 6 long long solve(long long n){ 7 long long left,m,sum =0; 8 for(int i=1;i<12;i++){ 9 left = n/a[i]-1; 10 sum += left*a[i-1]; 11 m = (n%a[i]-n%a[i-1])/a[i-1]; 12 if(

【POJ】Parallelogram Counting(HASH,数学之平行四边形)

Parallelogram Counting 题意:输入t表示有t组数据 每组数据输入一个数n,表示有n个点 然后有n行,每行是这个点的(x,y) 问这些点能组成多少个平行四边形 思路:求中点,中点一样的是一个平行四边形. 记录同一个中点的个数sum(初始为1),平行四边形数是(sum-1) * sum / 2; #include<iostream> #include<algorithm> using namespace std; typedef long long ll; con

POJ 1971 Parallelogram Counting

题目链接: http://poj.org/problem?id=1971 题意: 二维空间给n个任意三点不共线的坐标,问这些点能够组成多少个不同的平行四边形. 题解: 使用的平行四边形的判断条件:对角线互相平分的四边形是平行四边形. 所以我们枚举每一条线段,如果有两条线段的中点是重合的,那么这四个顶点就能构成一个平行四边形,我们也就是说每条线段我们只要维护中点就可以了. 1.map维护中点:(数据比较大,t了) 1 #include<iostream> 2 #include<cstdio

poj 2408 Anagram Groups(hash)

题目链接:poj 2408 Anagram Groups 题目大意:给定若干个字符串,将其分组,按照组成元素相同为一组,输出数量最多的前5组,每组按照字典序输出所 有字符串.数量相同的输出字典序较小的一组. 解题思路:将所有的字符串统计字符后hash,排序之后确定每组的个数并且确定一组中字典序最小的字符串.根据个数 以及字符串对组进行排序. #include <cstdio> #include <cstring> #include <vector> #include &

POJ 3320 (尺取法+Hash)

题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的,因此需要统计不重复元素个数,而且需要记录重复个数. 最好能及时O(1)反馈不重复的个数.那么毫无疑问,得使用Hash. 推荐使用map,既能Hash,也能记录对于每个key的个数. 尺取的思路: ①不停扩展R,并把扫过知识点丢到map里,直到map的size符合要求. ②更新结果. ②L++,map

poj 2002 Squares,hash

poj 2002 Squares 给出n个点,问能组成多少个正方形? 题解: 先把每个点hash 然后枚举两点(即枚举正方形的一条边),然后通过三角形全等,可以推出正方形的另外两点,在hash表里查找这两点看是存在,存在则 Cnt +1. 最后 answer = Cnt/4 //因为同一正方形都统计了4次. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const

poj 1840 Eqs (Hash)

/* 这题10^8的暴力可以出答案 但是 慢..... 有个小小的bug 出题人卡int 却没注意short 用short可以不用hash 直接搞 49428K 313MS */ #include<iostream> #include<cstdio> #include<cstring> #define base 12500000 using namespace std; int a1,a2,a3,a4,a5,x1,x2,x3,x4,x5,ans; short f[125