1 条题解

  • 0
    @ 2026-11-2 13:31:55

    合并塔楼(Towers) 题解

    思路分析

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

    参考代码

    #include <iostream>
    #include <vector>
    using namespace std;
    
    const long long INF = (1LL << 60);
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        if (!(cin >> n)) return 0;
        vector<long long> pref(n + 1, 0);
        for (int i = 1; i <= n; ++i) {
            long long h;
            cin >> h;
            pref[i] = pref[i - 1] + h;
        }
        vector<int> cnt(n + 1, 0);
        vector<long long> lastH(n + 1, INF);
        cnt[0] = 0;
        lastH[0] = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = i - 1; j >= 0; --j) {
                if (pref[i] - pref[j] >= lastH[j]) {
                    cnt[i] = cnt[j] + 1;
                    lastH[i] = pref[i] - pref[j];
                    break;
                }
            }
        }
        cout << (n - cnt[n]) << "\n";
        return 0;
    }
    
    • 1

    信息

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