#include <bits/stdc++.h> using namespace std; typedef long long LL; const int MAX = 1e6+5; int a[MAX]; int id[MAX]; int main() { ios_base::sync_with_stdio(false); int n = 0; cin >> n; for(int i = 0; i < n; ++i) { cin >> a[i]; id[a[i]] = i; } int rl = id[n]; int rr = id[n]; LL res = 1; int ct = 2*n-1; for(int i = n-1; i > 0; --i) { int nw = id[ct/2]; ct--; if(nw < rl) rl = nw; if(nw > rr) rr = nw; int len = rr-rl+1; int maxlen = n-i+1; int freenumbers = maxlen-len; if(freenumbers >= 0) { int spaceleft = min(rl, freenumbers); int spaceright = min(n-rr-1, freenumbers); res += max(0, spaceleft+spaceright-freenumbers+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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; const int MAX = 1e6+5; int a[MAX]; int id[MAX]; int main() { ios_base::sync_with_stdio(false); int n = 0; cin >> n; for(int i = 0; i < n; ++i) { cin >> a[i]; id[a[i]] = i; } int rl = id[n]; int rr = id[n]; LL res = 1; int ct = 2*n-1; for(int i = n-1; i > 0; --i) { int nw = id[ct/2]; ct--; if(nw < rl) rl = nw; if(nw > rr) rr = nw; int len = rr-rl+1; int maxlen = n-i+1; int freenumbers = maxlen-len; if(freenumbers >= 0) { int spaceleft = min(rl, freenumbers); int spaceright = min(n-rr-1, freenumbers); res += max(0, spaceleft+spaceright-freenumbers+1); } } cout << 2*n+1 << ' ' << res << endl; } |