Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
After any operation, the remaining array is always a prefix of the original array. So the whole process is just jumping left through prefixes; no real deletion simulation is needed.
If the current prefix is , the next chosen element is the rightmost maximum inside that prefix. Ask which original positions can ever be chosen.
A position can be chosen exactly when it is at least as large as every value before it. In other words, look for prefix records, but with ties included.
The tie rule is the trap: because the rightmost maximum is chosen, equal prefix maxima are chosen one by one from right to left. For , the answer is , not .
Scan left to right while maintaining the maximum seen so far. Every time , increment the answer and update .
Research Notes I checked the Codeforces statement/tags (https://codeforces.com/problemset/problem/2204/B), the official Educational Codeforces Round 188 editorial code (https://codeforces.com/blog/entry/152150), CF Step's explanation/code (https://cfstep.com/codeforces/contests/contest-2204/problem-b/), and an independent solution mirror (https://github.com/mhdnazrul/Codeforces-Solutions/blob/main/Solutions/B_Right_Maximum.cpp). They all point to the same intended greedy: count prefix records with ties included. The supplied statement is the source of truth; the explanation below is original.
Core Observation Call an index a weak prefix maximum if ; for , this is automatically true. The answer is exactly the number of weak prefix maxima.
Why weak? Because equal values matter. If the array is , the first operation chooses position , then position , then position . Counting only strict new maxima would output , which is just wrong. Codeforces would bonk that instantly.
Why This Matches The Process Consider the current remaining array as a prefix . Let be the index chosen by the operation: the rightmost position among all maximum values in this prefix.
First, is a weak prefix maximum. Every element before is , because is a maximum of the whole prefix. So it qualifies.
Second, no index with can be a weak prefix maximum. Since is the rightmost maximum, every later value satisfies . But is before , so when checking , there is already a previous value larger than it. It fails.
So, in any prefix, the operation chooses exactly the largest-index weak prefix maximum still present. Then it removes that index and everything to its right. In other words, each operation deletes one weak prefix maximum plus some irrelevant smaller junk to its right. Repeating this chooses all weak prefix maxima from right to left.
Therefore, the number of operations is the count of positions where .
Algorithm
Maintain mx, the maximum value seen so far. Scan left to right:
a[i] >= mx, this position is a weak prefix maximum, so increment ans and set mx = a[i].a[i] < mx, it will never be chosen; it gets removed together with some earlier weak prefix maximum.Because , initializing mx = 0 is safe.
Edge Cases
Complexity Each test case is scanned once. Time complexity is , and extra memory is . Across all test cases, the total work is linear in the total input size.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int ans = 0;
int mx = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x >= mx) {
ans++;
mx = x;
}
}
cout << ans << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int ans = 0;
int mx = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x >= mx) {
ans++;
mx = x;
}
}
cout << ans << '\n';
}
return 0;
}