#include <iostream> #include <algorithm> #include <vector> using namespace std; vector <long long> g; pair <long long,long long> t[1000006]; long long w,mini,maxi,mini2,maxi2; long long res; int main(){ ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; res = (n + 1) + n; for(int i = 0; i < n; i++){ cin >> t[i].first; t[i].second = i; } sort(t,t + n); for(int i = n-1; i >= n/2 /*- 1 + (n % 2)*/; i--) g.push_back(t[i].second); mini = maxi = mini2 = maxi2 = g[0]; long long k = 1; for(int j = 0; j < g.size(); j++){ long long i = g[j]; mini = min(i,mini); maxi = max(i,maxi); i = g[j+1]; mini2 = min(i,mini2); maxi2 = max(i,maxi2); long long temp = min(mini,n - k) - max(maxi + 1 - k,0ll); temp++; temp = max(0ll,temp); long long temp2 = min(mini2,n - (k + 1)) - max(maxi2 - k,0ll); temp2++; temp2 = max(0ll,temp2); k += 2; w += temp + temp2; } cout << res << " " << w << 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 | #include <iostream> #include <algorithm> #include <vector> using namespace std; vector <long long> g; pair <long long,long long> t[1000006]; long long w,mini,maxi,mini2,maxi2; long long res; int main(){ ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; res = (n + 1) + n; for(int i = 0; i < n; i++){ cin >> t[i].first; t[i].second = i; } sort(t,t + n); for(int i = n-1; i >= n/2 /*- 1 + (n % 2)*/; i--) g.push_back(t[i].second); mini = maxi = mini2 = maxi2 = g[0]; long long k = 1; for(int j = 0; j < g.size(); j++){ long long i = g[j]; mini = min(i,mini); maxi = max(i,maxi); i = g[j+1]; mini2 = min(i,mini2); maxi2 = max(i,maxi2); long long temp = min(mini,n - k) - max(maxi + 1 - k,0ll); temp++; temp = max(0ll,temp); long long temp2 = min(mini2,n - (k + 1)) - max(maxi2 - k,0ll); temp2++; temp2 = max(0ll,temp2); k += 2; w += temp + temp2; } cout << res << " " << w << endl; } |