[Aizu 1379]Parallel Lines

Parallel Lines

Given an even number of distinct planar points, consider coupling all of the points into pairs. All the possible couplings are to be considered as long as all the given points are coupled to one and only one other point.

When lines are drawn connecting the two points of all the coupled point pairs, some of the drawn lines can be parallel to some others. Your task is to find the maximum number of parallel line pairs considering all the possible couplings of the points.

For the case given in the first sample input with four points, there are three patterns of point couplings as shown in Figure B.1. The numbers of parallel line pairs are 0, 0, and 1, from the left. So the maximum is 1.

img

Figure B.1. All three possible couplings for Sample Input 1

For the case given in the second sample input with eight points, the points can be coupled as shown in Figure B.2. With such a point pairing, all four lines are parallel to one another. In other words, the six line pairs (L1,L2)(L1,L2), (L1,L3)(L1,L3), (L1,L4)(L1,L4), (L2,L3)(L2,L3), (L2,L4)(L2,L4) and (L3,L4)(L3,L4) are parallel. So the maximum number of parallel line pairs, in this case, is 6.

Input

The input consists of a single test case of the following format.

m
x1 y1
...
xm ym

img

Figure B.2. Maximizing the number of parallel line pairs for Sample Input 2

The first line contains an even integer mm, which is the number of points (2m162\leq m\leq 16). Each of the following mm lines gives the coordinates of a point. Integers xixi and yiyi (1000xi,yi1000-1000 \leq x_i, y_i\leq 1000) in the ii-th line of them give the xx- and yy-coordinates, respectively, of the ii-th point.

The positions of points are all different, that is, xi≠xjxi≠xj or yi≠yjyi≠yj holds for all i≠ji≠j. Furthermore, No three points lie on a single line.

Output

Output the maximum possible number of parallel line pairs stated above, in one line.

Sample

Sample Input 1

4
0 0
1 1
0 2
2 4

Sample Output 1

1

Sample Input 2

8
0 0
0 5
2 2
2 7
3 -2
5 0
4 -2
8 2

Sample Output 2

6

Sample Input 3

6
0 0
0 5
3 -2
3 5
5 0
5 7

Sample Output 3

3

Sample Input 4

2
-1000 1000
1000 -1000

Sample Output 4

0

Sample Input 5

16
327 449
-509 761
-553 515
360 948
147 877
-694 468
241 320
463 -753
-206 -991
473 -738
-156 -916
-215 54
-112 -476
-452 780
-18 -335
-146 77

Sample Output 5

12

Solution

思路

观察到m十分小,考虑枚举所有配对方案判断是否可行再统计答案。

Code

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 20;
const double eps = 1e-8;
int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || '9' < ch)
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while ('0' <= ch && ch <= '9')
    {
        x = x * 10 + ch - 48;
        ch = getchar();
    }
    return x * f;
}

struct Point
{
    double x, y;
    Point () {}
    Point (double _x, double _y)
    {
        x = _x;
        y = _y;
    }
    void Init()
    {
        x = read();
        y = read();
    }
} p[N];

struct Vector
{
    double x, y;
    Vector () {}
    Vector (double _x, double _y)
    {
        x = _x;
        y = _y;
    }
    double operator * (const Vector &b) const {
        return x * b.x + y * b.y;
    }
    double operator ^ (const Vector &b) const {
        return x * b.y - y * b.x;
    }
};

Vector operator - (const Point &a, const Point &b) {
    return Vector(a.x - b.x, a.y - b.y);
}
int n;
void readin()
{
    n = read();
    for (int i = 1; i <= n; ++i)
        p[i].Init();
}

int c[N], ans;
void dfs(int k)
{
        
    if (k == n + 1)
    {
        int res = 0;
        for (int i = 1; i <= n; ++i)
        {
            Vector tmp1 = p[c[i]] - p[i];
            for (int j = 1; j <= n; ++j)
                if (c[i] != j && i != j)
                {
                    Vector tmp2 = p[c[j]] - p[j];
                    if (fabs(tmp1 ^ tmp2) < eps)
                        ++res;
                }
        }
        ans = max(ans, res);
        return;
    }
    if (c[k])
        dfs(k + 1);
    else 
        for (int i = 1; i <= n; ++i)
            if (i != k && !c[i])
            {
                c[i] = k; c[k] = i;
                dfs(k + 1);
                c[i] = 0; c[k] = 0;
            }
}
void solve()
{
    ans = 0;
    memset(c, 0, sizeof(c));
    dfs(1);
    printf("%d\n", ans / 8);
}

int main()
{
    readin();
    solve();
    return 0;
}
评论区

Gitalking ...

Markdown is supported

Be the first guy leaving a comment!