Sphere Online Judge (SPOJ) - Problem CIRU

【求圆并的若干种算法,圆并扩展算法】_AekdyCoin的空间_百度空间

  参考AekdyCoin的圆并算法解释,根据理解写出的代码。圆并这么多题中,最基础一题。

  操作如下:

(1)对一个圆,获得所有与其他圆的交点作为时间戳。

  a.如果这个圆不被其他任何圆覆盖,这个圆直接保留。

  b.如果Case中有两个完全重合的圆,可以保留标号小(或大)的圆,也只保留这一个。

(2)每经过一个时间戳就对计数器加减,如果计数器从0变1,加上这段圆弧和对应的三角形。

(3)所有这样的圆独立计算,最后求出的面积累加即可。

代码如下(1y):

 #include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm> using namespace std; const double PI = acos(-1.0);
const double EPS = 1e-;
inline int sgn(double x) { return (x > EPS) - (x < -EPS);}
template<class T> T sqr(T x) { return x * x;}
typedef pair<double, double> Point;
#define x first
#define y second
Point operator + (Point a, Point b) { return Point(a.x + b.x, a.y + b.y);}
Point operator - (Point a, Point b) { return Point(a.x - b.x, a.y - b.y);}
Point operator * (Point a, double p) { return Point(a.x * p, a.y * p);}
Point operator / (Point a, double p) { return Point(a.x / p, a.y / p);} inline double cross(Point a, Point b) { return a.x * b.y - a.y * b.x;}
inline double dot(Point a, Point b) { return a.x * b.x + a.y * b.y;}
inline double angle(Point a) { return atan2(a.y, a.x);}
inline double veclen(Point a) { return sqrt(dot(a, a));} struct Circle {
Point c;
double r;
Circle() {}
Circle(Point c, double r) : c(c), r(r) {}
Point point(double p) { return Point(c.x + r * cos(p), c.y + r * sin(p));}
void get() { scanf("%lf%lf%lf", &c.x, &c.y, &r);}
} ; bool ccint(Circle a, Circle b, double &a1, double &a2) { // ips for Circle-a (anti-clockwise p1->p2)
double d = veclen(a.c - b.c);
double r1 = a.r, r2 = b.r;
if (sgn(d - r1 - r2) >= ) return ;
if (sgn(fabs(r1 - r2) - d) >= ) return ;
double da = acos((sqr(d) + sqr(r1) - sqr(r2)) / ( * d * r1));
double ang = angle(b.c - a.c);
a1 = ang - da, a2 = ang + da;
return ;
} const int N = ;
Circle cir[N]; inline double gettri(Point o, Point a, Point b) { return cross(a - o, b - o) / ;}
inline double getarc(Circle o, double a, double b) { return (b - a) * sqr(o.r) / - gettri(o.c, o.point(a), o.point(b));} typedef pair<double, int> Event;
Event ev[N << ]; bool inside(int a, int n) {
double d, r1, r2;
for (int i = ; i < n; i++) {
if (i == a) continue;
d = veclen(cir[i].c - cir[a].c);
r1 = cir[i].r, r2 = cir[a].r;
if (sgn(r1 - r2) == && sgn(fabs(r1 - r2) - d) == ) {
if (i > a) return ;
continue;
}
if (sgn(r1 - r2) >= && sgn(fabs(r1 - r2) - d) >= ) return ;
}
return ;
} double getarea(int a, int n) {
if (inside(a, n)) return ;
Circle &c = cir[a];
double ret = ;
double a1, a2;
Point p1, p2;
int tt = ;
for (int i = ; i < n; i++) {
if (i == a) continue;
if (ccint(c, cir[i], a1, a2)) {
if (a2 > PI) ev[tt++] = Event(a1, ), ev[tt++] = Event(PI, -), ev[tt++] = Event(-PI, ), ev[tt++] = Event(a2 - * PI, -);
else if (a1 < -PI) ev[tt++] = Event(a1 + * PI, ), ev[tt++] = Event(PI, -), ev[tt++] = Event(-PI, ), ev[tt++] = Event(a2, -);
else ev[tt++] = Event(a1, ), ev[tt++] = Event(a2, -);
}
}
if (tt == ) return PI * sqr(c.r);
sort(ev, ev + tt);
int cnt = ;
double last = -PI;
for (int i = ; i < tt; i++) {
double cur = ev[i].x;
if (ev[i].y == && cnt == ) ret += getarc(c, last, cur) + cross(c.point(last), c.point(cur)) / ;
cnt += ev[i].y;
last = cur;
}
ret += getarc(c, last, PI) + cross(c.point(last), c.point(PI)) / ;
return ret;
} double work(int n) {
double ret = ;
for (int i = ; i < n; i++) ret += getarea(i, n);
return ret;
} int main() {
int n;
while (~scanf("%d", &n)) {
for (int i = ; i < n; i++) cir[i].get();
printf("%.3f\n", work(n));
}
return ;
}

