How Many Triangles

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5784

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.

3≤n≤2000

0≤xi,yi≤1e9

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

Hint

题意

平面给你2000个不重合的点,问你有多少个锐角三角形

题解:

数一数锐角的数量A和直角+钝角的数量B,那么答案就是(A-2B)/3。 暴力算的话是\(O(n^3)\)的。使用极角排序+two pointers就可以做到\(O(n^2log\ n)\)

这边钝角指代范围在90度到180度之间的角(不包括90和180)。

我们枚举一个点,算出所有向量,然后枚举一个向量,towpointer很容易算出直角那条线,和钝角那条线,然后就可以统计个数了。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2005;
const int Q = 1e9 + 7; struct Point {
int x , y;
Point (int _x = 0 , int _y = 0) {
x = _x , y = _y;
}
Point operator - (const Point &R) const {
return Point(x - R.x , y - R.y);
}
LL operator ^ (const Point &R) const {
return (LL)x * R.y - (LL)y * R.x;
}
LL operator % (const Point &R) const {
return (LL)x * R.x + (LL)y * R.y;
}
bool sign() const {
return y > 0 || (y == 0 && x > 0);
}
bool operator < (const Point &R) const {
if (sign() != R.sign()) {
return sign() > R.sign();
}
return (*this ^ R) > 0;
}
};
int n;
Point P[N]; void work() {
for (int i = 0 ; i < n ; ++ i) {
scanf("%d%d" , &P[i].x , &P[i].y);
}
LL res = 0;
for (int i = 0 ; i < n ; ++ i) {
vector<Point> V;
for (int j = 0 ; j < n ; ++ j) {
if (P[j].x != P[i].x || P[j].y != P[i].y)
V.push_back(P[j] - P[i]);
} sort(V.begin() , V.end());
int m = V.size();
int e = 0 , p = 0 , w = 0;
for (int j = 0 ; j < m ; ++ j) {
while (e < m && (V[j] ^ V[(j + e) % m]) == 0) {
++ e;
}
p = max(e , p);
while (p < m && ((V[j] ^ V[(j + p) % m]) > 0 && (V[j] % V[(j + p) % m]) > 0)) {
++ p;
}
w = max(w , p);
while (w < m && ((V[j] ^ V[(j + w) % m]) > 0 && (V[j] % V[(j + w) % m]) <= 0)) { ++ w;
}
res += p - e;
res -= 2 * (w - p);
e = max(1 , e - 1);
p = max(1 , p - 1);
w = max(1 , w - 1);
}
}
cout << res / 3 << endl;
} int main() {
while (~scanf("%d" , &n)) {
work();
}
return 0;
}

hdu 5784 How Many Triangles 计算几何,平面有多少个锐角三角形的更多相关文章

  1. HDU 5784 How Many Triangles

    计算几何,极角排序,双指针,二分. 直接找锐角三角形的个数不好找,可以通过反面来求解. 首先,$n$个点最多能组成三角形个数有$C_n^3$个,但是这之中还包括了直角三角形,钝角三角形,平角三角形,我 ...

  2. HDU 5784 (计算几何)

    Problem How Many Triangles (HDU 5784) 题目大意 给定平面上的n个点(n<2000),询问可以组成多少个锐角三角形. 解题分析 直接统计锐角三角形较困难,考虑 ...

  3. hdu-5784 How Many Triangles(计算几何+极角排序)

    题目链接: How Many Triangles Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  4. HDU 5839 Special Tetrahedron (计算几何)

    Special Tetrahedron 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5839 Description Given n points ...

  5. hdu 2393:Higher Math(计算几何,水题)

    Higher Math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. 计算几何 平面最近点对 nlogn分治算法 求平面中距离最近的两点

    平面最近点对,即平面中距离最近的两点 分治算法: int SOLVE(int left,int right)//求解点集中区间[left,right]中的最近点对 { double ans; //an ...

  7. HDU 1249 三角形(三角形分割平面)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1249 三角形 Time Limit: 2000/1000 MS (Java/Others)    Me ...

  8. HDU 4173 Party Location(计算几何,枚举)

    HDU 4173 题意:已知n(n<=200)位參赛选手的住所坐标.现要邀请尽可能多的选手来參加一个party,而每一个选手对于离住所超过2.5Km的party一律不去,求最多能够有多少个选手去 ...

  9. HDU 6351暴力枚举 6354计算几何

    Beautiful Now Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

随机推荐

  1. bzoj千题计划247:bzoj4903: [Ctsc2017]吉夫特

    http://uoj.ac/problem/300 预备知识: C(n,m)是奇数的充要条件是 n&m==m 由卢卡斯定理可以推出 选出的任意相邻两个数a,b 的组合数计算C(a,b)必须是奇 ...

  2. LaTeX字体设置

    % 导言区 % 帮助文档 texdoc lshort-zh % 设置normalsize大小 \documentclass[10pt]{ctexart} %article,ctexbook封面, ct ...

  3. mysql复杂查询(一)

    所谓复杂查询,指涉及多个表.具有嵌套等复杂结构的查询.这里简要介绍典型的几种复杂查询格式. 一.连接查询 连接是区别关系与非关系系统的最重要的标志.通过连接运算符可以实现多个表查询.连接查询主要包括内 ...

  4. dedecms织梦让channelartlist标签支持currentstyle属性

    打开include\taglib\channelartlist.lib.php  大约93行 找到: $pv->Fields['typeurl'] = GetOneTypeUrlA($typei ...

  5. html5 canvas 填充渐变形状

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. [整理]Error: [ngRepeat:dupes]的解决方法

    sdfsadf <div class="pageNum middle PT10"> <a href="javascript:void(0);" ...

  7. [BZOJ 4350]括号序列再战猪猪侠 题解(区间DP)

    [BZOJ 4350]括号序列再战猪猪侠 Description 括号序列与猪猪侠又大战了起来. 众所周知,括号序列是一个只有(和)组成的序列,我们称一个括号 序列S合法,当且仅当: 1.( )是一个 ...

  8. centos-7安装redis服务

    一.Redis下载 在centOS里通过wget下载redis wget http://download.redis.io/releases/redis-4.0.11.tar.gz  具体版本下载地址 ...

  9. 转载 你不知道的super

    http://funhacks.net/2016/11/09/super/ super仅被用于新式类 在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能 ...

  10. cancel_delayed_work和flush_scheduled_work【转】

    转自:http://blog.chinaunix.net/uid-9688646-id-4052595.html 是不是觉得很玄?像思念一样玄?那好,我们来看点具体的,比如935行,INIT_DELA ...