#include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> v(n); vector<vector<int>> g(n); for(int& i : v) cin >> i; for(int i = 0; i < n; i++) { for(int j = 0; j < i; j++) { if(v[j] > v[i]) { g[i].push_back(j); g[j].push_back(i); } } } for(int i = 0; i < n; i++) { vector<int> d(n, 1e9); d[i] = 0; queue<int> q; q.push(i); while(!q.empty()) { int v = q.front(); q.pop(); for(int to : g[v]) { if(d[to] == 1e9) { d[to] = d[v] + 1; q.push(to); } } } long long sm = 0; for(int j = 0; j < n; j++) { sm += d[j] == 1e9 ? 0 : d[j]; } if(i) cout << " "; cout << sm; } cout << endl; 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 | #include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> v(n); vector<vector<int>> g(n); for(int& i : v) cin >> i; for(int i = 0; i < n; i++) { for(int j = 0; j < i; j++) { if(v[j] > v[i]) { g[i].push_back(j); g[j].push_back(i); } } } for(int i = 0; i < n; i++) { vector<int> d(n, 1e9); d[i] = 0; queue<int> q; q.push(i); while(!q.empty()) { int v = q.front(); q.pop(); for(int to : g[v]) { if(d[to] == 1e9) { d[to] = d[v] + 1; q.push(to); } } } long long sm = 0; for(int j = 0; j < n; j++) { sm += d[j] == 1e9 ? 0 : d[j]; } if(i) cout << " "; cout << sm; } cout << endl; return 0; } |