Submission #1153954

#TimeUsernameProblemLanguageResultExecution timeMemory
1153954jmuzhenPo (COCI21_po)C++20
20 / 70
22 ms584 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

using ll = long long;

signed main() {
  int n; cin >> n;
  stack<int> st;
  int ans = 0;
  
  for (int i = 1; i <= n; i++) {
    int x; cin >> x;
    if (st.empty()) {
      st.push(x);
      ans++; // we increment answer when the segment starts
      continue;
    }
    while (!st.empty() && st.top() > x) {
      // decreasing; for example 3,2 -> need to end 3's layer because the 3 can't match another 3 to the right anymore
      // so we remove/end the segment of 3
      st.pop();
    }

    // if equal, then ignore this one since we're continuing the segment
    if (!st.empty() && st.top() == x) {
      continue;
    }

    st.push(x); // create a new segment for us
    ans++; // we increment answer when the segment starts
  }

  cout << ans << endl;

}
#Verdict Execution timeMemoryGrader output
Fetching results...