题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1502

题解:

simpson积分求面积,s = (f(a)+f(b)+4*f(c))/6*Δx ,c=(a+b)/2。

题中的树投影下来是一些圆和相邻圆的公切线组成的一个封闭图形,并且上下对称,所以可以只求上半部分。

simpson求面积时,若f(x)代价很大,要尽量减少其的重复调用。其次尽量优化f(x)函数的计算。

写完后还要自己出极限随机数据,将eps调到能接受的最大值。

 #include <cstdio>
#include <cmath>
#include <iostream>
#define maxn 510
#define eps 1e-6
using namespace std; int sg( double x ) {
return (x>-eps)-(x<eps);
}
struct Vector {
double x, y;
Vector(){}
Vector( double x, double y ) : x(x), y(y) {}
Vector operator+( Vector b ) { return Vector(x+b.x,y+b.y); }
Vector operator-( Vector b ) { return Vector(x-b.x,y-b.y); }
Vector operator*( double b ) { return Vector(x*b,y*b); }
double len() {
return sqrt( x*x+y*y );
}
};
typedef Vector Point; struct Line {
Point A, B;
double k, b;
double lf, rg;
Line(){}
Line( Point A, Point B ):A(A),B(B) {
if( A.x>B.x ) swap(A,B);
lf = A.x;
rg = B.x;
k = (B.y-A.y)/(B.x-A.x);
b = A.y-A.x*k;
}
inline bool in( double x ) {
return sg(x-lf)>= && sg(rg-x)>=;
}
inline double f( double x ) { return k*x+b; }
void out() {
printf( "(%lf,%lf) (%lf,%lf) \n", A.x, A.y, B.x, B.y );
}
};
struct Circle {
Point C;
double r;
double lf, rg;
Circle(){}
void make() {
lf = C.x-r;
rg = C.x+r;
}
inline bool in( double x ) {
return sg(x-lf)>= && sg(rg-x)>=;
}
inline double f( double x ) {
return sqrt( r*r-(C.x-x)*(C.x-x) );
}
void out() {
printf( "(%lf,%lf) r=%lf\n", C.x, C.y, r );
}
}; int n;
double alpha;
Line lines[maxn];
Circle cirs[maxn]; Line cir_tang( Circle & a, Circle & b ) {
double ang = acos( (a.r-b.r)/(b.C.x-a.C.x) );
Point A = a.C + Vector(cos(ang),sin(ang))*a.r;
Point B = b.C + Vector(cos(ang),sin(ang))*b.r;
return Line(A,B);
} double F( double x ) {
double y = -1.0;
for( int i=; i<=n; i++ ) {
if( cirs[i].lf-eps<=x && x<=cirs[i].rg+eps )
y = max( y, cirs[i].f(x) );
if( i&& lines[i].lf-eps<=x && x<=lines[i].rg+eps )
y = max( y, lines[i].f(x) );
}
return y;
}
inline double simpson( double a, double b, double fa, double fb, double fc ) {
return (fa+*fc+fb)*(b-a)/;
}
double adapt( double a, double b, double c, double fa, double fb, double fc ) {
double d = (a+c)/, e = (c+b)/;
double fd = F(d), fe = F(e);
double sa = simpson(a,c,fa,fc,fd);
double sb = simpson(c,b,fc,fb,fe);
double ss = simpson(a,b,fa,fb,fc);
if( fabs(sa+sb-ss)<*eps ) return sa+sb;
return adapt(a,c,d,fa,fc,fd)+adapt(c,b,e,fc,fb,fe);
} int main() {
scanf( "%d%lf", &n, &alpha );
double k = /tan(alpha);
double hsum = 0.0, h;
double lf=1e200, rg=-1e200;
for( int i=; i<=n; i++ ) {
scanf( "%lf", &h );
hsum += h;
cirs[i].C.x = hsum*k;
cirs[i].C.y = 0.0;
}
for( int i=; i<n; i++ ) {
scanf( "%lf", &cirs[i].r );
cirs[i].make();
lf = min( lf, cirs[i].lf );
rg = max( rg, cirs[i].rg );
}
cirs[n].r = 0.0;
rg = max( rg, cirs[n].C.x );
for( int i=; i<=n; i++ )
lines[i] = cir_tang( cirs[i-], cirs[i] );
printf( "%.2lf\n", adapt(lf,rg,(lf+rg)/,F(lf),F(rg),F((lf+rg)/) )* );
}

