LA 4064 (计数 极角排序) Magnetic Train Tracks

时间:2022-01-17 21:00:57

这个题和UVa11529很相似。

枚举一个中心点,然后按极角排序,统计以这个点为钝角的三角形的个数,然后用C(n, 3)减去就是答案。

另外遇到直角三角形的情况很是蛋疼,可以用一个eps,不嫌麻烦的话就用整数的向量做点积。

 #include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; typedef long long LL;
const int maxn = + ;
const double PI = acos(-1.0);
const double eps = 1e-; struct Point
{
double x, y;
Point() {}
Point(double x, double y):x(x), y(y) {}
}p[maxn], p2[maxn]; Point operator - (const Point& A, const Point& B)
{
return Point(A.x-B.x, A.y-B.y);
} double ang[maxn * ]; LL inline C3(int n) { return (LL)n * (n-) / * (n-) / ; } int main()
{
//freopen("in.txt", "r", stdin);
int n, kase = ; while(scanf("%d", &n) == && n)
{
for(int i = ; i < n; i++) scanf("%lf%lf", &p[i].x, &p[i].y); LL cnt = ;
for(int i = ; i < n; i++)
{
int k = ;
for(int j = ; j < n; j++) if(j != i)
{
p2[k] = p[j] - p[i];
ang[k] = atan2(p2[k].y, p2[k].x);
ang[k + n - ] = ang[k] + PI * 2.0;
k++;
}
k = *n-;
sort(ang, ang + k);
int L, R1 = , R2 = ;
for(L = ; L < n-; L++)
{
double b1 = ang[L] + PI / ;
double b2 = ang[L] + PI;
while(ang[R1] <= b1 - eps) R1++;
while(ang[R2] < b2) R2++;
cnt += R2 - R1;
}
}
LL ans = C3(n) - cnt;
printf("Scenario %d:\nThere are %lld sites for making valid tracks\n", ++kase, ans);
} return ;
}

代码君