【HDU5784】How Many Triangles(极角排序 + two-pointer)

记录一个菜逼的成长。。

题目大意:
给你一些点的坐标让你求出锐角三角形的个数。
题解:
数一数锐角的数量A和直角+钝角的数量B,那么答案就是(A-2B)/3。 暴力算的话是O(n^3)的。使用极角排序+two pointers就可以做到O(n2log n)。
这边钝角指代范围在90度到180度之间的角(不包括90和180)。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <deque>
#include <cctype>
#include <bitset>
#include <cmath>
using namespace std;
#define cl(a) memset(a,0,sizeof(a))
#define ALL(a) (a).begin(),(a).end()
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const int INF = 0x3f3f3f3f;
const int maxn = 2000 + 10;
class Point{
public:
    int x,y;
    Point(){}
    Point(int _x ,int _y):x(_x),y(_y){}
    Point operator - (const Point &a)
    {
        return Point(x - a.x,y - a.y);
    }
}point[maxn];
int n;
LL det(const Point &a,const Point &b)//叉积 判断sin的符号
{
    return a.x * 1LL * b.y - a.y * 1LL * b.x;
}
LL dot(const Point &a,const Point &b)//点积  判断cos的符号
{
    return a.x * 1LL * b.x + a.y * 1LL * b.y;
}
bool Polar_cmp(Point a,Point b)//极角排序
{
    if (a.y * 1ll * b.y <= 0) {             //两个点在x轴的上下方
        if (a.y >= 0 || b.y >= 0) return a.y < b.y; //若两点连线不与x轴平行 
        if (a.y == 0 && b.y == 0) return a.x < b.x; //若两点连线与x轴平行
    }
    return det(a,b) > 0;                //按从a 到 b 为锐角排序
}
LL solve()
{
    LL num1 = 0;//锐角数量
    LL num2 = 0;//直角和钝角数量
    vector<Point>vec;
    for( int i = 0; i < n; i++ ){
        vec.clear();
        for( int j = 0; j < n; j++ ){
            if(j != i){
                vec.push_back(point[j] - point[i]);
            }
        }
        sort(ALL(vec),Polar_cmp);
        vec.insert(vec.end(),ALL(vec));
        for( int j = 0,t = 0,k = 0,r = 0; j < n - 1; j++ ){
            while(t < j + n - 1 && det(vec[j],vec[t]) == 0 && dot(vec[j],vec[t]) > 0)t++;//去掉共线和重点,虽然题目已说任意两点不同。。
            k = max(k,t);
            while(k < j + n - 1 && det(vec[j],vec[k]) > 0 && dot(vec[j],vec[k]) > 0)k++;
            r = max(k,r);
            while(r < j + n - 1 && det(vec[j],vec[r]) > 0)r++;
            num1 += k - t;
            num2 += r - k;
        }
    }
    return (num1 - 2 * num2) / 3;
}
int main()
{
    while(~scanf("%d",&n)){
        for( int i = 0; i < n; i++ ){
            scanf("%d%d",&point[i].x,&point[i].y);
        }
        printf("%lld\n",solve());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值