area 估算函数(simpson)

来源:互联网 发布:淘宝网灯具城 编辑:程序博客网 时间:2024/05/16 14:11

Problem 3. area
Input file: area.in
Output file: area.out
Time limit: 1 second
Mr.H最近有些无聊,就在纸上画了n 个开口向上的抛物线,并且这些抛物线与x 轴至多有一个交点。
通过这些函数,我们可以构造一个新的函数:
g(x) = min fi(x)(i=1..n)
现在,Mr.Hu 想问你在x 属于 [L,R] 这个范围内,g(x) 与x 轴围成的面积是多少。
Input
第1 行,2个整数n q,表示抛物线条数和询问次数。
接下来n 行,每行3 个数:a b c,表示fi(x) = ax^2 + bx + c。
接下来q 行,每行两个数:L R,表示一次询问。
Output
输出一行,包含一个数,表示面积,保留三位小数。
Note
. 对于30% 的数据,n = 1。
. 对于50% 的数据,n = 2。
. 对于100% 的数据,1  n  50,L  R,其他所有数的绝对值不超过50。

思路:
simpson裸题

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#define inf 1e100 using namespace std;const int N = 55;const double eps = 1e-8;int n, q;double a[N], b[N], c[N];double F( double x ) {//高度     double y = inf;    double xx = x * x;    for(int i=0; i<n; i++)         y = min(y, a[i] * xx + b[i] * x + c[i]);    return y;}inline double simpson( double a, double b, double fa, double fb, double fc ) {    return (fa + 4*fc + fb) * (b - a) / 6;}double adapt( double a, double b, double c, double fa, double fb, double fc ) {    double d = (a+c)/2, e = (c+b)/2;    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() {    freopen("area.in", "r", stdin);    freopen("area.out", "w", stdout);    scanf("%d%d", &n, &q);    for(int i = 0; i < n; i++)        scanf("%lf%lf%lf", a + i, b + i, c + i);    for(int i = 0; i < q; i++) {        double L, R;        scanf("%lf%lf", &L, &R);        printf("%.3f\n", adapt(L,R,(L+R)/2.0,F(L),F(R),F((L+R)/2.0)));    }}
原创粉丝点击