Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int main() {
- int n; cin >> n; // size of array
- vector<int> a(n);
- for (int i = 0; i < n; i++) cin >> a[i]; // read array
- vector<int> candidates;
- int ans = 0;
- for (int j = 0; j < n; j++) {
- // if the index is a potential answer, add it
- if (candidates.size()==0 || a[j]<a[candidates.back()]) candidates.push_back(j);
- // start the binary search, notice that candidates[candidates.size()-1] always works
- int l=0, r=candidates.size()-2, i=candidates[candidates.size()-1];
- while (l <= r) {
- int m = (l+r)/2;
- if (a[candidates[m]] <= a[j]) {
- i = candidates[m];
- r = m-1;
- } else l = m+1;
- }
- // take the maximum result of the binary search
- ans = max(ans, j-i);
- }
- cout << ans << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement