ural 1207. Median on the Plane(極角排序)

題意是求一堆點中的兩個點,其連接的直線線要能等分所有點。

#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAX = 10010;
const double eps = 1e-6;
struct point{ double x,y;int ind;};
point p[MAX];
double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向 
{
	return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
bool cmp(point a,point b)
{
	return crossProduct(p[0],a,b) < eps;
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0; i<n; i++)
	{
		scanf("%lf%lf",&p[i].x,&p[i].y);
		p[i].ind = i+1;
	}
	int t = 0;
	for(int i=1; i<n; i++)
		if( p[i].x < p[t].x || (p[i].x == p[t].x && p[i].y < p[t].y) )
			t = i;
	swap(p[t],p[0]);
	sort(p+1,p+n,cmp);
	printf("%d %d\n",p[0].ind,p[n/2].ind);
return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章