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;
    }
    不疯魔不成活
  • 相关阅读:
    【NOIP2013】 华容道 bfs预处理+bfs
    【NOIP2017】逛公园 最短路+DP
    NOIP上机测试注意事项
    【NOIP2013】货车运输 最大生成树+倍增
    【NOIP2013】 火柴排队 贪心+splay
    【NOIP2013】转圈游戏 快速幂
    【xsy1143】 兔子的数字 搜索
    【xsy1172】 染色 dp
    【NOIP2017】 宝藏 状压dp
    【NOIP2017】列队 splay
  • 原文地址:https://www.cnblogs.com/gzr2018/p/11605356.html
Copyright © 2011-2022 走看看