#include<bits/stdc++.h> using namespace std; typedef long long ll; int32_t main(){ ios::sync_with_stdio(false); ll n; cin >> n; vector<ll> tab(n); ll L=1e9,H=0; vector<ll> where(n+1); for(int i=0;i<n;i++) { cin >> tab[i]; where[tab[i]] = i; } ll res = 0; for(int i=n;i>=(n+1)/2;i--) { L = min(L, where[i]); H = max(H, where[i]); assert(H-L >= n-i); // cout<<i<<" "<<L<<" "<<H<<endl; for(int b=0;b<2;b++) { int len = (n-i)*2+b; if(H-L+1 <= len) { // cout<<len<<endl; ll sL = max(0LL,H-len+1); ll sR = min(L, n-len); // cout<<sL<<" "<<sR<<endl; res += max(0LL,sR-sL+1); } } } cout<<2*n+1<<" "<<res<<"\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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; int32_t main(){ ios::sync_with_stdio(false); ll n; cin >> n; vector<ll> tab(n); ll L=1e9,H=0; vector<ll> where(n+1); for(int i=0;i<n;i++) { cin >> tab[i]; where[tab[i]] = i; } ll res = 0; for(int i=n;i>=(n+1)/2;i--) { L = min(L, where[i]); H = max(H, where[i]); assert(H-L >= n-i); // cout<<i<<" "<<L<<" "<<H<<endl; for(int b=0;b<2;b++) { int len = (n-i)*2+b; if(H-L+1 <= len) { // cout<<len<<endl; ll sL = max(0LL,H-len+1); ll sR = min(L, n-len); // cout<<sL<<" "<<sR<<endl; res += max(0LL,sR-sL+1); } } } cout<<2*n+1<<" "<<res<<"\n"; } |