Rectangle

题目描述

在 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;
}

 

posted @ 2019-09-26 20:26  Practical  阅读(1653)  评论(0编辑  收藏  举报