UVa 1115 - Water Shortage

contents

  1. 1. Problem
  2. 2. Sample Input
  3. 3. Sample Output
  4. 4. Solution

Problem

根據連通管原理,給定要灌入水的容積,請問最後水位的位置為何?

給定每個水槽的長寬高、以及離地面的高度。

Sample Input

1
2
3
4
5
6
7
8
1
4
11.0 7.0 5.0 1.0
15.0 6.0 4.0 1.0
5.0 8.0 5.0 1.0
19.0 4.0 8.0 1.0
78.0

Sample Output

1
17.00

Solution

講到連通管原理,很容易想起帕斯卡原理 … 喵帕斯。

二分水位高度,計算容積與目標容積的差異。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <algorithm>
using namespace std;
const int MAXN = 1024;
double LV[MAXN], H[MAXN], W[MAXN], D[MAXN], V;
void solve(int n, double V) {
double l = 0, r = 0, mid;
double sumV = 0;
for (int i = 0; i < n; i++) {
sumV += H[i] * W[i] * D[i];
r = max(r, LV[i] + H[i]);
}
if (sumV < V) {
puts("OVERFLOW");
return ;
}
for (int it = 0; it < 100; it++) {
mid = (l + r)/2;
sumV = 0;
for (int i = 0; i < n; i++) {
if (mid < LV[i]) continue;
sumV += W[i] * D[i] * min(H[i], mid - LV[i]);
}
if (sumV > V)
r = mid;
else
l = mid;
}
printf("%.2lf\n", l);
}
int main() {
int testcase, n;
scanf("%d", &testcase);
while (testcase--) {
scanf("%d", &n);
assert(n < MAXN);
for (int i = 0; i < n; i++)
scanf("%lf %lf %lf %lf", &LV[i], &H[i], &W[i], &D[i]);
scanf("%lf", &V);
solve(n, V);
if (testcase) puts("");
}
return 0;
}