直角三角形 纪中 1385 数学_斜率 英文题解
2016-07-12 20:27
330 查看
题解
We need to find an algorithm of complexity better than O(N3). Here we will describe three suchalgorithms.
The basic idea of the first one is: choose the point in which the angle will be right, fix one other point
in the triangle and quickly calculate how many of the remaining points form a right triangle with the
first two points. The algorithm is based on the so-called canonical representation of a line. Each line
can be, regardless of which two points it is generated from, transformed into a unique form. For a pair
of points we use the usual formulas to calculate three integers A, B and C such that Ax + By + C = 0.
The equation can be multiplied by a constant, because this doesn’t change the line it represents. Now
divide A, B and C by their greatest common divisor so that they become relatively prime, and also
negate the entire equation if the first non-zero number is negative. With this we can build a
function/map f(line), which tells us how many points are on a line. Now it is easy to implement the
idea from the start of this paragraph, and using appropriate date structures, the complexity will be
good.
The second algorithm, after choosing the first point (where the right angle will be), sorts the remaining
points around it by angle. Now we can use two variables (the so-called “sweep” method) to count all
right triangles. We move the first variable point by point, and have the second one 90 degrees ahead of
it, moving forward, trying to form right triangles with the first variable.
The third algorithm is different from the first two, but also easier to implement. Choose a point P and
translate the coordinate plane so that the point P is the origin (more precisely, subtract the coordinates
of point P from every point). Now, for each point, first determine which quadrant it is in, and then
rotate it by 90 degrees until it is in the first quadrant. After that, sort all points by angle (y divided by x).
Two points form a right triangle with point P if they have the same angle and if they were in
neighbouring quadrants before rotating. After sorting, for each set of points with the same angle, count
how many of them were in each of the four quadrants and multiply the numbers for neighbouring
quadrants.
The complexity of all three algorithms is O(N2·logN). The official source code features the lastalgorithm.
相关文章推荐
- SpringMVC+Spring搭建web工程环境
- AbstractQueuedSynchronizer(五)——方法比较
- Java多线程中Sleep与Wait的区别
- 【Unity技巧】四元数(Quaternion)和旋转
- mysql不走索引总结
- 某航空公司有M个城市之间的全连通运营线路,最近业务扩张,新增了N(>1)个城市。为了保持其全连通运营的特色,公司新增了58种单程票(往与返各算一种单程票,没有联程票);那么扩张后,该航空公司有()个城
- Java EE 组件机制
- 直角三角形 纪中 1385 数学_斜率
- 百万计数据数据库处理
- Java的国际化与格式化
- 创建yum仓库
- android 全局变量Application类使用
- 144. Binary Tree Preorder Traversal
- 欢迎使用CSDN-markdown编辑器
- 发送验证码倒计时、图片验证码
- Java Enum类的笔记
- (java)socket全双工长连接通信实例
- SSI与Velocity解析时的区别
- cocos2d-x lua中实现异步加载纹理
- asp.net mvc本地程序集和GAC的程序集冲突解决方法