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
63
64
65
66
67
68
69
70
71
72
73
74
#ifdef _MSC_VER
  #ifndef __GNUC__
    #pragma warning(disable: 4996)
  #endif
  #define main main0
#endif
#include <iostream>
//#include <list>
#include <vector>
using namespace std;
typedef long long           ll;
typedef unsigned long long ull;
typedef unsigned int      uint;

struct Perla {
  int wartosc;
  int liczba;
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, iPerla = 0, maxPozycja = -1, maxWartosc = 0, wynik = 1;
  vector<Perla> vPerla;
  vector<int> vWartosc;

  cin >> n;
  vPerla.resize(n);
  vWartosc.resize(n);
  for(int i = 0; i < n; ++i) {
    cin >> vWartosc[i];
    if(maxWartosc < vWartosc[i]) {
      maxWartosc = vWartosc[i];
      maxPozycja = i;
    }
  }
  ++maxPozycja;

  for(int i = maxPozycja, j; i < maxPozycja + n; ++i) {
    int wartosc = vWartosc[i%n];
    if(wartosc < maxWartosc) {
      for(j = 0; j < iPerla && vPerla[j].wartosc > wartosc; ++j)
        continue;                               // pomin wieksze
      if(j == iPerla) {                         // dodaj nowy na koncu
        vPerla[j].wartosc = wartosc;
        vPerla[j].liczba = 1;
      } else if(vPerla[j].wartosc < wartosc) {  // dolicz perle
        vPerla[j].wartosc = wartosc;
        ++vPerla[j].liczba;
        if(j > 0 && vPerla[j-1].liczba == vPerla[j].liczba) {
          vPerla[j-1].wartosc = wartosc;
          --j;
        }
      } else { // vPerla[j].wartosc == wartosc - ustaw ostatni
        if(j + 1 < iPerla && vPerla[j].liczba == vPerla[j + 1].liczba) {
          ++vPerla[j].liczba;
          if(j > 0 && vPerla[j-1].liczba == vPerla[j].liczba) {
            vPerla[j-1].wartosc = wartosc;
            --j;
          }
        }
      }
      iPerla = j + 1;
    } else {
      wynik = wynik >= vPerla[0].liczba ? wynik : vPerla[0].liczba + 1;
      iPerla = 0;
    }
  }

  cout << wynik << endl;

  return 0;
}