#include #include using namespace std; struct terrace { size_t position, height; terrace (size_t position, size_t height) : position (position), height (height) {} }; int main() { ios_base::sync_with_stdio(false); size_t size; cin >> size; size_t totalLen = 0; stack possibleBridgeStarts; for (size_t i = 0; i < size; i++) { size_t height; cin >> height; if (i > 0) { while (!possibleBridgeStarts.empty () && possibleBridgeStarts.top().height < height) { possibleBridgeStarts.pop(); } if (!possibleBridgeStarts.empty () && possibleBridgeStarts.top().height == height) { totalLen += i - 1 - possibleBridgeStarts.top().position; } } possibleBridgeStarts.push(terrace (i, height)); } cout << totalLen << endl; return 0; }