zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    C#异常断电后重新启动项目出现配置未初始化错误
    TFS: 解决The build agent error
    删除TFS中的项目
    将现有项目添加到TFS中
    Typora开启行内公式
    Markdown上下标内容多于一项
    小甲鱼python基础教程飞机大战源码及素材
    Git 将本地库添加到远程仓库
    C# float与UInt16互转
    C++的重载流输出运算符
  • 原文地址:https://www.cnblogs.com/ustc-anmin/p/11594208.html
Copyright © 2011-2022 走看看