c++最大的矩形纸片

最大的矩形纸片

描述

一张半边参差不齐的网格纸(网格边长均为1),有一边是完整没有破损的。现要从中剪出一片面积最大的矩形纸片。

给定网格纸中完整边的长度N(1≤N≤1000000),以及网格中每一列残存部分的高度(1≤高度≤10000),输出能够剪出的最大矩形纸片面积。

输入

第一行输入一个正整数N(1≤N≤1000000),表示纸片完整边的长度

第二行输入N个正整数(1≤正整数≤10000),表示每列格子残存部分的高度,两个正整数之间用一个空格隔开

输出

输出一个正整数,表示能够剪出的最大矩形纸片面积

输入样例 1 

6
3 2 1 4 5 2

输出样例 1

8
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int largestRectangleArea(vector<int>& heights) {
    int n = heights.size();
    vector<int> left(n), right(n, n);
    stack<int> mono_stack;
    for (int i = 0; i < n; ++i) {
        while (!mono_stack.empty() && heights[mono_stack.top()] >= heights[i]) {
            right[mono_stack.top()] = i;
            mono_stack.pop();
        }
        left[i] = (mono_stack.empty() ? -1 : mono_stack.top());
        mono_stack.push(i);
    }

    int maxArea = 0;
    for (int i = 0; i < n; ++i) {
        maxArea = max(maxArea, heights[i] * (right[i] - left[i] - 1));
    }
    return maxArea;
}

int main() {
    int N;
    cin >> N;
    vector<int> heights(N);
    
    for (int i = 0; i < N; i++) {
        cin >> heights[i];
    }

    cout << largestRectangleArea(heights) << endl;
    return 0;
}

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 C++ 实现的基于矩形面积的内接最大矩形算法: ```c++ #include<bits/stdc++.h> using namespace std; // 定义一个矩形结构体 struct Rectangle { int width; int height; }; // 计算矩形面积 int area(Rectangle r) { return r.width * r.height; } // 计算内接最大矩形 Rectangle maxInner(Rectangle r) { // 如果矩形的宽或高为1,则返回该矩形 if (r.width == 1 || r.height == 1) { return r; } // 如果矩形的宽和高相等,则返回该矩形 if (r.width == r.height) { return r; } // 如果矩形的宽大于高,则将其分成两个矩形 if (r.width > r.height) { int new_width = r.width / 2; Rectangle left_rect = {new_width, r.height}; Rectangle right_rect = {r.width - new_width, r.height}; // 分别计算左右两个矩形的内接最大矩形 Rectangle left_inner = maxInner(left_rect); Rectangle right_inner = maxInner(right_rect); // 返回面积较大的内接最大矩形 return area(left_inner) > area(right_inner) ? left_inner : right_inner; } // 如果矩形的高大于宽,则将其分成两个矩形 else { int new_height = r.height / 2; Rectangle top_rect = {r.width, new_height}; Rectangle bottom_rect = {r.width, r.height - new_height}; // 分别计算上下两个矩形的内接最大矩形 Rectangle top_inner = maxInner(top_rect); Rectangle bottom_inner = maxInner(bottom_rect); // 返回面积较大的内接最大矩形 return area(top_inner) > area(bottom_inner) ? top_inner : bottom_inner; } } int main() { // 定义一个矩形 Rectangle r = {10, 6}; // 计算内接最大矩形 Rectangle inner_rect = maxInner(r); // 输出内接最大矩形的宽和高 cout << "Width: " << inner_rect.width << endl; cout << "Height: " << inner_rect.height << endl; return 0; } ``` 以上代码中,我们首先定义了一个矩形结构体,包含矩形的宽和高。然后,我们定义了一个计算矩形面积的函数 `area`。接着,我们实现了计算内接最大矩形的函数 `maxInner`,该函数基于分治思想,将矩形递归地分成两个子矩形,分别计算子矩形的内接最大矩形,并返回面积较大的那个矩形。最后,在 `main` 函数中,我们定义了一个矩形,计算其内接最大矩形,并输出其宽和高。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值