#include using namespace std; #if 0 class CIntervalMax { public: CIntervalMax ( vector & data ) : m_Array ( data ), m_BlockSize ( std::sqrt ( data . size () ) + 1 ), m_Maxs ( ) { for ( size_t i = 0; i < m_Array . size (); i += m_BlockSize ) { size_t en = std::min ( i + m_BlockSize, m_Array . size () ); int localMax = 0; for ( size_t st = i; st < en; st ++ ) localMax = max ( localMax, m_Array[st] ); m_Maxs . push_back ( localMax ); } #ifdef CHS cout << "size = " << m_Maxs . size () << endl; for ( int & xMax : m_Maxs ) cout << xMax << " "; cout << endl; #endif } int FindMax ( size_t st, size_t en ) { size_t stBlock = st / m_BlockSize; size_t enBlock = en / m_BlockSize; } private: vector & m_Array; size_t m_BlockSize; vector m_Maxs; }; #else class CIntervalMax { public: CIntervalMax ( vector & data ) : m_Array ( data ) { } int FindMax ( size_t st, size_t en ) { return *max_element ( m_Array . begin () + st, m_Array . begin () + en ); } private: vector & m_Array; }; #endif int main ( int argc, char * argv [] ) { ios::sync_with_stdio ( 0 ); int n; map > heightsMap; vector heights; cin >> n; for ( int i = 0; i < n; i ++ ) { int x; cin >> x; heightsMap[x] . push_back ( i ); heights . push_back ( x ); } #ifdef CHS for ( const auto & entry : heightsMap ) { cout << entry . first << " => " << "["; bool first = true; for ( const auto & x : entry . second ) if ( first ) { cout << x; first = false; } else cout << ", " << x; cout << "]" << endl; } #endif CIntervalMax m ( heights ); int totalLength = 0; // all bridges for ( const auto & mapEntry : heightsMap ) { int height = mapEntry . first; const vector & elems = mapEntry . second; for ( size_t i = 1; i < elems . size (); i ++ ) if ( height >= m . FindMax ( elems[i - 1], elems[i] ) ) totalLength += elems[i] - elems[i-1] - 1; } cout << totalLength << endl; return ( 0 ); }