Skip to content

Instantly share code, notes, and snippets.

Created August 22, 2017 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d40d400100298a8954b692fede92869e to your computer and use it in GitHub Desktop.
Save anonymous/d40d400100298a8954b692fede92869e to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <list>
#include <map>
//#include <unordered_map>
#include <algorithm>
#include <string>
#include <memory>
#include <cstring>
#include <stack>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
const int MAXN = 50000;
int s[MAXN + 1];
int r[MAXN + 1]; //first index of smaller element on the right
int m[MAXN + 1]; // index of largest element in [current, r[current])
int main() {
while (true) {
int n;
if(scanf("%d", &n) != 1) break;;
for (int i = 0; i < n; ++i) {
scanf("%d", s + i);
}
s[n] = -1;
r[n] = n;
m[n] = n;
int ret = -1;
for (int i = n - 1; i >= 0; --i) {
int j = i + 1;
m[i] = i;
while(s[j] > s[i]) {
m[i] = s[m[i]] > s[m[j]] ? m[i] : m[j];
j = r[j];
}
r[i] = j;
ret = max(ret, m[i] - i);
}
cout << (ret ? ret : -1) << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment