Parallel Lines ICPC 2017 Japan Tsukuba

博客围绕平面点配对问题展开,给定偶数个不同平面点,需将其两两配对。通过连线,要找出所有配对方案中平行线段对的最大数量。输入为点的数量和坐标,输出最大平行线段对数,解题采用枚举所有可能方案,时间复杂度为(n - 1)!!。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时间限制: 10 Sec 内存限制: 128 MB

Problem

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.

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, L3), (L1, L4), (L2, L3), (L2, L4) and (L3, L4) are parallel. So the maximum number of parallel line pairs, in this case, is 6.

输入

The input consists of a single test case of the following format.
m
x1 y1
.
.
.
xm ym
这里写图片描述
Figure B.2. Maximizing the number of parallel line pairs for Sample Input 2
The first line contains an even integer m, which is the number of points (2 ≤ m ≤ 16). Each of the following m lines gives the coordinates of a point. Integers xi and yi (−1000 ≤ xi ≤ 1000,−1000 ≤ yi ≤ 1000) in the i-th line of them give the x- and y-coordinates, respectively, of the i-th point.
The positions of points are all different, that is, xi ≠ xj or yi ≠ yj holds for all i ≠ j. Furthermore, No three points lie on a single line.

输出

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

样例输入

4
0 0
1 1
0 2
2 4

样例输出

1

Soluton

枚举所有可能方案,不重复的情况下,时间复杂度是15*13*11*…*1=
(n-1)!!

#include <bits/stdc++.h>
#define pii pair<int,int>
using namespace std;
int n,ans;
int x[18],y[18];
bool vis[18];
map<pii,int> Map;
int biao[121];
int ha[18][18];
int cn,mx;
void dfs(int s,int res) {
    if (s==n/2) {
        cn++;
        //printf("\n");
        ans=max(ans,res);
        return;
    }
    if (ans==mx) return;
    int p,q;
    for (p=0; p<n-1; ++p)
        if (!vis[p]) {
            vis[p]=true;
            break;
        }
    int tmp;
    for (q=p+1; q<n; ++q)
        if (!vis[q]) {
            tmp=res-biao[ha[p][q]]*(biao[ha[p][q]]-1)/2;
            biao[ha[p][q]]++;
            tmp+=biao[ha[p][q]]*(biao[ha[p][q]]-1)/2;
            //printf("(%d,%d) ",p,q);
            vis[q]=true;
            dfs(s+1,tmp);
            biao[ha[p][q]]--;
            vis[q]=false;
        }
    vis[p]=false;
}
int main() {
    scanf("%d",&n);
    for (int i=0; i<n; ++i)
        scanf("%d%d",&x[i],&y[i]);
    int tot=0;
    for (int i=0; i<n-1; ++i) {
        for (int p=i+1; p<n; ++p) {
            int ux,uy,t;
            ux=x[i]-x[p];
            uy=y[i]-y[p];
            if (ux==0) {
                if (!Map[pii(0,1)]) Map[pii(0,1)]=++tot;
            } else if (uy==0) {
                if (!Map[pii(1,0)]) Map[pii(1,0)]=++tot;
            } else {
                if (uy<0) {
                    ux*=-1;
                    uy*=-1;
                }
                t=__gcd(abs(ux),abs(uy));
                ux/=t;
                uy/=t;
                if (!Map[pii(ux,uy)]) Map[pii(ux,uy)]=++tot;
            }
        }
    }
    for (int i=0; i<n-1; ++i) {
        for (int p=i+1; p<n; ++p) {
            int ux,uy,t;
            ux=x[i]-x[p];
            uy=y[i]-y[p];
            if (ux==0) {
                ha[i][p]=Map[pii(0,1)];
            } else if (uy==0) {
                ha[i][p]=Map[pii(1,0)];
            } else {
                if (uy<0) {
                    ux*=-1;
                    uy*=-1;
                }
                t=__gcd(abs(ux),abs(uy));
                ux/=t;
                uy/=t;
                ha[i][p]=Map[pii(ux,uy)];
            }
        }
    }
    mx=n*(n-1)/2;
    vis[0]=true;
    for (int i=1; i<n; ++i) {
        vis[i]=true;
        biao[ha[0][i]]++;
        //printf("(%d,%d) ",0,i);
        dfs(1,0);
        vis[i]=false;
        biao[ha[0][i]]--;
    }
    printf("%d\n",ans);
    //printf("cnt=%d\n",cn);
    return 0;
}

/**************************************************************
    Problem: 8839
    User: St085
    Language: C++
    Result: 正确
    Time:160 ms
    Memory:1708 kb
****************************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值