1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#define N 1000000

int t[2*N];
int s[N];
int main() {
  int n;
  int mx_el = 0;
  int pos = 0;
  scanf("%d", &n);
  for(int i = 0; i < n; i++) {
    scanf("%d", &t[i]);
    t[i+n] = t[i];
    if(t[i] > mx_el) {
      mx_el = t[i];
      pos = i+n;
    }
  }
  s[0] = t[pos];
  int s_p = 0;
  int tmx = 1;
  int mx = 1;
  for(int i = pos - 1; i > pos - n; i--) {
    if(t[i] < s[s_p]) {
      tmx++;
      s_p++;
      s[s_p] = t[i];
    }
    else {
      if(tmx > mx) {
        mx = tmx;
      }
      int l = 0;
      int r = s_p;
      while(l < r) {
        int m = (l+r)/2;
        if(s[m] > t[i]) {
          l = m + 1;
        }
        else {
          r = m;
        }
      }
      s_p = l;
      s[s_p] = t[i];
      tmx = s_p + 1;
//      for(int j = 0; ; j++) {
//        if(t[i] >= s[j]) {
//          s_p = j;
//          s[j] = t[i];
//          tmx = s_p + 1;
//          break;
//        }
//      }
    }
  }
  if(tmx > mx) {
    mx = tmx;
  }
  printf("%d\n", mx);
  return 0;
}