#include <bits/stdc++.h> using namespace std; #define fi first #define se second typedef long long ll; const int N = 2e5 + 5; const int INF = 1e9 + 5; int kon[N]; bool odw[N]; int dis[N]; vector <int> kraw[N]; int n; ll bfs(int r){ for(int i = 1; i <= n; i++){ odw[i] = 0; dis[i] = 0; } odw[r] = 1; //dis[r] = 0; queue <int> q; //-dis, x q.push(r); while(!q.empty()){ int x = q.front(); q.pop(); //if(odw[x]) continue; //odw[x] = 1; for(int v : kraw[x]){ if(odw[v]) continue; dis[v] = dis[x] + 1; odw[v] = 1; q.push(v); } } ll wyn = 0; //cout << r << ": "; for(int i = 1; i <= n; i++){ wyn += dis[i]; //cout << i << " " << dis[i] << "\n"; } //cout << "\n"; return wyn; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 1; i <= n; i++) cin >> kon[i]; for(int i = 1; i <= n; i++){ for(int j = 1; j < i; j++){ if(kon[j] < kon[i]) continue; kraw[i].push_back(j); } for(int j = i + 1; j <= n; j++){ if(kon[i] < kon[j]) continue; kraw[i].push_back(j); } //sort(kraw[i].begin(), kraw[i].end()); } /*for(int i = 1; i <= n; i++){ cout << i << ": "; for(int j = 0; j < kraw[i].size(); j++){ cout << kraw[i][j].fi.fi << " " << kraw[i][j].fi.se << " " << kraw[i][j].se << "\n"; } cout << "\n"; }*/ for(int i = 1; i <= n; i++){ cout << bfs(i) << " "; //cout << wyn[i] << "\n"; } 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #include <bits/stdc++.h> using namespace std; #define fi first #define se second typedef long long ll; const int N = 2e5 + 5; const int INF = 1e9 + 5; int kon[N]; bool odw[N]; int dis[N]; vector <int> kraw[N]; int n; ll bfs(int r){ for(int i = 1; i <= n; i++){ odw[i] = 0; dis[i] = 0; } odw[r] = 1; //dis[r] = 0; queue <int> q; //-dis, x q.push(r); while(!q.empty()){ int x = q.front(); q.pop(); //if(odw[x]) continue; //odw[x] = 1; for(int v : kraw[x]){ if(odw[v]) continue; dis[v] = dis[x] + 1; odw[v] = 1; q.push(v); } } ll wyn = 0; //cout << r << ": "; for(int i = 1; i <= n; i++){ wyn += dis[i]; //cout << i << " " << dis[i] << "\n"; } //cout << "\n"; return wyn; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 1; i <= n; i++) cin >> kon[i]; for(int i = 1; i <= n; i++){ for(int j = 1; j < i; j++){ if(kon[j] < kon[i]) continue; kraw[i].push_back(j); } for(int j = i + 1; j <= n; j++){ if(kon[i] < kon[j]) continue; kraw[i].push_back(j); } //sort(kraw[i].begin(), kraw[i].end()); } /*for(int i = 1; i <= n; i++){ cout << i << ": "; for(int j = 0; j < kraw[i].size(); j++){ cout << kraw[i][j].fi.fi << " " << kraw[i][j].fi.se << " " << kraw[i][j].se << "\n"; } cout << "\n"; }*/ for(int i = 1; i <= n; i++){ cout << bfs(i) << " "; //cout << wyn[i] << "\n"; } cout << "\n"; } |