24h購物| | PChome| 登入
2013-10-11 08:00:13| 人氣1,594| 回應0 | 上一篇 | 下一篇

[UVA][積分&二分] 1280 - Curvy Little Bottles

推薦 0 收藏 0 轉貼0 訂閱站台

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 polynomial P 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 P, xlow, xhigh, 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: a0, a1,..., 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

題目描述:
要請你將燒瓶畫上刻度,而燒瓶給定的方式為一個在[xlow, xhight] 恆在 x 軸上方的多項式,
該多項式繞著 x 軸旋轉而成,xlow 處表示燒杯底部。

每個刻度間的體積差為指定容量,印出最多前 8 個刻度。

題目解法:


先解出積分方程,
integral(p^2 * pi)dx

二分下一個指定的刻度。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
const double pi = acos(-1);
double calcPoly(double p[], double x, int n) {
double ret = 0, y = 1;
int i;
for(i = 0; i <= n; i++)
ret += p[i]*y, y *= x;
return ret;
}
#define eps 1e-8
int main() {
int n, cases = 0;
int i, j, k;
double xlow, xhigh, inc;
double a[25], b[25], c[25];
while(scanf("%d", &n) == 1) {
for(i = 0; i <= n; i++)
scanf("%lf", &a[i]);
scanf("%lf %lf %lf", &xlow, &xhigh, &inc);
memset(b, 0, sizeof(b));// b = a^2
memset(c, 0, sizeof(c));// c = integral of b.
for(i = 0; i <= n; i++)
for(j = 0; j <= n; j++)
b[i+j] += a[i]*a[j];
for(i = 0; i <= 2*n; i++)
c[i+1] = b[i]/(i+1);
n = 2*n+1;
double volume = (calcPoly(c, xhigh, n)-calcPoly(c, xlow, n))*pi;
printf("Case %d: %.2lf\n", ++cases, volume);
int marking = min((int)floor(volume/inc), 8);
if(marking == 0)
puts("insufficient volume");
else {
double prev = xlow;
for(i = 0; i < marking; i++) {
double l = prev, r = xhigh, mid;
double lv = calcPoly(c, l, n), mv;
while(fabs(l-r) > eps) {
mid = (l+r)/2;
mv = calcPoly(c, mid, n);
if((mv-lv)*pi > inc)
r = mid;
else
l = mid;
}
if(i) putchar(' ');
printf("%.2lf", mid-xlow);
prev = mid;
}
puts("");
}
}
return 0;
}
 

台長: Morris
人氣(1,594) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][離散化&dp] 1092 - Tracking Bio-bots
此分類上一篇:[UVA][二分搜] 10372 - Leaps Tall Buildings

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文