• [CF598C] Nearest vectors


    [CF598C] Nearest vectors - 计算几何

    Description

    有 n 个点,每个点表示原点到该点的向量,让你求出两个向量最小的夹角,输出向量的序号。

    Solution

    考虑暴力算出极角然后排序,用 atan2(y,x)

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long
    
    const double pi = acos(-1);
    
    
    signed main()
    {
        struct point
        {
            long double x, y, a;
            int id;
            void gen_angle()
            {
                long double res = atan2(y, x);
                if (res < 0.0)
                    res += 2 * pi;
                a = res;
            }
    
            long double angle() const
            {
                return a;
            }
    
            bool operator<(const point &rhs) const
            {
                return angle() < rhs.angle();
            }
        };
    
        ios::sync_with_stdio(false);
    
        int n;
        cin >> n;
    
        vector<point> p(n + 2);
        for (int i = 1; i <= n; i++)
            cin >> p[i].x >> p[i].y, p[i].gen_angle(), p[i].id = i;
    
        sort(&p[1], &p[n + 1]);
        p[n + 1] = p[1];
    
        long double min_value = 1e9;
        int min_pos = 0;
    
        for (int i = 1; i < n; i++)
        {
            long double now = abs(p[i + 1].angle() - p[i].angle());
            if (now < min_value)
            {
                min_value = now;
                min_pos = i;
            }
        }
    
        long double now = abs(-p[n].angle() + 2 * pi + p[1].angle());
        if (now < min_value)
        {
            min_value = now;
            min_pos = n;
        }
    
        int x = min_pos, y = min_pos + 1;
    
        cout << p[x].id << " " << p[y].id << endl;
    }
    
  • 相关阅读:
    android 启动报错
    android 百度地图
    android LayoutInflater使用
    spring mvc No mapping found for HTTP request with URI [/web/test.do] in DispatcherServlet with name 'spring'
    sql mysql和sqlserver存在就更新,不存在就插入的写法(转)
    jsp include
    json 解析
    css
    Scrapy组件之item
    Scrapy库安装和项目创建
  • 原文地址:https://www.cnblogs.com/mollnn/p/14354087.html
Copyright © 2020-2023  润新知