#include <iostream> #include <vector> #include <algorithm> using namespace std; int t[1000010]; int mn[1000010]; int mx[1000010]; int main() { int n; scanf("%d",&n); for (int i=0;i<n;i++) { scanf("%d",&t[i]); t[i]--; mn[t[i]]=i; mx[t[i]]=i; } mn[n]=n+1; mx[n]=-1; for (int i=n-1;i>=0;i--) { mn[i] = min(mn[i],mn[i+1]); mx[i] = max(mx[i],mx[i+1]); } long long res = 0; for (int i=0;i<=n/2;i++) { int a=mn[n - 1 - i]; int b=mx[n - 1 - i]; int L=2*i; if (L >= (b - a + 1)) { res += min(a, n - L ) - max(0, b - L + 1) + 1; } L++; if (L >= (b - a + 1)) { res += min(a, n - L ) - max(0, b - L + 1) + 1; } } cout << (2*n + 1) << " " << (res) << endl; }
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 <iostream> #include <vector> #include <algorithm> using namespace std; int t[1000010]; int mn[1000010]; int mx[1000010]; int main() { int n; scanf("%d",&n); for (int i=0;i<n;i++) { scanf("%d",&t[i]); t[i]--; mn[t[i]]=i; mx[t[i]]=i; } mn[n]=n+1; mx[n]=-1; for (int i=n-1;i>=0;i--) { mn[i] = min(mn[i],mn[i+1]); mx[i] = max(mx[i],mx[i+1]); } long long res = 0; for (int i=0;i<=n/2;i++) { int a=mn[n - 1 - i]; int b=mx[n - 1 - i]; int L=2*i; if (L >= (b - a + 1)) { res += min(a, n - L ) - max(0, b - L + 1) + 1; } L++; if (L >= (b - a + 1)) { res += min(a, n - L ) - max(0, b - L + 1) + 1; } } cout << (2*n + 1) << " " << (res) << endl; } |