#include <bits/stdc++.h> #define F first #define S second using namespace std; const int INF = 1e9; vector<int> ri; void rozszerz(int& p,int& k,int& l,int lw){ for(int i=l; i<lw; i++){ p = min(p,ri[i]); k = max(k,ri[i]); } l = lw; } long long licz(int p,int k,int n,int l){ int pp, kk; kk = p + l - 1; pp = k - l + 1; kk = min(kk, n-1); pp = max(pp, 0); return (long long)max(0, (kk - pp + 1) - l + 1); } int main(){ int n; cin >> n; ri.resize(n); for(int i=0,a; i<n; i++){ cin >> a; ri[n-a] = i; } int p,k,l; p = INF; k = -INF; l = 0; long long wynik = 0; for(int i=1; i<=n; i++){ int wolne = (i-1)/2; rozszerz(p,k,l,i-wolne); wynik += licz(p,k,n,i); } cout << 2 * n + 1 << " " << wynik << "\n"; }
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 | #include <bits/stdc++.h> #define F first #define S second using namespace std; const int INF = 1e9; vector<int> ri; void rozszerz(int& p,int& k,int& l,int lw){ for(int i=l; i<lw; i++){ p = min(p,ri[i]); k = max(k,ri[i]); } l = lw; } long long licz(int p,int k,int n,int l){ int pp, kk; kk = p + l - 1; pp = k - l + 1; kk = min(kk, n-1); pp = max(pp, 0); return (long long)max(0, (kk - pp + 1) - l + 1); } int main(){ int n; cin >> n; ri.resize(n); for(int i=0,a; i<n; i++){ cin >> a; ri[n-a] = i; } int p,k,l; p = INF; k = -INF; l = 0; long long wynik = 0; for(int i=1; i<=n; i++){ int wolne = (i-1)/2; rozszerz(p,k,l,i-wolne); wynik += licz(p,k,n,i); } cout << 2 * n + 1 << " " << wynik << "\n"; } |