#include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ int n; cin >> n; vector<int> numbers(n); vector<int> position(n+1); for(int i=0;i<n;i++){ cin >> numbers[i]; position[numbers[i]] = i; } int bigger = n, smaller = n; int _max = position[n], _min = position[n]; long long result = 0; for(int i=1;i<=n;i++){ _max=max(_max,max(position[bigger], position[smaller])); _min=min(_min,min(position[bigger], position[smaller])); if(_max-_min+1<=i){ int start = max(_max-i+1, 0); int end = min(_min, n-i); result += (end-start + 1); } if(smaller==bigger){ smaller--; }else{ bigger--; } } cout << 2*n+1 << " " << result << 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 | #include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ int n; cin >> n; vector<int> numbers(n); vector<int> position(n+1); for(int i=0;i<n;i++){ cin >> numbers[i]; position[numbers[i]] = i; } int bigger = n, smaller = n; int _max = position[n], _min = position[n]; long long result = 0; for(int i=1;i<=n;i++){ _max=max(_max,max(position[bigger], position[smaller])); _min=min(_min,min(position[bigger], position[smaller])); if(_max-_min+1<=i){ int start = max(_max-i+1, 0); int end = min(_min, n-i); result += (end-start + 1); } if(smaller==bigger){ smaller--; }else{ bigger--; } } cout << 2*n+1 << " " << result << endl; } |