HDU 5784 How Many Triangles 极角排序

How Many Triangles


Problem Description
Alice has n points in two-dimensional plane. She wants to know how many different acute triangles they can form. Two triangles are considered different if they differ in at least one point.
 

Input
The input contains multiple test cases.
For each test case, begin with an integer n,
next n lines each contains two integers  xi  and  yi .
3n2000
0xi,yi1e9
Any two points will not coincide.
 

Output
For each test case output a line contains an integer.
 

Sample Input
  
  
3 1 1 2 2 2 3 3 1 1 2 3 3 2 4 1 1 3 1 4 1 2 3
 

Sample Output
  
  
0 1 2

 题意:给定二维平面上n个点坐标,没有重复点,求这n个点能组成多少个不同的锐角三角形。
 思路:求出总共有多少个锐角、钝角、直角。
         三角形总共有C(n,3)个,对应的 锐角数 + 钝角数 + 直角数 = 3*C(n,3)
         假设有x个钝角, y个直角,而每个钝角和直角分别有两个锐角与之对应,则三角形总数为:
  (3*C(n,3)-2*x-x-2*y-y)/3 = C(n,3)-x-y

    #include <bits/stdc++.h>
    #define endl "\n"
    using namespace std;
    typedef long long ll;
    const int MAXN = 2000 + 7;

    struct Point {
        Point(ll x = 0, ll y = 0) : x(x), y(y){}
        ll x, y;
        Point operator - (const Point &p) const {
            return Point(x - p.x, y - p.y);
        }
        ll operator * (const Point &p) const {      //    dot
            return x * p.x + y * p.y;
        }
        ll operator ^ (const Point &p) const {      //    det
            return x * p.y - y * p.x;
        }
        bool operator < (const Point &p) const {    //   angle sorting
            bool up[2] = {0, 0};
            if(y > 0 || (y == 0 && x > 0)) up[0] = 1;
            if(p.y > 0 || (p.y == 0 && p.x > 0)) up[1] = 1;
            if(up[0] ^ up[1]) return up[0];
            return (*this) ^ p ? ((*this) ^ p) > 0 : ((*this) * (*this)) < (p * p);
        }
    }p[2020], vec[4020];

    int main() {
        ios::sync_with_stdio(false);
        int n;
        while(cin >> n) {
            ll ans = 1LL * n * (n-1) *(n-2) / 6, tmp = 0;
            //  input data starts
            for(int i = 0; i < n; ++i) {
                cin >> p[i].x >> p[i].y;
            }
            //   input data ends
            //  solution start
            for(int i = 0; i < n; ++i) {
                int cnt = 0;
                for(int j = 0; j < n; ++j) {
                    if(i == j) continue;
                    vec[cnt++] = p[j] - p[i];
                }
                sort(vec, vec + cnt);
                for(int j = 0; j < cnt; ++j) {
                    vec[j + cnt] = vec[j];
                }
                ll num = 0;
                for(int j = 1; j < cnt; ++j) {
                    if((vec[j] ^ vec[j-1]) == 0 && vec[j] * vec[j-1] > 0) { //   共线
                        num++;
                    }
                    else num = 0;
                    tmp += num;
                }
                int p1 = 0, p2 = 0;
                for(int j = 0; j < cnt; ++j) {
                    //  vec[j]逆时针方向锐角数
                    while(p1 <= j || (p1 < j + cnt && (vec[p1] ^ vec[j]) < 0 && (vec[p1] * vec[j]) > 0)) p1++;
                    //  vec[j]逆时针方向的锐角、直角、钝角数
                    while(p2 <= j || (p2 < j + cnt && (vec[p2] ^ vec[j]) < 0)) p2++;
                    ans -= p2 - p1;
                }
            }
            //  solution ends
            cout << ans - tmp/2 << endl;
        }
        return 0;
    }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值