#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int n; cin>>n; vector<pair<int,int>> tab(n+1); tab[0]=make_pair(0,0); for(int i=1;i<=n;i++) { cin>>tab[i].first; tab[i].second=i; } sort(tab.begin(),tab.end()); vector<pair<int,int>> max_intervals(n+1); max_intervals[n]=make_pair(tab[n].second,tab[n].second); for(int i=n-1;i>0;i--) { int x=tab[i].second; int p=max_intervals[i+1].first; int k=max_intervals[i+1].second; max_intervals[i].first=min(p,x); max_intervals[i].second=max(k,x); } /*for(int i=1;i<=n;i++) { cout<<max_intervals[i].first<<" "<<max_intervals[i].second<<endl; }*/ long long sum=0; for(int s=1;s<=n;s++) { int x=s/2+1; x=n+1-x; //cout<<"#"<<s<<" "<<x<<endl; int p=max_intervals[x].first; int k=max_intervals[x].second; //cout<<p<<" "<<k<<endl; if(k-p+1>s) { continue; } int p_1=max(1,k-s+1); int k_1=min(s+p-1,n); //cout<<p_1<<" "<<k_1<<endl; int moving=k_1-p_1-s+2; //cout<<moving<<endl; sum+=moving; } cout<<2*n+1<<" "<<sum<<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 73 74 75 76 77 78 | #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int n; cin>>n; vector<pair<int,int>> tab(n+1); tab[0]=make_pair(0,0); for(int i=1;i<=n;i++) { cin>>tab[i].first; tab[i].second=i; } sort(tab.begin(),tab.end()); vector<pair<int,int>> max_intervals(n+1); max_intervals[n]=make_pair(tab[n].second,tab[n].second); for(int i=n-1;i>0;i--) { int x=tab[i].second; int p=max_intervals[i+1].first; int k=max_intervals[i+1].second; max_intervals[i].first=min(p,x); max_intervals[i].second=max(k,x); } /*for(int i=1;i<=n;i++) { cout<<max_intervals[i].first<<" "<<max_intervals[i].second<<endl; }*/ long long sum=0; for(int s=1;s<=n;s++) { int x=s/2+1; x=n+1-x; //cout<<"#"<<s<<" "<<x<<endl; int p=max_intervals[x].first; int k=max_intervals[x].second; //cout<<p<<" "<<k<<endl; if(k-p+1>s) { continue; } int p_1=max(1,k-s+1); int k_1=min(s+p-1,n); //cout<<p_1<<" "<<k_1<<endl; int moving=k_1-p_1-s+2; //cout<<moving<<endl; sum+=moving; } cout<<2*n+1<<" "<<sum<<endl; return 0; } |