#include <bits/stdc++.h> using namespace std; template<class c> struct rge { c b, e; }; template<class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template<class c> auto dud(c* x) -> decltype(cerr << *x, 0); template<class c> char dud(...); struct debug { ~debug() { cerr << endl; } template<class c> typename \ enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template<class c> typename \ enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template<class c, class b> debug & operator <<(pair<b, c> d) { return *this << "(" << d.first << ", " << d.second << ")"; } template<class c> debug & operator <<(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) { *this << ", " + 2 * (it == d.b) << *it; } return *this << "]"; } }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " constexpr int MAXN = 200'007; int p[MAXN]; vector<int> graph[MAXN]; int dist[MAXN]; long long result[MAXN]; void bfs(int start, int n) { fill(dist + 1, dist + n + 1, MAXN); dist[start] = 0; queue<int> q; q.push(start); while (!q.empty()) { int u = q.front(); q.pop(); for (auto v : graph[u]) { if (dist[u] + 1 < dist[v]) { dist[v] = dist[u] + 1; q.push(v); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> p[i]; } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (p[i] > p[j]) { graph[i].emplace_back(j); graph[j].emplace_back(i); } } } for (int i = 1; i <= n; i++) { bfs(i, n); for (int j = 1; j <= n; j++) { if (dist[j] != MAXN) { result[i] += dist[j]; } } } for (int i = 1; i <= n; i++) { cout << result[i] << (i == n ? '\n' : ' '); } 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | #include <bits/stdc++.h> using namespace std; template<class c> struct rge { c b, e; }; template<class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template<class c> auto dud(c* x) -> decltype(cerr << *x, 0); template<class c> char dud(...); struct debug { ~debug() { cerr << endl; } template<class c> typename \ enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template<class c> typename \ enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template<class c, class b> debug & operator <<(pair<b, c> d) { return *this << "(" << d.first << ", " << d.second << ")"; } template<class c> debug & operator <<(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) { *this << ", " + 2 * (it == d.b) << *it; } return *this << "]"; } }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " constexpr int MAXN = 200'007; int p[MAXN]; vector<int> graph[MAXN]; int dist[MAXN]; long long result[MAXN]; void bfs(int start, int n) { fill(dist + 1, dist + n + 1, MAXN); dist[start] = 0; queue<int> q; q.push(start); while (!q.empty()) { int u = q.front(); q.pop(); for (auto v : graph[u]) { if (dist[u] + 1 < dist[v]) { dist[v] = dist[u] + 1; q.push(v); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> p[i]; } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (p[i] > p[j]) { graph[i].emplace_back(j); graph[j].emplace_back(i); } } } for (int i = 1; i <= n; i++) { bfs(i, n); for (int j = 1; j <= n; j++) { if (dist[j] != MAXN) { result[i] += dist[j]; } } } for (int i = 1; i <= n; i++) { cout << result[i] << (i == n ? '\n' : ' '); } return 0; } |