bzoj1502 simpson求面积的更多相关文章

  1. poj 3348--Cows(凸包求面积)

    链接:http://poj.org/problem?id=3348 Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions:  ...

  2. P - Atlantis - hdu1542(求面积)

    题意:rt 求面积......不计算重复面积(废话..)hdu1255 的弱化版,应该先做这道题在做那道题的. ******************************************** ...

  3. 编写一个矩形类,私有数据成员为矩形的长( len)和宽(wid),wid设置为0,有参构造函数设置和的值,另外,类还包括矩形的周长、求面积、取矩形的长度、取矩形的长度、取矩形的宽度、修改矩形的长度和宽度为对应的形参值等公用方法。

    class Rectangle { private double len, wid; public Rectangle()//求矩形周长 { len = 0; wid = 0; } public Re ...

  4. HDU - 1255 覆盖的面积 (线段树求面积交)

    https://cn.vjudge.net/problem/HDU-1255 题意 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 分析 求面积并的题:https://www.cnbl ...

  5. 覆盖的面积 HDU - 1255(扫描线求面积交)

    题意: 就是扫描线求面积交 解析: 参考求面积并.... 就是把down的判断条件改了一下..由w > 0 改为 w > 1 同时要讨论一下 == 1 时  的情况, 所以就要用到一个临时 ...

  6. hdu1542 Atlantis 线段树--扫描线求面积并

    There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...

  7. 两条线段求交点+叉积求面积 poj 1408

    题目链接:https://vjudge.net/problem/POJ-1408 题目是叫我们求出所有四边形里最大的那个的面积. 思路:因为这里只给了我们正方形四条边上的点,所以我们要先计算横竖线段两 ...

  8. 牛客训练二:处女座的签到题(STL+精度+三角形求面积公式)

    题目链接:传送门 知识点: (1)三个点,三角形求面积公式 (2)精度问题: double 15-16位(参考文章) float 6-7位 long long 约20位 int 约10位 unsign ...

  9. Herding(hdu4709)三点运用行列式求面积

    Herding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. CTF AWD模式攻防Note

    ###0x01 AWD模式 Attack With Defence,简而言之就是你既是一个hacker,又是一个manager.比赛形式:一般就是一个ssh对应一个web服务,然后flag五分钟一轮, ...

  2. Redis—初探Redis

    一.什么是Redis? 学习Redis最好的是看官网了,下面是Redis的官网对Redis的介绍 可见,Redis是一个内存存储的数据结构服务器,可以用作数据库.缓存等.支持的数据结构也很丰富,有字符 ...

  3. python脚本-实现自动按规则创建指定大小和指定个数的文件案例

    # -*- coding: cp936 -*-#---------------------------------------------------------------------------- ...

  4. shell脚本自带变量的含义

    $0 Shell本身的文件名 $1-$n 添加到Shell的各参数值.$1是第1参数.$2是第2参数… $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process ...

  5. 短信API——短信验证码

    简介 短信服务(Short Message Service.SMS)是指通过调用短信发送API,将指定短信内容发送给指定手机用户. 阿里云短信服务 阿里云短信服务产品介绍:https://www.al ...

  6. jquery 通过 live() 方法附加的事件处理程序适用于匹配选择器的当前及未来的元素(比如由脚本创建的新元素)

    jquery 通过 live() 方法附加的事件处理程序适用于匹配选择器的当前及未来的元素(比如由脚本创建的新元素) $("ul").append("<li cla ...

  7. python3.6升级及setuptools、pip安装

    升级python3.6 1.打开官网www.python.org,找到最新3.6.3版本,复制下载链接 2.创建/app目录,wget下载到该目录下,编译安装 mkdir /app cd /app w ...

  8. go语言入门(三)

    条件语句 go语言的条件语句结构如下: go语言的条件语句和其他语言类似.简单列举下: 1.if 语句,布尔表达式不需要括号 if 布尔表达式 { /* 在布尔表达式为 true 时执行 */ } 2 ...

  9. 数据分析python应用到的ggplot

    数据分析中应用到python中的ggplot库,可以用来画图 数据之类的用优达学院中课程七中的数据为例 数据是:https://s3.amazonaws.com/content.udacity-dat ...

  10. Ceph 时钟偏移问题 clock skew detected 解决方案--- 部署内网NTP服务

    告警:HEALTH_WARN clock skew detected on mon.ip-10-25-195-6; 8 requests are blocked > 32 sec; Monito ...