tanks_1.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <memory.h>
  4. #include <math.h>
  5. #define Eps 1e-10
  6. #define KP 0.097
  7. #define MAXN 11
  8. double col_height[MAXN], pipe_height[MAXN], height[MAXN];
  9. int N, testID;
  10. double ans;
  11. int work(int ID, double P, double V) {
  12. double a, b, c, delta, x1, x2;
  13. //(height[0] - height[ID] - x) * KP = V * P / (V - x) - 1
  14. //((height[0] - height[ID]) * KP + 1 - x * KP)(V - x) = V * P
  15. a = KP;
  16. b = - ((height[0] - height[ID]) * KP + 1) - V * KP;
  17. c = ((height[0] - height[ID]) * KP + 1) * V - V * P;
  18. delta = b * b - 4 * a * c;
  19. x1 = (- b - sqrt(delta)) / (2 * a);
  20. x2 = (- b + sqrt(delta)) / (2 * a);
  21. if (x1 < -Eps) height[ID] += x2;
  22. else height[ID] += x1;
  23. return 0;
  24. }
  25. int main() {
  26. double h1, h2, V, P;
  27. freopen("tanks.in", "r", stdin);
  28. freopen("tanks.out", "w", stdout);
  29. while (scanf("%d", &N) && N) {
  30. V = ans = 0;
  31. for (int i = 0; i < N; i ++) {
  32. scanf("%lf", &col_height[i]);
  33. V += col_height[i];
  34. }
  35. for (int i = 0; i < N - 1; i ++) {
  36. scanf("%lf", &pipe_height[i]);
  37. }
  38. memset(height, 0, sizeof(height));
  39. height[0] = col_height[0];
  40. height[1] = pipe_height[0];
  41. V -= height[0];
  42. V -= height[1];
  43. P = 1;
  44. for (int i = 1; i < N; i ++) {
  45. h1 = pipe_height[i] - pipe_height[i - 1];
  46. h2 = pipe_height[i];
  47. if (i == N - 1 || P * V / (V - h1) - 1 >= (col_height[0] - pipe_height[i]) * KP) {
  48. work(i, P, V);
  49. break;
  50. } else if (P * V / (V - h1 - h2) - 1 >= (col_height[0] - pipe_height[i]) * KP) {
  51. height[i] += h1;
  52. height[i + 1] = V - h1 - P * V / ((col_height[0] - pipe_height[i]) * KP + 1);
  53. break;
  54. } else {
  55. height[i] += h1;
  56. height[i + 1] += h2;
  57. P = P * V / (V - h1 - h2);
  58. work(i, P, col_height[i] - pipe_height[i]);
  59. V -= h1 + h2 + col_height[i] - pipe_height[i];
  60. }
  61. }
  62. for (int i = 0; i < N; i ++) {
  63. ans += height[i];
  64. }
  65. printf("Case %d: %.3lf\n\n", ++ testID, ans);
  66. }
  67. return 0;
  68. }