bzoj1502 simpson求面积
题目: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求面积的更多相关文章
- poj 3348--Cows(凸包求面积)
链接:http://poj.org/problem?id=3348 Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: ...
- P - Atlantis - hdu1542(求面积)
题意:rt 求面积......不计算重复面积(废话..)hdu1255 的弱化版,应该先做这道题在做那道题的. ******************************************** ...
- 编写一个矩形类,私有数据成员为矩形的长( len)和宽(wid),wid设置为0,有参构造函数设置和的值,另外,类还包括矩形的周长、求面积、取矩形的长度、取矩形的长度、取矩形的宽度、修改矩形的长度和宽度为对应的形参值等公用方法。
class Rectangle { private double len, wid; public Rectangle()//求矩形周长 { len = 0; wid = 0; } public Re ...
- HDU - 1255 覆盖的面积 (线段树求面积交)
https://cn.vjudge.net/problem/HDU-1255 题意 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 分析 求面积并的题:https://www.cnbl ...
- 覆盖的面积 HDU - 1255(扫描线求面积交)
题意: 就是扫描线求面积交 解析: 参考求面积并.... 就是把down的判断条件改了一下..由w > 0 改为 w > 1 同时要讨论一下 == 1 时 的情况, 所以就要用到一个临时 ...
- hdu1542 Atlantis 线段树--扫描线求面积并
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
- 两条线段求交点+叉积求面积 poj 1408
题目链接:https://vjudge.net/problem/POJ-1408 题目是叫我们求出所有四边形里最大的那个的面积. 思路:因为这里只给了我们正方形四条边上的点,所以我们要先计算横竖线段两 ...
- 牛客训练二:处女座的签到题(STL+精度+三角形求面积公式)
题目链接:传送门 知识点: (1)三个点,三角形求面积公式 (2)精度问题: double 15-16位(参考文章) float 6-7位 long long 约20位 int 约10位 unsign ...
- Herding(hdu4709)三点运用行列式求面积
Herding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- H5调试工具 - weinre远程调试工具
weinre 简介 weinre 是一款类似于firebug 和Web Inspector的网页调试工具, 它的不同之处在于可以用于进行远程调试,比如调试手机上面的网页. 安装 weinre(运行在n ...
- bootstrap带图标的按钮与图标做连接
bootstrap通过引入bootstrap的JS与css文件,给元素添加class属性即可. 使用图标只需要加入一个span,class属性设置为对应的图标属性即可.图标对应的class属性可以参考 ...
- 宋牧春: Linux设备树文件结构与解析深度分析(1) 【转】
转自:https://mp.weixin.qq.com/s/OX-aXd5MYlE_YoZ3p32qWA 作者简介 宋牧春,linux内核爱好者,喜欢阅读各种开源代码(uboot.linux.ucos ...
- python多线程下载文件
从文件中读取图片url和名称,将url中的文件下载下来.文件中每一行包含一个url和文件名,用制表符隔开. 1.使用requests请求url并下载文件 def download(img_url, i ...
- Linux软件管理器(如何使用软件管理器来管理软件)2---安装及管理Linux应用程序
安装及管理Linux应用程序 Linux应用程序的组成1.普通的可执行程序文件,一般保存在/usr/bin目录中,普通用户即可执行.2.服务器程序.管理程序文件,一般保存在/usr/sbin目录中,需 ...
- linux 下配置文件目录/etc/sysconfig
/etc/sysconfig/目录详解 2010-06-19 11:12 6693人阅读 评论(1) 收藏 举报 桌面环境debugging防火墙serviceunix语言 /etc/sysconfi ...
- SAM-Toy Cars题解
题目描述 Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio 拿不到它们. 为了让他的房间有足够的空间,在任何时刻地板上都不会有超过k 个 ...
- objective-c Quick Reference
- HashMap碰撞问题
HashMap是最常用的集合类框架之一,它实现了Map接口,所以存储的元素也是键值对映射的结构,并允许使用null值和null键,其内元素是无序的,如果要保证有序,可以使用LinkedHashMap. ...
- 转:Heap spraying high addresses in 32-bit Chrome/Firefox on 64-bit Windows
转:https://blog.skylined.nl/20160622001.html,June 22nd, 2016 In my previous blog post I wrote about m ...