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. Codeforces 463D

    题目链接 D. Gargari and Permutations time limit per test 2 seconds memory limit per test 256 megabytes i ...

  2. 在 IE 选项那提示 ”某些设置由系统管理员进行管理” 解决

    运行 regedit 打开注册表 修改注册表值HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\CurrentVersion\Interne ...

  3. Leetcode79. Word Search单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

  4. Liferay 7:portlet name

    总结自: https://web.liferay.com/zh/web/user.26526/blog/-/blogs/proper-portlet-name-for-your-portlet-com ...

  5. ML面试1000题系列(91-100)

    本文总结ML面试常见的问题集 转载来源:https://blog.csdn.net/v_july_v/article/details/78121924 91 简单说说RNN的原理?我们升学到高三准备高 ...

  6. CSS Tools: Reset CSS

    样式初始化 /* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: none (public domain) */ ...

  7. Android中使用ormlite实现持久化--HelloOrmLite

    Android中内置了sqlite,但是常用的开发语言java是面向对象的,而数据库是关系型的,二者之间的转化每次都很麻烦(主要是我对sql语言不熟悉).而Java Web开发中有很多orm框架,但是 ...

  8. 电脑上做的ppt拿到别的电脑或手机上播放的时候字体错位的解决方法

    原因:字体不对!!! 比如你英文用的Calibri字体,但是手机的wps或者别的电脑上的低版本的office没有这个字体,所以就会强制转换成那里有的字体(一般是黑体),此时字体就会错位!! 不要以为那 ...

  9. awk notes

    字符串拼接 cat sql | awk '{print " ALTER TABLE  tableA   ALTER " $1 " TYPE " $2 " ...

  10. C++ int与string的互转

    int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省情况下,是当成10进制(dec)来解释,如果想用8进制,16进制,怎么办?加上前缀,告诉编译器按照不同进制去解释.8进制 ...