#include<bits/stdc++.h> #define fi first #define se second using namespace std; const int N=1e6; int tab[N+10]; int g[N+10]; bool vis[N+10]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; long long ans=0; cin>>n; for(int i=1;i<=n;i++) { cin>>tab[i]; g[tab[i]]=i; } ans=1; int l,r; l=r=g[n]; vis[n]=true; int j=n-1; while(j>0) { while(g[j]<l) { l--; vis[tab[l]]=true; } while(g[j]>r) { r++; vis[tab[r]]=true; } while(vis[j]) { j--; for(int d:{2*(n-j)-2,2*(n-j)-1}) { if(d<=n && d>=r-l+1) { ans+=min({l,n-r+1,d-(r-l),n-d+1}); //cerr<<d<<" "<<l<<" "<<r<<" "<<min({l,n-r+1,d-(r-l),n-d+1})<<"\n"; } } } } cout<<2*n+1<<" "<<ans<<"\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 49 50 51 52 53 54 | #include<bits/stdc++.h> #define fi first #define se second using namespace std; const int N=1e6; int tab[N+10]; int g[N+10]; bool vis[N+10]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; long long ans=0; cin>>n; for(int i=1;i<=n;i++) { cin>>tab[i]; g[tab[i]]=i; } ans=1; int l,r; l=r=g[n]; vis[n]=true; int j=n-1; while(j>0) { while(g[j]<l) { l--; vis[tab[l]]=true; } while(g[j]>r) { r++; vis[tab[r]]=true; } while(vis[j]) { j--; for(int d:{2*(n-j)-2,2*(n-j)-1}) { if(d<=n && d>=r-l+1) { ans+=min({l,n-r+1,d-(r-l),n-d+1}); //cerr<<d<<" "<<l<<" "<<r<<" "<<min({l,n-r+1,d-(r-l),n-d+1})<<"\n"; } } } } cout<<2*n+1<<" "<<ans<<"\n"; return 0; } |