zoukankan      html  css  js  c++  java
  • BZOJ 1914 [Usaco2010 OPen]Triangle Counting 数三角形

    BZOJ_1914

        这个题目N好大,乱蒙的话大概也会想到先参考原点来个极角排序。

        接着考虑怎么去计算了,一开始想直接算满足要求的三角形,不过想了几种思路之后还是没法解决。后来突然想到不妨尝试一下计算不满足要求的三角形,这时会发现原点和三个点的连线的跨度小于180度,也就是说两条夹角小于180度的射线中间又夹了一条射线,这样的三角形才会是不符合要求的。这样我们枚举不符合要求的三角形上按极角序出现的第一个点,这时如果我们按极角序找到两射线夹角小于180度的最远的那个点,就可以O(1)计算了。

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #define MAXD 200010
    typedef long long LL;
    int N;
    struct Point
    {
        int x, y, a;
        Point(){}
        Point(int _x, int _y, int _a) : x(_x), y(_y), a(_a){}
        bool operator < (const Point &t) const
        {
            if(a == t.a) return (LL)y * t.x < (LL)x * t.y;
            return a < t.a;
        }
    }p[MAXD];
    LL det(int x1, int y1, int x2, int y2)
    {
        return (LL)x1 * y2 - (LL)x2 * y1;
    }
    void init()
    {
        for(int i = 0; i < N; i ++)
        {
            int x, y, area;
            scanf("%d%d", &x, &y);
            if(x >= 0 && y >= 0) area = 2;
            else if(x <= 0 && y <= 0) area = 0;
            else if(x > 0 && y < 0) area = 1;
            else area = 3;
            p[i] = Point(x, y, area);
        }
        std::sort(p, p + N);
        for(int i = 0; i < N; i ++) p[i + N] = p[i];
    }
    void solve()
    {
        LL ans = 0;
        int j = 0;
        for(int i = 0; i < N; i ++)
        {
            while(j < i + N && det(p[i].x, p[i].y, p[j].x, p[j].y) >= 0) ++ j;
            if(j - i >= 3) ans += (LL)(j - i - 2) * (j - i - 1) / 2;
        }
        printf("%lld\n", (LL)N * (N - 1) * (N - 2) / 6 - ans);
    }
    int main()
    {
        while(scanf("%d", &N) == 1)
        {
            init();
            if(N < 3) printf("0\n");
            else solve();
        }
        return 0;
    }
  • 相关阅读:
    实验六 继承定义与使用
    第四周java实验
    解决 GitHub 提交次数过多 .git 文件过大的问题
    添加开机启动项目
    bash启用 z(同理git bash)
    WIndows to go安装win10系统到移动硬盘
    Make for Windows
    zotero引用3GPP标准暂不完善——使用BibTeX
    Spyder中内嵌的IPython Console自动续行而不运行的问题
    texstudio.org打不开——下载最新版TeXstudio
  • 原文地址:https://www.cnblogs.com/staginner/p/2737619.html
Copyright © 2011-2022 走看看