#include <bits/stdc++.h>
using namespace std;
template <class t> ostream& operator<<(ostream &l, const vector<t> &r) {
l << "{";
for (int i = 0; i + 1 < r.size(); i++) l << r[i] << ", ";
if (!r.empty()) l << r[r.size() - 1];
l << "}";
return l;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
assert(n <= 5000);
set<int> states;
vector<pair<int, bool>> links(n);
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] > 0) {
states.insert(i);
links[i] = { -1, true };
} else {
int ptr = -1;
for (int j = i - 1; j >= 0; j--) {
if (a[j] > a[i] && links[j].second) {
ptr = j;
break;
}
}
links[i] = { ptr, true };
assert(ptr != -1);
while (links[ptr].first != -1) {
links[ptr].second ^= true;
ptr = links[ptr].first;
}
links[ptr].second ^= true;
// cout << i << " " << ptr << endl;
bool removed = states.erase(ptr);
if (!removed) states.insert(ptr);
}
if (states.empty()) cout << 0;
else {
cout << a[*states.rbegin()];
}
cout << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |