#include<cstdio> #include<algorithm> #define S 1000007 using namespace std; typedef long long ll; int T[S]; int Y[S]; int mini[S]; int maxi[S]; int main(void){ int n; scanf("%d",&n); for(int i = 1; i<= n;i++){ scanf("%d",&T[i]); Y[T[i]] = i; } ll odp = 0; mini[n+1] = 1e7; for(int i = n; i >= 1; i--){ mini[i] = min(mini[i+1],Y[i]); maxi[i] = max(maxi[i+1],Y[i]); } for(int i = 1; i <= n;i++){ int p = (i+1)/2 - 1; int p2 = i-p; int dl = maxi[n-p2+1] - mini[n-p2+1] + 1; if(dl > i) continue; int l = max(1,maxi[n-p2+1] - i + 1); int r = min(mini[n-p2+1],n - i + 1); odp += (r-l+1); } printf("%d %lld",2*n+1,odp); 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 | #include<cstdio> #include<algorithm> #define S 1000007 using namespace std; typedef long long ll; int T[S]; int Y[S]; int mini[S]; int maxi[S]; int main(void){ int n; scanf("%d",&n); for(int i = 1; i<= n;i++){ scanf("%d",&T[i]); Y[T[i]] = i; } ll odp = 0; mini[n+1] = 1e7; for(int i = n; i >= 1; i--){ mini[i] = min(mini[i+1],Y[i]); maxi[i] = max(maxi[i+1],Y[i]); } for(int i = 1; i <= n;i++){ int p = (i+1)/2 - 1; int p2 = i-p; int dl = maxi[n-p2+1] - mini[n-p2+1] + 1; if(dl > i) continue; int l = max(1,maxi[n-p2+1] - i + 1); int r = min(mini[n-p2+1],n - i + 1); odp += (r-l+1); } printf("%d %lld",2*n+1,odp); return 0; } |