Parallel Lines ICPC 2017 Japan Tsukuba
时间限制: 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
****************************************************************/
上一篇: leetcode 39 java dfs
下一篇: ccf201712-3 crontab