小G的日常之接雨水 ZZULIOJ - 2400 思維
阿新 • • 發佈:2018-11-19
題解
題目連結
當前位置能積水只有兩邊都有比他高度高的柱子才行 接受的同時計算從左向右到達當前位置的最高高度
倒著掃一遍 過程中計算從右向左到達當前位置的最高高度答案為 min(左最高, 右最高) - 當前高 注意不能為負
AC程式碼
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6 + 10;
int a[MAXN], l[MAXN] ; //從左向右到達此位置的最高高度
int main()
{
#ifdef LOCAL
freopen("C:/input.txt", "r", stdin);
#endif
int N;
cin >> N;
for (int i = 1; i <= N; i++)
scanf("%d", &a[i]), l[i] = max(l[i - 1], a[i]);
ll ans = 0;
int mx = 0;
for (int i = N; i >= 1; i--)
{
mx = max(mx, a[i]);
ans += max(0, min (mx, l[i]) - a[i]); //左邊的最高高度和右邊的最高高度取最小減去當前高度
}
cout << ans << endl;
return 0;
}