码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 11186 - Circum Triangle(计算几何+容斥)

时间:2015-03-28 10:07:48      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

这题用n^2的算法能过,先任意枚举两点,和圆心组成的三角形求面积,这个面积可能会被加(n - 2)次,但是要注意,如果有3点是在同一侧,那么要减去,于是在枚举一遍,每次枚举一个点,然后枚举和这个点度数相差180以内的点,求面积,这个面积要减去2 * (j - i + 1)次

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int N = 505;
const double PI = acos(-1.0);

int n;
double r, d[N * 2];

struct Point {
    double x, y;
    Point() {}
    Point(double x, double y) {
        this->x = x;
        this->y = y;
    }
};

typedef Point Vector;

Vector operator - (Vector A, Vector B) {
    return Vector(A.x - B.x, A.y - B.y);
}

inline double Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;} //叉积
inline double Area2(Point A, Point B, Point C) {return Cross(B - A, C - A);} //有向面积
inline double Are(Point A, Point B, Point C) {return fabs(Area2(A, B, C)) / 2;}
inline Point getP(double d) {
    double rad = d / 180 * PI;
    return Point(cos(rad) * r, sin(rad) * r);
}

int main() {
    while (~scanf("%d%lf", &n, &r) && n || r) {
        for (int i = 0; i < n; i++) scanf("%lf", &d[i]);
        sort(d, d + n);
        double ans = 0;
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++)
                ans += Are(Point(0, 0), getP(d[i]), getP(d[j])) * (n - 2);
        for (int i = 0; i < n; i++)
            d[i + n] = d[i] + 360;
        for (int i = 0; i < n; i++) {
            for (int j = i + 2; d[j] - d[i] < 180; j++) {
                ans -= 2 * Are(Point(0, 0), getP(d[i]), getP(d[j % n])) * (j - i - 1);
            }
        }
        printf("%.0f\n", ans);
    }
    return 0;
}


UVA 11186 - Circum Triangle(计算几何+容斥)

标签:

原文地址:http://blog.csdn.net/accelerator_/article/details/44698869

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!