题目:Amphiphilic Carbon Molecules
题意:平面上有n(n≤1000)个点,每个点为白点或者黑点。 现在需放置一条隔板,使得隔板一侧的白点数加上另一侧的黑点数总数最大。 隔板上的点可以看作是在任意一侧。
思路:枚举基准点,作为分隔线上一点,然后再扫描相对基准点的点,将扫描的点都相加起来,只需扫白的一半,因为处理的时候将黑点旋转180后就只需记录一侧的点即可。
(1)枚举平面上的每一个点作为基准点;
(2)将剩余点相对应基准点计算出各个点后存放到数组中,并且将黑点的坐标旋转180后(如果在当前侧被旋转了另一侧,如果在另一侧正好转到此侧了!),这样只需统计一侧的所有点即可,不用考虑黑白点了。然后按各个点的方位角(与x轴夹角)递增排序;
(3)开始扫描计数:用变量l代表分隔点,r代表扫描点,r进行累加,cnt进行累计点个数,当l == r || 分隔点斜率 < 扫描点斜率时停止当前l的累计,然后l继续增加,再进行。。。
(然后并不懂扫描这块。。。)
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 1000 + 5;
struct point {
int x,y;
double rad;
bool operator < (const point&rhs)const{
return rad < rhs.rad;
}
}op[maxn],p[maxn];
int n,color[maxn];
bool Left(point a,point b){
return a.x*b.y - a.y*b.x >= 0;//x1/y1 >= x2/y2 比较俩个坐标的斜率
}
int solve(){
if(n <= 2) return 2;
int ans = 0;
for(int i=0;i<n;i++){//枚举基准点p[i]
int index = 0;
for(int j=0;j<n;j++){//将其他点找到放入新数组
if(j == i) continue;
op[index].x = p[j].x - p[i].x;//将相对基点的坐标算出
op[index].y = p[j].y - p[i].y;
if(color[j]){//将黑色的旋转到白色区域
op[index].x = -op[index].x;op[index].y = -op[index].y;
}
op[index].rad = atan2(op[index].y,op[index].x);//计算极角,(x,y)与x轴的夹角
index++;
}
sort(op,op+index);//按极角递增排序
int l = 0,r = 0,cnt = 2;//在分隔线上有俩个点,基点+扫描点
while(l < index){//l
if(r == l){//空区域,也计数,最后会减去,(不懂!)
r = (r+1)%index;//扫描点移动
cnt++;
}
while(r != l && Left(op[l],op[r])){//旋转,当分隔点斜率 < 扫描点斜率时停止
r = (r+1)%index;cnt++;
}
cnt--;//舍去多余的点
l++;//分隔点累计
ans = max(ans,cnt);
}
}
return ans;
}
int main()
{
while(scanf("%d",&n)==1 && n){
for(int i=0;i<n;i++) scanf("%d%d%d",&p[i].x,&p[i].y,&color[i]);
printf("%d\n",solve());
}
return 0;
}