提交时间:2024-04-06 16:35:29
运行 ID: 142101
# include <bits/stdc++.h> using namespace std ; vector <int> height ; int trap ( vector <int> height ) { int left = 0 , right = height . size ( ) - 1 ; int res = 0 ; int left_max = 0 , right_max = 0; while ( left < right ) { if ( height [left] < height [right] ) { height [left] >= left_max ? ( left_max = height[left] ) : res += ( left_max - height[left] ) ; ++ left ; } else { height [right] >= right_max ? ( right_max = height [right] ) : res += ( right_max - height [right] ) ; -- right ; } } return res ; } int main ( ) { int a ; cin >> a ; for ( int i = 1 ; i <= a ; i ++ ) { int x ; cin >> x ; height . push_back ( x ) ; } cout << trap ( height) << endl ; return 0 ; }