#include <bits/stdc++.h> #define ll long long using namespace std; int main(){ ios_base::sync_with_stdio(0); ll n; cin>>n; vector<ll> tab(n); for(ll i = 0; i < n; i++){ ll a; cin>>a; a--; tab[a] = i; } ll wyn = 1; ll pocz = tab.back(); ll kon = tab.back(); for(ll i = n-2; i >= 0; i--){ pocz = min(pocz, tab[i]); kon = max(kon, tab[i]); ll dlg = kon-pocz+1; ll zapas = 2*(n-i)-1-dlg; ll d1 = pocz; ll d2 = n-1-kon; if(d1<d2) swap(d1, d2); //cout<<zapas<<'\n'; if(zapas>=0){ if(zapas<=d2){ wyn+=zapas+1; }else{ zapas-=d2; wyn+=max(0ll, min(d2+1, d1+1-zapas)); } zapas = 2*(n-i)-2-dlg; if(zapas>=0){ if(zapas<=d2){ wyn+=zapas+1; }else{ zapas-=d2; wyn+=max(0ll, min(d2+1, d1+1-zapas)); } } } } cout<<2*n+1<<" "<<wyn<<'\n'; return 0; }
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 | #include <bits/stdc++.h> #define ll long long using namespace std; int main(){ ios_base::sync_with_stdio(0); ll n; cin>>n; vector<ll> tab(n); for(ll i = 0; i < n; i++){ ll a; cin>>a; a--; tab[a] = i; } ll wyn = 1; ll pocz = tab.back(); ll kon = tab.back(); for(ll i = n-2; i >= 0; i--){ pocz = min(pocz, tab[i]); kon = max(kon, tab[i]); ll dlg = kon-pocz+1; ll zapas = 2*(n-i)-1-dlg; ll d1 = pocz; ll d2 = n-1-kon; if(d1<d2) swap(d1, d2); //cout<<zapas<<'\n'; if(zapas>=0){ if(zapas<=d2){ wyn+=zapas+1; }else{ zapas-=d2; wyn+=max(0ll, min(d2+1, d1+1-zapas)); } zapas = 2*(n-i)-2-dlg; if(zapas>=0){ if(zapas<=d2){ wyn+=zapas+1; }else{ zapas-=d2; wyn+=max(0ll, min(d2+1, d1+1-zapas)); } } } } cout<<2*n+1<<" "<<wyn<<'\n'; return 0; } |