题目: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. 37 - 网络编程-UDP编程

    目录 1 UDP协议 2 UDP通信流程 3 UDP编程 3.1 构建服务端 3.3 常用方法 4 聊天室 5 UDP协议应用 1 UDP协议 UDP是面向无连接的协议,使用UDP协议时,不需要建立连 ...

  2. Git远程操作详解【转】

    转自:http://www.ruanyifeng.com/blog/2014/06/git_remote.html 作者: 阮一峰 日期: 2014年6月12日 Git是目前最流行的版本管理系统,学会 ...

  3. 细说show slave status参数详解(最全)【转】

    在搭建好mysql主从之后,我们一般在从库上通过命令 show slave status\G 来查看主从的状态,会有很多的参数,接下来笔者就带大家好好的了解这些参数 root@localhost (n ...

  4. tracert和traceroute使用

    Traceroute提取发 ICMP TTL到期消息设备的IP地址并作域名解析.每次 ,Traceroute都打印出一系列数据,包括所经过的路由设备的域名及 IP地址,三个包每次来回所花时间. 转自 ...

  5. #题目:有10 台被监控主机、一台监控机,在监控机上编写脚本,一旦某台被监控机器/ 分区适用率大于80%, 就发邮件报警放到crontab 里面, 每10 分钟检查一次

    #题目:有10 台被监控主机.一台监控机,在监控机上编写脚本,一旦某台被监控机器/ 分区适用率大于80%, 就发邮件报警放到crontab 里面, 每10 分钟检查一次 #测试机器:虚拟机Linux ...

  6. 9.Python3标准库--数据压缩与归档

    ''' 尽管现代计算机系统的存储能力日益增长,但生成数据的增长是永无休止的. 无损(lossless)压缩算法以压缩或解压缩数据花费的时间来换取存储数据所需要的空间,以弥补存储能力的不足. Pytho ...

  7. pandas安装及使用

    一. 安装pandas1. Anaconda        安装pandas.Python和SciPy最简单的方式是用Anaconda.Anaconda是关于Python数据分析和科学计算的分发包.2 ...

  8. (五)Spring 对事务的支持

    第一节:事务简介 满足一下四个条件: 第一:原子性: 第二:一致性: 第三:隔离性: 第四:持久性: ------------------------------------------------- ...

  9. CMS(Concurrent Mark-Sweep)垃圾回收器

    http://www.iteye.com/topic/1119491 1.总体介绍: CMS(Concurrent Mark-Sweep)是以牺牲吞吐量为代价来获得最短回收停顿时间的垃圾回收器.对于要 ...

  10. Django 中form的用法

    form的主要作用:1.在html中生成表单框架,2.验证数据(实话实说,很简洁,但不实用,灵活性差) from django.db import models # Create your model ...