題目描述
在 x 軸上有相互挨着的矩形, 這些矩形有一個邊緊貼着 x 軸,現在給出每個矩形的長寬, 所有的矩形看作整體當作一個畫布, 則可以在這個畫布上畫出的最大的矩形的面積是多少。(畫出的矩形長和高平行於X,Y軸)
解答要求時間限制:3000ms, 內存限制:64MB
輸入
每組第一個數N(0<=N<=20000)表示N個矩形。下面N行有兩個數a(1 <= a <=1000),b(1 <= b<=1000)分別表示每個矩形的x軸長度和y軸長度。
輸出
輸出最大的面積。
#include <stdio.h> long dynamicCaculate(int size); long x_and_y[20000][2] = {0}; int main() { int n; scanf("%d", &n); long i = 0; while (i < n) { scanf("%d %d", &x_and_y[i][0], &x_and_y[i][1]); i++; } long res = dynamicCaculate(n); printf("%ld", res); return 0; } //分包不包括下一個輸入的矩形 long dynamicCaculate(int size) { if (size == 0) { return 0; } long res_1 = 0; for (int i = 0; i < size; ++i) { long tempArea = 0; int totalWidth = x_and_y[i][0]; for (int j = i - 1; j >= 0; --j) { if (x_and_y[j][1] >= x_and_y[i][1]) { totalWidth += x_and_y[j][0]; } else { break; } } for (int j = i + 1; j < size; ++j) { if (x_and_y[j][1] >= x_and_y[i][1]) { totalWidth += x_and_y[j][0]; } else { break; } } tempArea = totalWidth * x_and_y[i][1]; res_1 = res_1 > tempArea ? res_1 : tempArea; } return res_1; }