hdu4498Function Curve(求曲线长度)

Function Curve

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 615    Accepted Submission(s): 257


Problem Description
Given sequences of k 1, k 2, … k n, a 1, a 2, …, a n and b 1, b 2, …, b n. Consider following function:

Then we draw F(x) on a xy-plane, the value of x is in the range of [0,100]. Of course, we can get a curve from that plane.
Can you calculate the length of this curve?
 

Input
The first line of the input contains one integer T (1<=T<=15), representing the number of test cases.
Then T blocks follow, which describe different test cases.
The first line of a block contains an integer n ( 1 <= n <= 50 ).
Then followed by n lines, each line contains three integers k i, a i, b i ( 0<=a i, b i<100, 0<k i<100 ) .
 

Output
For each test case, output a real number L which is rounded to 2 digits after the decimal point, means the length of the curve.
 

Sample Input
 
 
2 3 1 2 3 4 5 6 7 8 9 1 4 5 6
 

Sample Output
 
 
215.56 278.91

思路:先将每条曲线的相交的交点求出来,相邻两个交点肯定会有一条曲线或者直线,将每个区间的中间值带入原方程如果最小值大于100,说明是直线,否则曲线,设抛物线方程为 f(x),那么 [L,R] 的曲线长度为 RLf(x)2+1dx,利用Simpson公式就能求出答案

代码:

#include<bits/stdc++.h>
using namespace std;
int n;
double k[105],a[105],b[105];
vector<double>v;
void findx(int x,int y)
{
    double aa=k[x]-k[y];
    double bb=2.0*(-a[x]*k[x]+a[y]*k[y]);
    double cc=k[x]*a[x]*a[x]-k[y]*a[y]*a[y]+b[x]-b[y];
    double kk=bb*bb-4.0*aa*cc;
    if(kk<0)return ;
    double x1=(-bb-sqrt(kk))/(2.0*aa);
    double x2=(-bb+sqrt(kk))/(2.0*aa);
    if(x1>=0&&x1<=100)v.push_back(x1);
    if(x2>=0&&x2<=100)v.push_back(x2);
}
double js(double x,int y)
{
    double ans=2.0*k[y]*(x-a[y]);
    return sqrt(ans*ans+1.0);
}
double cal(double L,double R,int x)
{
    double ans=0;
    ans=(R-L)/6.0*(js(L,x)+4.0*js((R+L)/2.0,x)+js(R,x));
    return  ans;
}
double simpson(double L,double R,int x)
{
    double mid=(L+R)/2;
    double s0=cal(L,mid,x);
    double s1=cal(mid,R,x);
    double s2=cal(L,R,x);
    if(fabs(s0+s1-s2)<1e-6)return s0+s1;
    return simpson(L,mid,x)+simpson(mid,R,x);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        v.clear();
        double ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf",&k[i],&a[i],&b[i]);
        }
        b[0]=100;

        v.push_back(0.0);
        v.push_back(100.0);
        for(int i=0;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                findx(i,j);
            }
        }
        sort(v.begin(),v.end());
        for(int i=1;i<v.size();i++)
        {
            double L=v[i-1];
            double R=v[i];
            double mid=(L+R)/2;
            double minn=100.0;
            int id=0;
            for(int j=0;j<=n;j++)
            {
                if(k[j]*(a[j]-mid)*(a[j]-mid)+b[j]<minn)
                {
                    minn=k[j]*(a[j]-mid)*(a[j]-mid)+b[j];
                    id=j;
                }
            }
            ans+=simpson(L,R,id);

        }
        printf("%.2f\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值