#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } int main () { int n; scanf("%d", &n); vi V(n); FOR(i,0,n) scanf("%d", &V[i]); ll ans = 1; vi idx(n); FOR(i,0,n) idx[V[i] - 1] = i; int b = idx.back(), e = idx.back() + 1; FORD(i,n-2,0){ int ile = n - i; if (ile % 2 == 0){ int myidx = n - 1 - ile / 2; b = min(b, idx[myidx]); e = max(e, idx[myidx] + 1); } int wolne = ile - e + b; if (wolne < 0) continue; //cout << ile << ": " << min(b, n - ile) + 1 - max(0, e - ile) << endl; ans += min(b, n - ile) + 1 - max(0, e - ile); } assert(ans > 0); printf("%d %lld\n", 2 * n + 1, ans); }
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 | #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } int main () { int n; scanf("%d", &n); vi V(n); FOR(i,0,n) scanf("%d", &V[i]); ll ans = 1; vi idx(n); FOR(i,0,n) idx[V[i] - 1] = i; int b = idx.back(), e = idx.back() + 1; FORD(i,n-2,0){ int ile = n - i; if (ile % 2 == 0){ int myidx = n - 1 - ile / 2; b = min(b, idx[myidx]); e = max(e, idx[myidx] + 1); } int wolne = ile - e + b; if (wolne < 0) continue; //cout << ile << ": " << min(b, n - ile) + 1 - max(0, e - ile) << endl; ans += min(b, n - ile) + 1 - max(0, e - ile); } assert(ans > 0); printf("%d %lld\n", 2 * n + 1, ans); } |