#include <bits/stdc++.h> using namespace std; #define int long long main() { ios_base::sync_with_stdio(false); unordered_map<int, int> M; set<int> S; int n; cin >> n; vector<int> v; for(int i=0; i<n; i++) { int a; cin >> a; v.push_back(a); M[a] = i;} int a = M[n]; int b = M[n]; S.insert(n); int result = 1; // cerr << a << " " << b << endl; for(int i=n-1; i>=1; i--) { int tmp = M[i]; while(tmp < a) { a--; S.insert(v[a]); } while(tmp > b) { b++; S.insert(v[b]); } //2n-1 int step = n-i + 1; // cerr << "step: " << step << endl; // cerr << a << " " << b << endl; // cerr << "result before: " << result <<endl; { int max_size = 2*step - 1; if((int)S.size() <= max_size) { int excess = max_size - S.size(); // cerr << excess << endl; int left = max<int>(0, a-excess); int right = min<int>(n-1, b+excess); int len = right - left + 1; if(len >= max_size) result += len - max_size + 1; } } // cerr << "result mid: " << result <<endl; //2n-2 { int max_size = 2*step - 2; if((int)S.size() <= max_size) { int excess = max_size - S.size(); int left = max<int>(0, a-excess); int right = min<int>(n-1, b+excess); int len = right - left + 1; // cerr << "ex :" << excess << " " << left << " " << right << " " << len << " " << S.size() << endl; if(len >= max_size) result += len - max_size + 1; } } // cerr << "result: " << result << " step: " << step << endl; } cout << n*2+1 << " " << result << endl; 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #include <bits/stdc++.h> using namespace std; #define int long long main() { ios_base::sync_with_stdio(false); unordered_map<int, int> M; set<int> S; int n; cin >> n; vector<int> v; for(int i=0; i<n; i++) { int a; cin >> a; v.push_back(a); M[a] = i;} int a = M[n]; int b = M[n]; S.insert(n); int result = 1; // cerr << a << " " << b << endl; for(int i=n-1; i>=1; i--) { int tmp = M[i]; while(tmp < a) { a--; S.insert(v[a]); } while(tmp > b) { b++; S.insert(v[b]); } //2n-1 int step = n-i + 1; // cerr << "step: " << step << endl; // cerr << a << " " << b << endl; // cerr << "result before: " << result <<endl; { int max_size = 2*step - 1; if((int)S.size() <= max_size) { int excess = max_size - S.size(); // cerr << excess << endl; int left = max<int>(0, a-excess); int right = min<int>(n-1, b+excess); int len = right - left + 1; if(len >= max_size) result += len - max_size + 1; } } // cerr << "result mid: " << result <<endl; //2n-2 { int max_size = 2*step - 2; if((int)S.size() <= max_size) { int excess = max_size - S.size(); int left = max<int>(0, a-excess); int right = min<int>(n-1, b+excess); int len = right - left + 1; // cerr << "ex :" << excess << " " << left << " " << right << " " << len << " " << S.size() << endl; if(len >= max_size) result += len - max_size + 1; } } // cerr << "result: " << result << " step: " << step << endl; } cout << n*2+1 << " " << result << endl; return 0; } |