1 条题解

  • 0
    @ 2026-11-2 13:10:53

    最大的矩形纸片(Largest Rectangular Paper) 题解

    思路分析

    按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。

    参考代码

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        if (!(cin >> n)) return 0;
        vector<long long> h(n + 1, 0);
        for (int i = 0; i < n; ++i) cin >> h[i];
        vector<int> stk;
        long long ans = 0;
        for (int i = 0; i <= n; ++i) {
            while (!stk.empty() && h[stk.back()] >= h[i]) {
                long long height = h[stk.back()];
                stk.pop_back();
                long long width = stk.empty() ? i : (i - stk.back() - 1);
                ans = max(ans, height * width);
            }
            stk.push_back(i);
        }
        cout << ans << "\n";
        return 0;
    }
    
    • 1

    最大的矩形纸片(Largest Rectangular Paper)

    信息

    ID
    8626
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者