CodeForces-598C-Nearest vectors【高精度几何】

版权声明:仅供研究,转载请注明出处。 https://blog.csdn.net/CSUstudent007/article/details/82468110

题解:用atan2(long double y,long double x)返回点(x,y)与x正半轴的夹角,取值从(-PI,PI]。

得到所有的单点偏转角排序后,用相邻数组元素相减得到两点与原点连线的夹角,然后维护它的最小值就行了,注意最后还要用第一个减最后一个(构成整个循环),并且如果其若为负数还要加2*PI.

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const long double PI = acos(-1.0);
vector<pair<long double,int>>vec;
int flag1,flag2;
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		long double x,y;
		cin>>x>>y;
		pair<long double ,int > angle;
		angle.first=atan2(y,x);
		angle.second=i+1;
		vec.push_back(angle);
	}
	sort(vec.begin(),vec.end());
	long double ans = 2*PI;
	for(int i=0;i<n;i++){
		long double tmp = (vec[(i+1)%n].first-vec[i].first);
		if(tmp<0) tmp+=2*PI;
		if(tmp<ans){
			ans=tmp;
			flag1=vec[i].second;
			flag2=vec[(i+1)%n].second;
		}
	}
	cout<<flag1<<" "<<flag2<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/CSUstudent007/article/details/82468110
今日推荐