[省选前题目整理][BZOJ 1502]月下柠檬树(Simpson积分)

142 篇文章 0 订阅
31 篇文章 0 订阅

题目链接

http://www.lydsy.com/JudgeOnline/problem.php?id=1502

思路

http://blog.csdn.net/qpswwww/article/details/44204425

代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <cmath>

#define MAXN 550
#define EPS 1e-6

using namespace std;

int n;

struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y):x(_x),y(_y){}
}points[MAXN];

struct Circle
{
    Point o;
    double r;
    Circle(){}
    Circle(Point _o,double _r):o(_o),r(_r){}
}circles[MAXN];

struct Line
{
    Point st,ed;
    double k,b;
    Line(){}
    Line(Point _st,Point _ed):st(_st),ed(_ed)
    {
        if(st.x>ed.x) swap(st,ed);
        k=(st.y-ed.y)/(st.x-ed.x);
        b=st.y-st.x*k;
    }
    double F(double x)
    {
        return k*x+b;
    }
}lines[MAXN];

int tot=0; //直线总数

double f(double x)
{
    double ans=0;
    for(int i=1;i<=n;i++)
    {
        double dist=fabs(x-circles[i].o.x);
        if(dist-circles[i].r>-EPS) continue;
        double len=2*sqrt(circles[i].r*circles[i].r-dist*dist);
        ans=max(ans,len);
    }
    for(int i=1;i<=tot;i++)
        if(x>=lines[i].st.x&&x<=lines[i].ed.x)
            ans=max(ans,2*lines[i].F(x));
    return ans;
}

double calc(double len,double fL,double fM,double fR)
{
    return (fL+4*fM+fR)*len/6;
}

double Simpson(double L,double M,double R,double fL,double fM,double fR,double sqr)
{
    double M1=(L+M)/2,M2=(M+R)/2;
    double fM1=f(M1),fM2=f(M2);
    double g1=calc(M-L,fL,fM1,fM),g2=calc(R-M,fM,fM2,fR);
    if(fabs(g1+g2-sqr)<EPS) return g1+g2;
    return Simpson(L,M1,M,fL,fM1,fM,g1)+Simpson(M,M2,R,fM,fM2,fR,g2);
}

int main()
{
    double lowerBound=1e12,upperBound=-1e12;
    double alpha; //光线与地面夹角
    scanf("%d%lf",&n,&alpha);
    n++;
    for(int i=1;i<=n;i++)
    {
        double h;
        scanf("%lf",&h);
        circles[i].o.y=0;
        circles[i].o.x=h/tan(alpha);
        circles[i].o.x+=circles[i-1].o.x;
    }
    for(int i=1;i<n;i++)
        scanf("%lf",&circles[i].r);
    for(int i=1;i<=n;i++)
    {
        lowerBound=min(lowerBound,circles[i].o.x-circles[i].r);
        upperBound=max(upperBound,circles[i].o.x+circles[i].r); //!!!!!
    }
    for(int i=1;i<n;i++)
    {
        double dist=circles[i+1].o.x-circles[i].o.x;
        if(dist-fabs(circles[i+1].r-circles[i].r)<-EPS) continue; //一个圆内含在另一个圆里
        double sinalpha=(circles[i].r-circles[i+1].r)/dist;
        double cosalpha=sqrt(1-sinalpha*sinalpha);
        lines[++tot]=Line(Point(circles[i].o.x+circles[i].r*sinalpha,circles[i].r*cosalpha),Point(circles[i+1].o.x+circles[i+1].r*sinalpha,circles[i+1].r*cosalpha));
    }
    double L=lowerBound,R=upperBound;
    double M=(L+R)/2;
    double fL=f(L),fM=f(M),fR=f(R);
    printf("%.2lf\n",Simpson(L,M,R,fL,fM,fR,calc(R-L,fL,fM,fR)));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值