hdu 5784 How Many Triangles 计算几何,平面有多少个锐角三角形
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 计算几何,平面有多少个锐角三角形的更多相关文章
- HDU 5784 How Many Triangles
计算几何,极角排序,双指针,二分. 直接找锐角三角形的个数不好找,可以通过反面来求解. 首先,$n$个点最多能组成三角形个数有$C_n^3$个,但是这之中还包括了直角三角形,钝角三角形,平角三角形,我 ...
- HDU 5784 (计算几何)
Problem How Many Triangles (HDU 5784) 题目大意 给定平面上的n个点(n<2000),询问可以组成多少个锐角三角形. 解题分析 直接统计锐角三角形较困难,考虑 ...
- hdu-5784 How Many Triangles(计算几何+极角排序)
题目链接: How Many Triangles Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- HDU 5839 Special Tetrahedron (计算几何)
Special Tetrahedron 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5839 Description Given n points ...
- hdu 2393:Higher Math(计算几何,水题)
Higher Math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 计算几何 平面最近点对 nlogn分治算法 求平面中距离最近的两点
平面最近点对,即平面中距离最近的两点 分治算法: int SOLVE(int left,int right)//求解点集中区间[left,right]中的最近点对 { double ans; //an ...
- HDU 1249 三角形(三角形分割平面)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1249 三角形 Time Limit: 2000/1000 MS (Java/Others) Me ...
- HDU 4173 Party Location(计算几何,枚举)
HDU 4173 题意:已知n(n<=200)位參赛选手的住所坐标.现要邀请尽可能多的选手来參加一个party,而每一个选手对于离住所超过2.5Km的party一律不去,求最多能够有多少个选手去 ...
- HDU 6351暴力枚举 6354计算几何
Beautiful Now Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
随机推荐
- bzoj千题计划240:bzoj3900: 交换茸角
http://www.lydsy.com/JudgeOnline/problem.php?id=3900 dp[i]表示让状态为i的鹿满足要求的最少交换次数 不能枚举两头鹿交换,因为一头鹿可能交换多次 ...
- 并查集:POJ 1182 食物链 复习
#include <iostream> #include <algorithm> #include <cstring> #include <cstdlib&g ...
- Spring RedisTemplate操作-哈希操作(3)
@Autowired @Resource(name="redisTemplate") private RedisTemplate<String, String> rt; ...
- [整理]WebAPI中应用oData
http://www.odata.org/ http://bitoftech.net/category/odata/ http://www.hanselman.com/blog/CreatingAnO ...
- 20155325 2016-2017-2 《Java程序设计》第8周学习总结
教材学习内容总结 NIO用于高级输入/输出处理 名称 衔接数据源与目的地 IO InputStream,OutputStream NIO Channel 类 方法 方法的作用 ReadableByte ...
- 对git简单的认识
了解git工作区.暂存区.版本库: 其中,使用 git add .就是将文件添加到了暂存区:而git commit -m ‘desc’:将暂存区的文件添加到版本库: 每次更新项目的步骤: 1)每次更新 ...
- Ubuntu14.04搭建Android O编译环境
一.搭建环境 官方参考文档: 1.代号.标签和版本号 2.Factory Images 3.Driver Binaries 4.工具链 软硬件版本: 1.系统平台:I5-8500T+8G+1T,Ub ...
- object-c 数学计算公式
1. 三角函数 double sin (double);正弦 double cos (double);余弦 double tan (double);正切 2 .反三角函数 double as ...
- Servlet、ServletConfig、ServletContext深入学习
1.Servlet学习 1.Servlet生命周期 Servlet 加载—>实例化—>服务—>销毁. init(servletConfig):(经过自己的测试发现会先调用这个而不是i ...
- 解读Android LOG机制的实现【转】
转自:http://www.cnblogs.com/hoys/archive/2011/09/30/2196199.html http://armboard.taobao.com/ Android提供 ...