1 条题解

  • 1
    @ 2025-3-18 7:53:52
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll, LL;
    #define pb push_back;
    
    void solve() {
        int n;
        cin >> n;
        ll a[n], ans[n] = {0}, result = 0;
        int max = 0;
        for(int i = 0; i < n; i++) {
            cin >> a[i];
            if(a[i] > max) max = a[i];
            else {
                ans[i] = max - a[i];
            }
        }
        max = 0;
        for(int i = n - 1; i >= 0; i--) {
            if(a[i] > max) max = a[i];
            else {
                result += min(max - a[i], ans[i]);
            }
        }
        cout << result;
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
    
        int t = 1;
        // cin >> t;
        while(t--) {
            solve();
        }
        return 0;
    }
    // 从左开始往右,记录并不断更新最大数,当前位置低于最大位置则存放该位置雨水数量为 最大-当前
    // 若不低于则记为0
    // 从右往左再遍历,不断更新最大数,若不低于则记录0,若低于则记为 最大-当前
    // 记录的雨水量取每个位置记录过的最小值
    
    • 1

    信息

    ID
    1651
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    提交数
    20
    已通过
    11
    上传者