Amphiphilic Carbon Molecules UVA - 1606

DESCRIPTION Title:
figure n points, black and white. Selected from the white point of a straight, linear statistical number of black dots and the other end of the one end and, selecting the maximum value of this number.
Topic Analysis: very clever solution, can determine a straight line connecting two points, choose one of the points as the reference point, do the rest point relative to the coordinates, there is this point of the polar angle (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() );
	}
}
Published 22 original articles · won praise 3 · Views 1824

Guess you like

Origin blog.csdn.net/qq_42825058/article/details/103965016