Amphiphilic Carbon Molecules UVA - 1606

题目简述:
图上n个点,有黑色和白色。选一条直线,统计直线一端的黑点数和另一端的白点数之和,求这个数的最大值。
题目分析:很巧妙的解法,可以确定两个点连接一条直线,选其中一个点为基准点,做其余点相对于他的坐标,还有这个点的极角(atan2)

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e3+10;
int n;
struct node
{
	int x,y,f;
	double rad;
	bool operator <(const node &p)const
	{
		return rad<p.rad;
	}
}a[maxn],b[maxn];
bool judge(node a,node b)
{
	return a.x*b.y-a.y*b.x>=0;
}
int solve()
{
	int ans=0;
	if(n<=3)return n;
	for(int i=0;i<n;++i)
	{
		int p=0;
		for(int j=0;j<n;++j)
		{
			if(i==j)continue;
			b[p].x=a[j].x-a[i].x;
			b[p].y=a[j].y-a[i].y;
			if(a[j].f==1)
			{
				b[p].x=-b[p].x;
				b[p].y=-b[p].y;
			}
			b[p].rad=atan2(b[p].y,b[p].x);
			p++;
		}
		sort(b,b+p);
		int l=0,r=0,cnt=2;//这两段while不是很理解
		while(l<p)
		{
			if(l==r){r=(r+1)%p;cnt++;}
			while(r!=l&&judge(b[l],b[r]))
			{
				r=(r+1)%p;
				cnt++;
			}
			cnt--;
			l++;
			ans=max(ans,cnt);
		}
	}
	return ans;
}
int main()
{
	//freopen("in.txt","r",stdin);
	while(scanf("%d",&n)&&n)
	{
		for(int i=0;i<n;++i)
		{
			scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].f);
		}
		printf("%d\n",solve() );
	}
}
发布了22 篇原创文章 · 获赞 3 · 访问量 1824

猜你喜欢

转载自blog.csdn.net/qq_42825058/article/details/103965016