zoukankan      html  css  js  c++  java
  • 秦皇岛站2019CCPC A.Angel Beats

    题意:平面内给定n个点,q次询问,给次给定一个点P,问这个点与平面内n个点可以组成多少直角三角形,其中(n+q)个点互不相等

    思路:

    分别考虑P点作直角顶点和非直角顶点。这个题思路很简单,就是看如何实现简单而且不会tle!!!

    对于直角顶点和非直角顶点代码都比较简单,求后者有点离线的思想。

    这里想说的就是map的用法,自定义小于运算符,使得在map中查找的时候,统一斜率的向量都会加起来,虽然在map中依然会保存多个不同的向量。(听说是现场一血的写法,中山大学大佬nb)

    算法复杂度大概n*n*log(n)(由于n和p的范围一样,这里统一同n表示),运行时间10s左右

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    const int N = 2005;
    struct P
    {
        ll x, y;
        P(ll xx=0, ll yy=0) {
            x = xx; y = yy;
        }
        P base()const{
            if (x < 0 || (x == 0 && y < 0))return P(-x, -y);
            return *this;
        }
        bool operator<(const P&b)const {
            P p1 = base(); P p2 =b.base();
           //如果共线,考虑是相同的索引
            return p1.x*p2.y<p1.y*p2.x;
        }
        P operator-(const P&b)const {
            return P(x - b.x, y - b.y);
        }
    }a[N],qur[N];
    int n, q;
    map<P, int>m;
    ll ans[N];
    int main() {
        while (~scanf("%d%d", &n, &q)) {
            memset(ans,0,sizeof(ans));
            for (int i = 0; i < n; i++)scanf("%lld%lld", &a[i].x, &a[i].y);
            for (int i = 0; i < q; i++)scanf("%lld%lld", &qur[i].x, &qur[i].y);
            for (int i = 0; i < q; i++) {
              //求解作为直角顶点
                m.clear();
                for (int j = 0; j < n; j++)
                    m[a[j] - qur[i]]++;
                for (int j = 0; j < n; j++) {
                    P p = a[j] - qur[i];
                    p = P(-p.y, p.x);
                    ans[i] += m.count(p) ? m[p] : 0;
                }
           //由于两条直角边都会枚举,所以除2
                ans[i] /= 2;
            }
            for (int i = 0; i < n; i++) {
               //作为非直角顶点,每次枚举点i,作为直角顶点,更新全部的q组询问点
                m.clear();
                for (int j = 0; j < n; j++) {
                    if (i != j)m[a[j] - a[i]]++;
                }
                for (int j = 0; j < q; j++) {
                    P p = qur[j] - a[i];
                    p = P(-p.y, p.x);
                    ans[j] += m.count(p) ? m[p] : 0;
                }
            }
            for (int i = 0; i < q; i++)printf("%lld
    ", ans[i]);
        }
         return 0;
    }
    不疯魔不成活
  • 相关阅读:
    易宝支付Demo,生产中封装成简洁的代付接口,不用request如何获取项目运行时的真实路径(转)
    java之IO流的关闭
    Java IO包装流如何关闭?
    qt5.9模块
    九款免费轻量的 AutoCAD 的开源替代品推荐
    QT pro 添加带空格的路径以及添加库文件的正确方法
    QT添加openssl的方法
    手机芯战!麒麟与骁龙上演难分胜负的技术竞速赛(2013以后,芯片和基带都集成到一起去了)
    使用redis缓存加索引处理数据库百万级并发
    TF.Learn
  • 原文地址:https://www.cnblogs.com/gzr2018/p/11605356.html
Copyright © 2011-2022 走看看