【ACM ICPC World Finals 2012】 Curvy Little Bottles (二分)

本文介绍了一种计算特殊形状瓶子上液体体积刻度的方法。通过给定的多项式方程来定义瓶子形状,并计算不同高度对应的体积增量。适用于如测量瓶等需要精确刻度的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


In her bike rides around Warsaw, Jill happened upon a shop that sold interesting glass bottles. She thought it might make an interesting project to use such bottles for measuring liquids, but this would require placing markings on the bottles to indicate various volumes. Where should those volume marks be placed?

Jill formalized the problem as follows. Assume a bottle is formed by revolving a shape that is the same as the graph of a polynomialP between x = xlow and x = xhigh around the x-axis. Thus the x-axis is coincident with a vertical line through the center of the bottle. The bottom of the bottle is formed by a solid circular region at x = xlow , and the top of the bottle, at x = xhigh, is left open.

The first sample input represents a bottle formed using the simple polynomial 4 - 0.25x, with xlow = 0 and xhigh = 12. The bottom of this bottle is a circle with a radius of 4, and the opening at the top is a circle with a radius of 1. The height of this bottle is 12. Volume markings are in increments of 25.

Given a polynomial Pxlowxhigh, and the volume increment between successive marks on the bottle, compute the distances up from xlow for the marks at successive volume increments. A mark cannot be made past the top of the bottle, and no more than the first 8 increments should be marked. Assume the value of P is greater than zero everywhere between xlow and xhigh.

Input

Each test case consists of three lines of bottle data:

  • Line 1: n, the degree of the polynomial (an integer satisfying 0$ \le$n$ \le$10).
  • Line 2: a0a1,..., an, the real coefficients of the polynomial P defining the bottle's shape, where a0 is the constant term, a1 is the coefficient of x1,..., and an is the coefficient of xn. For each i, -100$ \le$ai$ \le$100, and an $ \neq$ 0.
  • Line 3:
    o
    xlow and xhigh, the real valued boundaries of the bottle (- 100$ \le$xlow < xhigh$ \le$100 and xhigh - xlow > 0.1).
    o
    inc, an integer which is the volume increment before each successive mark on the bottle (1$ \le$inc$ \le$500).

Output

For each test case, display the case number and the volume of the full bottle on one line. On a second line, display the increasing sequence of no more than 8 successive distances up from the bottom of the bottle for the volume markings. All volumes and height marks should be accurate to two decimal places. If the bottle does not have a volume that allows at least one mark, display the phrase `insufficient volume'. No test case will result in a mark within 0.01 from the top of the bottle. The volume of the bottle will not exceed 1 000. All rounded distances for marks on a bottle differ by at least 0.05.

Sample Input

1
4.0 -0.25
0.0 12.0 25
1
4.0 -0.25
0.0 12.0 300
0
1.7841241161782
5.0 10.0 20
0
1.0
0.0 10.0 10

Sample Output

Case 1: 263.89
0.51 1.06 1.66 2.31 3.02 3.83 4.75 5.87
Case 2: 263.89
insufficient volume
Case 3: 50.00
2.00 4.00
Case 4: 31.42
3.18 6.37 9.55
代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <string>
#include <stdlib.h>
#include <limits.h>
#include <iostream>
#include <algorithm>
#define len 14
#define pi acos(-1.0)
#define eps 1e-6
using namespace std;

double getV(double a[], int n,double x1,double x2)
{
    double ans=0.0;
    double x_1=1.0,x_2=1.0;

    for(int i=0; i<=2*n; ++i)
    {
        x_1*=x1;
        x_2*=x2;
        ans+=a[i]*(x_2-x_1)/(i+1);
    }

    return ans*pi;
}

double getMark(double a[],int n,double x1,double x2,int mark)
{
    double x_1=x1;
    double mid=x2;
    double dif=getV(a,n,x_1,mid)-mark;
    if(dif>0)
    {
        while(fabs(dif)>eps)
        {
            mid=(x1+x2)/2;
            dif=getV(a,n,x_1,mid)-mark;

            if(dif>0)
                x2=mid;
            else
                x1=mid;
        }
    }
    return mid;

}

double a[len],b[2*len],c[10] ;

int main()
{
    int n,mark,k=0,count;
    double x_high,x_low,res,x0,_x;
    while(scanf("%d",&n)!=EOF)
    {
        count=0;
        k++;

        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        for(int i=0; i<=n; ++i)
            scanf("%lf",&a[i]);
        for(int i=0; i<=n; ++i)
            for(int j=0; j<=n; ++j)
                b[i+j]+=a[i]*a[j];

        scanf("%lf%lf%d",&x_low,&x_high,&mark);

        printf("Case %d: ",k);
        res=getV(b,n,x_low,x_high);
        printf("%.2lf\n",res);
        if(res<mark)
            printf("insufficient volume\n");
        else
        {
            x0=x_low;
            while(count<8)
            {
                _x=getMark(b,n,x_low,x_high,mark);

                if(_x<x_high)
                {
                    c[++count]=_x-x0;
                    x_low=_x;
                }
                else
                    break;
            }
            for(int i=1; i<=count; ++i)
                printf((i==count)?"%.2lf\n":"%.2lf ",c[i]);

        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值