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 .
3≤n≤2000
0≤xi,yi≤1e9
Any two points will not coincide.
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
题意:给定二维平面上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
(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;
}