#include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> #include <numeric> #include <deque> using namespace std; #define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b)) using ll = long long; const int INF = 1e9; int n; vector<vector<int>> dist; vector<vector<bool>> adj; vector<vector<int>> g; void bfs() { for (int root = 0; root < n; ++root) { deque<pair<int, int>> q = {{root, 0}}; vector<bool> added(n); added[root] = true; while (!q.empty()) { int v = q.front().first; int d = q.front().second; q.pop_front(); dist[root][v] = d; for (auto u : g[v]) { if (!added[u]) { q.push_back({u, d+1}); added[u] = true; } } } } } void floyd() { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j) { dist[i][j] = 0; } else if (adj[i][j]) { dist[i][j] = 1; } else { dist[i][j] = INF; } } } for (int k = 0; k < n; ++k) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { int tmp = dist[i][k] + dist[k][j]; if (dist[i][j] > tmp) { dist[i][j] = tmp; } } } } // assign 0 to unreachable pairs for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (dist[i][j] == INF) { dist[i][j] = 0; } } } } int main() { scanf("%d", &n); dist = vector<vector<int>>(n, vector<int>(n)); vector<int> p(n); for (int i = 0; i < n; ++i) { scanf("%d", &p[i]); p[i]--; } adj = vector<vector<bool>>(n, vector<bool>(n)); g = vector<vector<int>>(n); for (int i = 0; i < n; ++i) { for (int j = i+1; j < n; ++j) { if (p[i] > p[j]) { adj[i][j] = true; adj[j][i] = true; g[i].push_back(j); g[j].push_back(i); } } } //floyd(); bfs(); for (int i = 0; i < n; ++i) { ll sum = 0; for (int j = 0; j < n; ++j) { sum += dist[i][j]; } printf("%lld ", sum); } printf("\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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> #include <numeric> #include <deque> using namespace std; #define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b)) using ll = long long; const int INF = 1e9; int n; vector<vector<int>> dist; vector<vector<bool>> adj; vector<vector<int>> g; void bfs() { for (int root = 0; root < n; ++root) { deque<pair<int, int>> q = {{root, 0}}; vector<bool> added(n); added[root] = true; while (!q.empty()) { int v = q.front().first; int d = q.front().second; q.pop_front(); dist[root][v] = d; for (auto u : g[v]) { if (!added[u]) { q.push_back({u, d+1}); added[u] = true; } } } } } void floyd() { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j) { dist[i][j] = 0; } else if (adj[i][j]) { dist[i][j] = 1; } else { dist[i][j] = INF; } } } for (int k = 0; k < n; ++k) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { int tmp = dist[i][k] + dist[k][j]; if (dist[i][j] > tmp) { dist[i][j] = tmp; } } } } // assign 0 to unreachable pairs for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (dist[i][j] == INF) { dist[i][j] = 0; } } } } int main() { scanf("%d", &n); dist = vector<vector<int>>(n, vector<int>(n)); vector<int> p(n); for (int i = 0; i < n; ++i) { scanf("%d", &p[i]); p[i]--; } adj = vector<vector<bool>>(n, vector<bool>(n)); g = vector<vector<int>>(n); for (int i = 0; i < n; ++i) { for (int j = i+1; j < n; ++j) { if (p[i] > p[j]) { adj[i][j] = true; adj[j][i] = true; g[i].push_back(j); g[j].push_back(i); } } } //floyd(); bfs(); for (int i = 0; i < n; ++i) { ll sum = 0; for (int j = 0; j < n; ++j) { sum += dist[i][j]; } printf("%lld ", sum); } printf("\n"); return 0; } |