您的位置:首页 > 其它

UVA1606 Amphiphilic Carbon Molecules

2015-03-09 08:24 344 查看
选定一个点作为基点,依次求出每个点的极角并排序,虚拟出一条直线开始旋转,动态统计两侧黑白点的个数。

#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#define maxn 100000+10
using namespace std;
int n;
struct node
{
    int x,y,cl;
    double rad;
    node(int x=0,int y=0):x(x),y(y) {}
    bool operator <(const node &a)const
    {
        return rad<a.rad;
    }
} p[maxn],st[maxn];
node operator -(const node &a,const node &b)
{
    return node(a.x-b.x,a.y-b.y);
}
int Cross(const node &a,const node &b)
{
    return (a.x*b.y-b.x*a.y);
}
int solve()
{
    int maxx=0;
    for(int i=0; i<n; i++)
    {
        int k=0;
        for(int j=0; j<n; j++)
            if(i!=j)
            {
                p[k]=st[j]-st[i];
                if(st[j].cl)//技巧性,以基点为中心对称,这样统计的黑白点就可以在一侧统计
                {
                    p[k].x=-p[k].x;
                    p[k].y=-p[k].y;
                }
                p[k].rad=atan2(p[k].y,p[k].x);
                k++;
            }
        sort(p,p+k);

        int l=0,r=0,cnt=2;
        for(;l<k;l++)
        {
            if(l==r)
            {
                r=(r+1)%k;
                cnt++;
            }
            while(l!=r&&Cross(p[l],p[r])>=0)
            {
                r=(r+1)%k;
                cnt++;
            }
            cnt--;
            maxx=max(maxx,cnt);
        }
    }
    return maxx;
}
int main()
{
    while(scanf("%d",&n)&&n)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%d %d %d",&st[i].x,&st[i].y,&st[i].cl);
        }
        printf("%d\n",solve());
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: