#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cmath>
#include <queue>
using namespace std;
//typy
using ll = long long;
using pll = pair <long long, long long>;
using pii = pair <int, int>;
using ull = unsigned long long;
//vectory
#define all(_) (_).begin(), (_).end()
#define vec vector
using vl = vector <ll>;
using vi = vector <int>;
using vs = vector <string>;
using vc = vector <char>;
using vpll = vector <pair <long long, long long> >;
using vpii = vector <pair <int, int> >;
using vvint = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vb = vector <bool>;
using ld = long double;
//mapy, sety
using mll = map <long long, long long>;
using mii = map <int, int>;
using mss = map <string, string>;
using mcc = map <char, char>;
using sl = set <long long>;
using si = set <int>;
using ss = set <string>;
using sc = set <char>;
//unordered mapy, sety
using umll = unordered_map <long long, long long>;
using umii = unordered_map <int, int>;
using umss = unordered_map <string, string>;
using umcc = unordered_map <char, char>;
using usl = unordered_set <long long>;
using usi = unordered_set <int>;
using uss = unordered_set <string>;
using usc = unordered_set <char>;
//tak, nie
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define TAK cout<<"TAK\n"
#define NIE cout<<"NIE\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define Tak cout<<"Tak\n"
#define Nie cout<<"Nie\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define tak cout<<"tak\n"
#define nie cout<<"nie\n"
//inne
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define LINIJKI ios_base::sync_with_stdio(0);cin.tie(0);
#define sorall(x) sort(x.begin(), x.end());
#define sorcmp(x, cmp) sort(x.begin(), x.end(), cmp);
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define per(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define nl '\n'
#define elif else if
#define vecout(v) {for(auto _:(v)) cout << _ << ' '; cout << "\n";}
#define wczyt(v) for(auto &_:(v)) cin >> (_);
//-----------------------------------------------------
const bool db = false, db1 = true, db2 = true, db3 = true, db4 = true;
const ll jps = 21;
vector <vector <ll> > jp;
ll n, wyn = 0;
vector <ll> l;
void ustaw_jp(){
for(ll i = 1; i < jps; i++){
for(ll j = 0; j < 2*n; j++){
if(jp[i - 1][j] == -1){
jp[i][j] = -1;
}
else jp[i][j] = jp[i - 1][jp[i - 1][j]];
}
}
}
ll ile_skokow_do_n(ll w){
ll zwr = 0, x = n;
for(ll i = jps; i >= 0; i--){
if((1LL << i) < x && jp[i][w] != -1){
if(db && db4) cout << "jestesmy "<< w << ", " << l[w] << ", skaczemy " << i << ", zostalismy ";
zwr += (1LL << i);
x -= (1LL << i);
w = jp[i][w];
if(db && db4)cout << w << ", " << l[w] << endl;
}
}
return zwr;
}
int main(){
LINIJKI
cin >> n;
jp.resize(jps, vector <ll>(2*n));
l.resize(2*n);
deque <ll> q;
for(ll i = 0; i < 2*n; i++){
if(i < n) cin >> l[i];
else l[i] = l[i-n];
}
if(db && db1) vecout(l);
for(ll i = 2*n-1; i >= 0; i--){
while(!q.empty() && l[q.back()] <= l[i]){
q.pop_back();
}
if(q.empty()) jp[0][i] = -1;
else jp[0][i] = q.back();
q.push_back(i);
}
ustaw_jp();
if(db && db2) vecout(jp[0]);
if(!db || !db4){
for(ll i = 0; i < n; i++){
if(db && db3) cout << i << ' ' << wyn << endl;
wyn = max(wyn, ile_skokow_do_n(i));
}
cout << wyn + 1;
}
else cout << ile_skokow_do_n(4) << endl;
}
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | #include <iostream> #include <vector> #include <algorithm> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <cmath> #include <queue> using namespace std; //typy using ll = long long; using pll = pair <long long, long long>; using pii = pair <int, int>; using ull = unsigned long long; //vectory #define all(_) (_).begin(), (_).end() #define vec vector using vl = vector <ll>; using vi = vector <int>; using vs = vector <string>; using vc = vector <char>; using vpll = vector <pair <long long, long long> >; using vpii = vector <pair <int, int> >; using vvint = vector<vector<int>>; using vvll = vector<vector<long long>>; using vb = vector <bool>; using ld = long double; //mapy, sety using mll = map <long long, long long>; using mii = map <int, int>; using mss = map <string, string>; using mcc = map <char, char>; using sl = set <long long>; using si = set <int>; using ss = set <string>; using sc = set <char>; //unordered mapy, sety using umll = unordered_map <long long, long long>; using umii = unordered_map <int, int>; using umss = unordered_map <string, string>; using umcc = unordered_map <char, char>; using usl = unordered_set <long long>; using usi = unordered_set <int>; using uss = unordered_set <string>; using usc = unordered_set <char>; //tak, nie #define YES cout<<"YES\n" #define NO cout<<"NO\n" #define TAK cout<<"TAK\n" #define NIE cout<<"NIE\n" #define Yes cout<<"Yes\n" #define No cout<<"No\n" #define Tak cout<<"Tak\n" #define Nie cout<<"Nie\n" #define yes cout<<"yes\n" #define no cout<<"no\n" #define tak cout<<"tak\n" #define nie cout<<"nie\n" //inne #define pb push_back #define eb emplace_back #define fi first #define se second #define LINIJKI ios_base::sync_with_stdio(0);cin.tie(0); #define sorall(x) sort(x.begin(), x.end()); #define sorcmp(x, cmp) sort(x.begin(), x.end(), cmp); #define rep(i,a,b) for (int i = (a); i < (b); i++) #define per(i,a,b) for (int i = (b)-1; i >= (a); i--) #define nl '\n' #define elif else if #define vecout(v) {for(auto _:(v)) cout << _ << ' '; cout << "\n";} #define wczyt(v) for(auto &_:(v)) cin >> (_); //----------------------------------------------------- const bool db = false, db1 = true, db2 = true, db3 = true, db4 = true; const ll jps = 21; vector <vector <ll> > jp; ll n, wyn = 0; vector <ll> l; void ustaw_jp(){ for(ll i = 1; i < jps; i++){ for(ll j = 0; j < 2*n; j++){ if(jp[i - 1][j] == -1){ jp[i][j] = -1; } else jp[i][j] = jp[i - 1][jp[i - 1][j]]; } } } ll ile_skokow_do_n(ll w){ ll zwr = 0, x = n; for(ll i = jps; i >= 0; i--){ if((1LL << i) < x && jp[i][w] != -1){ if(db && db4) cout << "jestesmy "<< w << ", " << l[w] << ", skaczemy " << i << ", zostalismy "; zwr += (1LL << i); x -= (1LL << i); w = jp[i][w]; if(db && db4)cout << w << ", " << l[w] << endl; } } return zwr; } int main(){ LINIJKI cin >> n; jp.resize(jps, vector <ll>(2*n)); l.resize(2*n); deque <ll> q; for(ll i = 0; i < 2*n; i++){ if(i < n) cin >> l[i]; else l[i] = l[i-n]; } if(db && db1) vecout(l); for(ll i = 2*n-1; i >= 0; i--){ while(!q.empty() && l[q.back()] <= l[i]){ q.pop_back(); } if(q.empty()) jp[0][i] = -1; else jp[0][i] = q.back(); q.push_back(i); } ustaw_jp(); if(db && db2) vecout(jp[0]); if(!db || !db4){ for(ll i = 0; i < n; i++){ if(db && db3) cout << i << ' ' << wyn << endl; wyn = max(wyn, ile_skokow_do_n(i)); } cout << wyn + 1; } else cout << ile_skokow_do_n(4) << endl; } |
English