#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(0); long long n; cin>>n; vector<long long> v(n); for(long long i=0; i<n; ++i) cin>>v[i]; vector<long long> place(n+1); for(long long i=0; i<n; ++i) place[v[i]]=i; long long res=1; if(n>1) ++res; long long l=place[n]; long long r=place[n]; for(long long d=2; d<n; ++d) { if(d%2==0) { long long t=place[n-d/2]; if(t<l) l=t; if(t>r) r=t; } long long over=d-(r-l+1); if(over>=0) { if(l>=over&&n-r-1>=over) res+=over+1; else { if(l>=over||n-r-1>=over) res+=min(l,n-r-1)+1; else res+=min(l,n-r-1)-(over-max(l,n-r-1))+1; } } } cout<<2*n+1<<" "<<res<<endl; 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 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(0); long long n; cin>>n; vector<long long> v(n); for(long long i=0; i<n; ++i) cin>>v[i]; vector<long long> place(n+1); for(long long i=0; i<n; ++i) place[v[i]]=i; long long res=1; if(n>1) ++res; long long l=place[n]; long long r=place[n]; for(long long d=2; d<n; ++d) { if(d%2==0) { long long t=place[n-d/2]; if(t<l) l=t; if(t>r) r=t; } long long over=d-(r-l+1); if(over>=0) { if(l>=over&&n-r-1>=over) res+=over+1; else { if(l>=over||n-r-1>=over) res+=min(l,n-r-1)+1; else res+=min(l,n-r-1)-(over-max(l,n-r-1))+1; } } } cout<<2*n+1<<" "<<res<<endl; return 0; } |