// Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif typedef long long ll; int n; vector<int> perm; vector<vector<int>> g; vector<ll> dist; ll bfs(int root) { fill(dist.begin(), dist.end(), -1); queue<int> q; q.push(root); dist[root] = 0; while(!q.empty()) { int v = q.front(); q.pop(); for(auto e : g[v]) { if(dist[e] == -1) { dist[e] = dist[v] + 1; q.push(e); } } } ll res = 0; for(int i=0;i<n;i++) { if(dist[i] != -1) res += dist[i]; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; perm.resize(n); for(auto &v : perm) { cin>>v; v--; } g.resize(n); for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(perm[i] > perm[j]) { g[i].push_back(j); g[j].push_back(i); } } } dist.resize(n); for(int i=0;i<n;i++) { cout<<bfs(i)<<' '; } cout<<'\n'; }
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 | // Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif typedef long long ll; int n; vector<int> perm; vector<vector<int>> g; vector<ll> dist; ll bfs(int root) { fill(dist.begin(), dist.end(), -1); queue<int> q; q.push(root); dist[root] = 0; while(!q.empty()) { int v = q.front(); q.pop(); for(auto e : g[v]) { if(dist[e] == -1) { dist[e] = dist[v] + 1; q.push(e); } } } ll res = 0; for(int i=0;i<n;i++) { if(dist[i] != -1) res += dist[i]; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; perm.resize(n); for(auto &v : perm) { cin>>v; v--; } g.resize(n); for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(perm[i] > perm[j]) { g[i].push_back(j); g[j].push_back(i); } } } dist.resize(n); for(int i=0;i<n;i++) { cout<<bfs(i)<<' '; } cout<<'\n'; } |