#include <bits/stdc++.h> using namespace std; const int MX=1000100; int n,i,mid,lft,rgh,cur,res,w[MX],a[MX]; long long cnt; int main() { scanf("%d",&n); for (i=1; i<=n; i++) { scanf("%d",&a[i]); w[a[i]]=i; } res=(n+1)/2+(n+2)/2+n; cnt=1; mid=n; lft=rgh=w[n]; for (i=n-1; i>0; i--) { if ((i&1)!=(n&1)) { --mid; lft=min(lft,w[mid]); rgh=max(rgh,w[mid]); } cur=n-i-rgh+lft; if (cur>=0) cnt+=min(cur+1,min(lft,i)-max(rgh-n+i,1)+1); } printf("%d %lld\n",res,cnt); 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 | #include <bits/stdc++.h> using namespace std; const int MX=1000100; int n,i,mid,lft,rgh,cur,res,w[MX],a[MX]; long long cnt; int main() { scanf("%d",&n); for (i=1; i<=n; i++) { scanf("%d",&a[i]); w[a[i]]=i; } res=(n+1)/2+(n+2)/2+n; cnt=1; mid=n; lft=rgh=w[n]; for (i=n-1; i>0; i--) { if ((i&1)!=(n&1)) { --mid; lft=min(lft,w[mid]); rgh=max(rgh,w[mid]); } cur=n-i-rgh+lft; if (cur>=0) cnt+=min(cur+1,min(lft,i)-max(rgh-n+i,1)+1); } printf("%d %lld\n",res,cnt); return 0; } |