#include <iostream> #include <vector> #include <queue> using namespace std; int main () { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int>P(n); for (auto& x : P) { cin >> x; x--; } vector<vector<int>>G(n); for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (P[i] > P[j]) { G[i].push_back(j); G[j].push_back(i); } for (int i = 0; i < n; i++) { vector<int>Dist(n, -1); Dist[i] = 0; queue<int>Q; Q.push(i); long long result = 0; while (!Q.empty()) { int q = Q.front(); Q.pop(); result += Dist[q]; for (int nei : G[q]) if (Dist[nei] == -1) { Dist[nei] = Dist[q] + 1; Q.push(nei); } } cout << result << " "; } cout << "\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 | #include <iostream> #include <vector> #include <queue> using namespace std; int main () { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int>P(n); for (auto& x : P) { cin >> x; x--; } vector<vector<int>>G(n); for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (P[i] > P[j]) { G[i].push_back(j); G[j].push_back(i); } for (int i = 0; i < n; i++) { vector<int>Dist(n, -1); Dist[i] = 0; queue<int>Q; Q.push(i); long long result = 0; while (!Q.empty()) { int q = Q.front(); Q.pop(); result += Dist[q]; for (int nei : G[q]) if (Dist[nei] == -1) { Dist[nei] = Dist[q] + 1; Q.push(nei); } } cout << result << " "; } cout << "\n"; return 0; } |