——written by Lyon

SPOJ 8073 The area of the union of circles (圆并入门)的更多相关文章

  1. SPOJ 8073 The area of the union of circles(计算几何の圆并)(CIRU)

    Description You are given N circles and expected to calculate the area of the union of the circles ! ...

  2. SPOJ CIRU - The area of the union of circles (圆的面积并)

    CIRU - The area of the union of circles no tags  You are given N circles and expected to calculate t ...

  3. SPOJ CIRU The area of the union of circles

    You are given N circles and expected to calculate the area of the union of the circles ! Input The f ...

  4. SPOJ CIRU The area of the union of circles (计算几何)

    题意:求 m 个圆的并的面积. 析:就是一个板子题,还有要注意圆的半径为0的情况. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024 ...

  5. SPOJ CIRU The area of the union of circles ——Simpson积分

    [题目分析] 圆的面积并. 直接Simpson积分,(但是有计算几何的解法,留着flag). simpson积分,如果圆出现了不连续的情况,是很容易出事情的.(脑补一下) 但是没有什么办法,本来就是一 ...

  6. 【题解】CIRU - The area of the union of circles [SP8073] \ 圆的面积并 [Bzoj2178]

    [题解]CIRU - The area of the union of circles [SP8073] \ 圆的面积并 [Bzoj2178] 传送门: \(\text{CIRU - The area ...

  7. [SPOJ-CIRU]The area of the union of circles/[BZOJ2178]圆的面积并

    [SPOJ-CIRU]The area of the union of circles/[BZOJ2178]圆的面积并 题目大意: 求\(n(n\le1000)\)个圆的面积并. 思路: 对于一个\( ...

  8. SPOJ CIRU SPOJ VCIRCLE 圆的面积并问题

    SPOJ VCIRCLE SPOJ CIRU 两道题都是给出若干圆 就面积并,数据规模和精度要求不同. 求圆面积并有两种常见的方法,一种是Simpson积分,另一种是几何法. 在这里给出几何方法. P ...

  9. POJ1151Atlantis 矩形面积并[线段树 离散化 扫描线]

    Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21734   Accepted: 8179 Descrip ...

随机推荐

  1. ubuntu忘记root密码

    解决办法: 选择GRUB第2个选项(恢复模式) 按e进入编辑模式 将ro recovery nomodeset修改成rw single init=/bin/bash 然后再按F10进入单用户模式,进入 ...

  2. case expressions must be constant expressions

    As the error message states, the case expressions must be constant. The compiler builds this as a ve ...

  3. java并发系列(一)-----多线程简介、创建以及生命周期

    进程.线程与任务 进程:程序的运行实例.打开电脑的任务管理器,如下: 正在运行的360浏览器就是一个进程.运行一个java程序的实质是启动一个java虚拟机进程,也就是说一个运行的java程序就是一个 ...

  4. python 读取xml文件

    首先,获得标签信息abc.xml <?xml version="1.0" encoding="utf-8"?> <catalog> &l ...

  5. hadoop-hive查询ncdc天气数据实例

    使用hive查询ncdc天气数据 在hive中将ncdc天气数据导入,然后执行查询shell,可以让hive自动生成mapredjob,快速去的想要的数据结果. 1. 在hive中创建ncdc表,这个 ...

  6. 创建多个Django业务模块

    manage.py startapp cmdb 如果运行错误记得加环境变量 c:\python35\python.exe manage.py startapp cmdb

  7. bzoj2049: [Sdoi2008]Cave 洞穴探测

    bzoj2049: [Sdoi2008]Cave 洞穴探测 给n个点,每次连接两个点或切断一条边,保证是树结构,多次询问两个点是否联通 Lct裸题 //Achen #include<algori ...

  8. Visual studio加载项目时报错 尚未配置为Web项目XXXX指定的本地IIS,需要配置虚拟目录。解决办法。

    在SVN上下载工程项目.使用visual studio打开时,出现如下提示: 查找相关资料,解决办法如下: 使用记事本打开工程目录下的.csproj文件.把<UseIIS>False< ...

  9. mac 下的 homebrew

    如果安装了macport 就不能安装homebrew ,必须先卸载macport $ sudo port -f uninstall installed$ sudo rm -rf \/opt/local ...

  10. js中字符串的加密base64

    base64编码主要用在传输,存储表示二进制的领域,还可以进行加密和解密.其实就是字符串的编码和解码 btoa与atob 只能加密ascii,不能加密汉字. var str = 'I LOVE YOU ...