Apr 20 2015 解題區►解題區 - UVa UVa 1115 - Water Shortage contents 1. Problem2. Sample Input3. Sample Output4. Solution Problem 根據連通管原理,給定要灌入水的容積,請問最後水位的位置為何? 給定每個水槽的長寬高、以及離地面的高度。 Sample Input 123456781411.0 7.0 5.0 1.015.0 6.0 4.0 1.0 5.0 8.0 5.0 1.019.0 4.0 8.0 1.078.0 Sample Output 117.00 Solution 講到連通管原理,很容易想起帕斯卡原理 … 喵帕斯。 二分水位高度,計算容積與目標容積的差異。 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950#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;} Newer 2015 Google Code Jam Round 1A Older UVa 1089 - Suffix-Replacement Grammars