Submission #512388

# Submission time Handle Problem Language Result Execution time Memory
512388 2022-01-16T10:19:03 Z Kaguya Po (COCI21_po) C++17
60 / 70
1000 ms 12580 KB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3","unroll-loops", "Ofast")
#pragma GCC target("bmi2")


set<int> pos[100051];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
//  freopen("input.txt", "r", stdin);
  int n;
  cin >> n;
  vector<int> a(n);
  bool have = false;
  for (auto& t : a) {
    cin >> t;
    if (t == 0)
      have = true;
  }
  vector<int> ve;
  for (int i = 0; i < n; i++)
    ve.push_back(a[i]);
  sort(ve.begin(), ve.end());
  ve.resize(unique(ve.begin(), ve.end()) - ve.begin());
  for (int i = 0; i < n; i++) {
    a[i] = lower_bound(ve.begin(), ve.end(), a[i]) - ve.begin();
  }
  int ans = 0;
  vector<int> se;
  vector<bool> vis(n + 25, false);
  vector<bool> was(n + 25, false);
  for (int i = 0; i < n; i++)
    if (!was[a[i]]) {
      se.push_back(a[i]);
      was[a[i]] = true;
    }
  sort(se.rbegin(), se.rend());
  for (int i = 0; i < n; i++)
    pos[a[i]].insert(i);
  if (!have) {
    ans += 1;
    se.pop_back();
  }
  if (have && se.back() == 0)
    se.pop_back();
  while (!se.empty()) {
    int x = se.back();
    se.pop_back();
    for (auto t : pos[x]) {
      if (vis[t])
        continue;
      vis[t] = true;
      int i = t;
      ans += 1;
      priority_queue<pair<int, int>, vector<pair<int, int>> > vs;
      while (i < n && a[i] >= x) {
        if (a[i] > x)
          vs.push({a[i], i});
        if (!vs.empty() && ((vs.top())).first < x) {
          while (!vs.empty() && ((vs.top())).first < x) {
            pos[(vs.top()).first].erase(vs.top().second);
            ans += 1;
            vs.pop();
          }
        }
        if (a[i] == x)
          vis[i] = true;
        i++;
      }
      if (a[i] == x)
        vis[i] = true;
    }
  }
  cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 29 ms 7656 KB Output is correct
5 Correct 32 ms 9088 KB Output is correct
6 Execution timed out 1088 ms 12580 KB Time limit exceeded
7 Correct 38 ms 10892 KB Output is correct