lightoj-1058 - Parallelogram Counting【计算几何】(思维)

    xiaoxiao2025-04-22  14

    题目链接:点击打开链接

    1058 - Parallelogram Counting    PDF (English)StatisticsForum Time Limit: 2 second(s)Memory Limit: 32 MB

    There are n distinct points in the plane, given by their integer coordinates. Find the number of parallelograms whose vertices lie on these points. In other words, find the number of 4-element subsets of these points that can be written as {A, B, C, D} such that AB || CD, and BC || AD. No four points are in a straight line.

    Input

    Input starts with an integer T (≤ 15), denoting the number of test cases.

    The first line of each test case contains an integer n (1 ≤ n ≤ 1000). Each of the next n lines, contains 2 space-separated integers x and y (the coordinates of a point) with magnitude (absolute value) of no more than1000000000.

    Output

    For each case, print the case number and the number of parallelograms that can be formed.

    Sample Input

    Output for Sample Input

    2

    6

    0 0

    2 0

    4 0

    1 1

    3 1

    5 1

    7

    -2 -1

    8 9

    5 7

    1 1

    4 8

    2 0

    9 8

    Case 1: 5

    Case 2: 6

     

    学了这么多年数学,然而只知道平行四边形对角线交于一点,却没想到只要存在两点的中点与另两点的中点相同,就能构成平行四边形,我个ZZ #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int MAX=1e6+10; int n; struct node { double x,y; }; node point1[MAX]; node point2[MAX]; // point2 的容量远比 point1 大得多 bool judge(int a,int b) { if(point2[a].x==point2[b].x&&point2[a].y==point2[b].y) return 1; return 0; } bool cmp(node a,node b) { if(a.x!=b.x) return a.x<b.x; return a.y<b.y; } int main() { int t,text=0; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0;i<n;i++) scanf("%lf %lf",&point1[i].x,&point1[i].y); int k=0; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { point2[k].x=(point1[i].x+point1[j].x)/2; point2[k++].y=(point1[i].y+point1[j].y)/2; } } /* 不能这样干,这样干是超时的, int cnt=0; for(int i=0;i<k;i++) { for(int j=i+1;j<k;j++) { if(judge(i,j)) cnt++; } } printf("Case %d: %d\n",++text,cnt);*/ int cnt=1,ans=0; sort(point2,point2+k,cmp); // 排序是为了方便下面比较 for(int i=1;i<k;i++) { if(judge(i,i-1)) { cnt++; } else { ans+=(cnt-1)*cnt/2; // 计算就是组合数:从 n个数里面取出 2个数 ,就是 C(n,2) cnt=1; // 一定要初始化的 } } if(cnt>1) ans+=(cnt-1)*cnt/2; // 判断循环的最后一组数据,如果也存在相同的点,就加上 printf("Case %d: %d\n",++text,ans); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1298344.html
    最新回复(0)