#include <bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define st first #define nd second #define pii pair<int, int> #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() int tab[200009]; vector<int> G[200009]; int dis[200009]; bool vis[200009]; queue<int> Q; int bfs(int x) { Q.push(x); vis[x] = true; int ans = 0; while(sz(Q)) { int y = Q.front(); Q.pop(); for(int z:G[y]) { if(!vis[z]) { vis[z] = true; dis[z] = dis[y] + 1; Q.push(z); ans += dis[z]; } } } return ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for(int i=1;i<=n;i++) { cin >> tab[i]; } for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { if(tab[j] < tab[i]) { G[i].pb(j); G[j].pb(i); } } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { vis[j] = false; dis[j] = 0; } 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 | #include <bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define st first #define nd second #define pii pair<int, int> #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() int tab[200009]; vector<int> G[200009]; int dis[200009]; bool vis[200009]; queue<int> Q; int bfs(int x) { Q.push(x); vis[x] = true; int ans = 0; while(sz(Q)) { int y = Q.front(); Q.pop(); for(int z:G[y]) { if(!vis[z]) { vis[z] = true; dis[z] = dis[y] + 1; Q.push(z); ans += dis[z]; } } } return ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for(int i=1;i<=n;i++) { cin >> tab[i]; } for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { if(tab[j] < tab[i]) { G[i].pb(j); G[j].pb(i); } } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { vis[j] = false; dis[j] = 0; } cout << bfs(i) << " "; } cout << "\n"; } |