Problem: C. Nearest vectors

发布时间 2023-11-29 17:22:47作者: jiangXxin

image
题意简述:
给出一堆起点为原点的向量,找出两个向量夹角最小.

做法:
使用余弦公式和c++自带的反余弦函数,求出到每个向量到极轴的夹角,随后排序即可。
注意比较第一个向量和最后一个向量之间的夹角

点击查看代码
// Problem: C. Nearest vectors
// Contest: Codeforces - Educational Codeforces Round 1
// URL: https://codeforces.com/contest/598/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Author: a2954898606
// Create Time:2023-11-29 13:47:51
// 
// Powered by CP Editor (https://cpeditor.org)

/*
    /\_/\
   (o   o)
  /      \
 // ^ ^  \\
///      \\\
 \       |
  \_____/
 /\_  _/\
 */
#include<bits/stdc++.h>

#define all(X) (X).begin(),(X).end()
#define all2(X) (X).begin()+1,(X).end()
#define pb push_back
#define mp make_pair
#define sz(X) (int)(X).size()

#define YES cout<<"YES"<<endl
#define Yes cout<<"Yes"<<endl
#define NO cout<<"NO"<<endl
#define No cout<<"No"<<endl

using namespace std;
typedef long long unt;
typedef vector<long long> vt_unt;
typedef vector<int> vt_int;
typedef vector<char> vt_char;

template<typename T1,typename T2> inline bool ckmin(T1 &a,T2 b){
	if(a>b){
		a=b;
		return true;
	}
	return false;
}
template<typename T1,typename T2> inline bool ckmax(T1 &a,T2 b){
	if(a<b){
		a=b;
		return true;
	}
	return false;
}

namespace smart_math{
    long long quickpow(long long a,long long b,long long mod=0){
        long long ret=1;
        while(b){
            if(b&1)ret*=a;
            if(mod)ret%=mod;
            b>>=1;
            a*=a;
            if(mod)a%=mod;
        }
        if(mod)ret%=mod;
        return ret;
    }
    long long quickmul(long long a,long long b,long long mod=0){
    	long long ret=0;
    	while(b){
    		if(b&1)ret+=a;
    		if(mod)ret%=mod;
    		b>>=1;
    		a=a+a;
    		if(mod)a%=mod;
		}
		if(mod)ret%=mod;
		return ret;
	}
	long long ceil_x(long long a,long long b){
		if(a%b==0)return a/b;
		else return (a/b)+1;
	}
}
using namespace smart_math;
const long long mod=1e9+7;
const long long N=310000;
const long long M=300;
long double Pi=acos(-1);

struct node{
	double x,y;
	int i;
	long double cos;
}a[N];
bool cmp(node aa,node bb){
	return aa.cos<bb.cos;
}
int solve(){
	int n;
	cin>>n;
	//vector<node> a(n);
	for(int i=0;i<n;i++){
		cin>>a[i].x>>a[i].y;
		a[i].cos=atan2(a[i].x,a[i].y);
		if(a[i].x<0)a[i].cos+=2*Pi;
		a[i].i=i;
	}
	sort(a,a+n,cmp);
	long double ans=a[0].cos-a[n-1].cos+2*Pi;
	int ans1=a[0].i+1,ans2=a[n-1].i+1;
	for(int i=1;i<n;i++){
		long double d=a[i].cos-a[i-1].cos;
		if(d<ans){
			ans=d;
			ans1=a[i].i+1,ans2=a[i-1].i+1;
		}
	}
//	double d=a[0]
	cout<<ans1<<" "<<ans2<<endl;
    return 0;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t;
    t=1;
    //cin>>t;
    while(t--){
        int flag=solve();
        if(flag==1) YES;
        if(flag==-1) NO;
    }
    return 0;
}