#include <algorithm> #include <array> #include <cassert> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <ranges> #include <set> #include <string> #include <vector> using namespace std; typedef long long LL; #define int LL typedef pair<int, int> PII; const int INF = 1e9 + 1; #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() void solve() { int n; cin >> n; vector<int> p(n); for (auto &x : p) cin >> x; vector<vector<int>> d(n, vector<int>(n, INF)); for (int i = 0; i < n; i++) { d[i][i] = 0; for (int j = i + 1; j < n; j++) { if (p[i] > p[j]) d[i][j] = d[j][i] = 1; } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d[i][j] = min(INF, min(d[i][j], d[i][k] + d[k][j])); } } } for (int i = 0; i < n; i++) { int res = 0; for (int j = 0; j < n; j++) { if (d[i][j] == INF) d[i][j] = 0; res += d[i][j]; } cout << res << " "; } cout << endl; } #undef int int main() { ios::sync_with_stdio(false); cin.exceptions(cin.failbit); cin.tie(0); solve(); }
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 | #include <algorithm> #include <array> #include <cassert> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <ranges> #include <set> #include <string> #include <vector> using namespace std; typedef long long LL; #define int LL typedef pair<int, int> PII; const int INF = 1e9 + 1; #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() void solve() { int n; cin >> n; vector<int> p(n); for (auto &x : p) cin >> x; vector<vector<int>> d(n, vector<int>(n, INF)); for (int i = 0; i < n; i++) { d[i][i] = 0; for (int j = i + 1; j < n; j++) { if (p[i] > p[j]) d[i][j] = d[j][i] = 1; } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d[i][j] = min(INF, min(d[i][j], d[i][k] + d[k][j])); } } } for (int i = 0; i < n; i++) { int res = 0; for (int j = 0; j < n; j++) { if (d[i][j] == INF) d[i][j] = 0; res += d[i][j]; } cout << res << " "; } cout << endl; } #undef int int main() { ios::sync_with_stdio(false); cin.exceptions(cin.failbit); cin.tie(0); solve(); } |