area 估算函数(simpson)

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)));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值