poj1971Parallelogram Counting

题意:给定平面上的n个点,求这n个点中能构成平行四边形的个数。

保证不会有四个点在同一条直线上。

解题思路:由于平行四边形的两条对角线交于一点,且该点为两对角线的中点。若两线段的中点是同一个点,则这两条线段的四个顶点一定可以构成一个平行四边形!

所以可以求所有线段的中点,然后根据相同中点的个数来判断平行四边形的个数。如果一个点重复了k次,则形成的平行四边形的个数为k(k-1)/2.

 1 //Accepted    16888 KB    980 ms
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 #include <algorithm>
6 using namespace std;
7 const int MAXN = 1005;
8 int n;
9 struct node
10 {
11 double x,y;
12 }p[MAXN*MAXN],e[MAXN];
13 int num;
14 void pre()
15 {
16 for (int i=0;i<n;i++)
17 {
18 scanf("%lf%lf",&e[i].x,&e[i].y);
19 }
20 for (int i=0;i<n;i++)
21 {
22 for (int j=i+1;j<n;j++)
23 {
24 p[num].x=(e[i].x+e[j].x)/2;
25 p[num++].y=(e[i].y+e[j].y)/2;
26 }
27 }
28 }
29 int cmp(struct node x,struct node y)
30 {
31 if (x.x==y.x)
32 {
33 return x.y<y.y;
34 }
35 else
36 return x.x<y.x;
37 }
38 int slove()
39 {
40 int ans=0;
41 sort(p,p+num,cmp);
42 int temp=1;
43 struct node pp=p[0];
44 for (int i=1;i<num;i++)
45 {
46 if (p[i].x==pp.x && p[i].y==pp.y)
47 {
48 temp++;
49 }
50 else
51 {
52 ans+=temp*(temp-1)/2;
53 pp.x=p[i].x;
54 pp.y=p[i].y;
55 temp=1;
56 }
57 }
58 return ans;
59 }
60 int main()
61 {
62 int T;
63 scanf("%d",&T);
64 for (int t=1;t<=T;t++)
65 {
66 scanf("%d",&n);
67 num=0;
68 pre();
69 printf("Case %d: %d\n",t,slove());
70 }
71 return 0;
72 }

poj1971Parallelogram Counting,布布扣,bubuko.com

时间: 2024-10-12 04:35:42

poj1971Parallelogram Counting的相关文章

UVA - 12075 Counting Triangles

Description Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in anMxN grid. For examp

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include

Counting Divisors HDU - 6069

Counting Divisors HDU - 6069 题意:给定区间[a,b]和k,求xk有多少因子(x属于[a,b]),求和. 题解:http://blog.csdn.net/zlh_hhhh/article/details/76680641 a.b最大可达到1e12,但是b-a<1e6. 一开始愚蠢的一个一个分解然后去求有多少因子然后求和,范围那么大裸裸的超时啊! 可以枚举素数,对每一个素数,把区间内所有可以分解的进行分解. 最后再求和. 1 #include <bits/stdc++

LightOJ - 1148 Mad Counting(坑)

Mad Counting Time Limit: 500MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Status Description Mob was hijacked by the mayor of the Town "TruthTown". Mayor wants Mob to count the total population of the town. Now the naive a

【LeetCode】338. Counting Bits (2 solutions)

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

[LeetCode][Java][JavaScript]Counting Bits

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

Project Euler 85 :Counting rectangles 数长方形

Counting rectangles By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles: Although there exists no rectangular grid that contains exactly two million rectangles, find the area of the grid with the

HDU3664 Permutation Counting

Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1487    Accepted Submission(s): 754 Problem Description Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-val

poj 2386 Lake Counting

Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24578   Accepted: 12407 Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 10