#include <iostream> // std::cout #include <algorithm> // std::sort #include <vector> // std::vector #include <set> #include <cmath> #include <string> #include <map> #include <cassert> #include <functional> #include <tuple> using namespace std; #define MAXN 1000010 int n, pos[MAXN]; long long res; int main() { scanf("%d", &n); for (int i=0; i<n; ++i){ int x; scanf("%d", &x); pos[x] = i; } for (int l=pos[n], r=pos[n], d=1; d<=n; ++d){ int min_median = n - d / 2; // minimal median rounded down l = min(l, pos[min_median]); r = max(r, pos[min_median]); res += max(min(l+d-1, n-1) - max(r-d+1, 0) - d + 2, 0); } printf("%d %lld", 2*n+1, res); 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 | #include <iostream> // std::cout #include <algorithm> // std::sort #include <vector> // std::vector #include <set> #include <cmath> #include <string> #include <map> #include <cassert> #include <functional> #include <tuple> using namespace std; #define MAXN 1000010 int n, pos[MAXN]; long long res; int main() { scanf("%d", &n); for (int i=0; i<n; ++i){ int x; scanf("%d", &x); pos[x] = i; } for (int l=pos[n], r=pos[n], d=1; d<=n; ++d){ int min_median = n - d / 2; // minimal median rounded down l = min(l, pos[min_median]); r = max(r, pos[min_median]); res += max(min(l+d-1, n-1) - max(r-d+1, 0) - d + 2, 0); } printf("%d %lld", 2*n+1, res); return 0; } |