/* Zadanie: Chodzenie po linie Autor: Tomasz Kwiatkowski */ #include <bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; typedef long long ll; const int MAXN = 2e5 + 7; const int INF = 1e9 + 7; vector<int> G[MAXN]; int n; ll BFS(int s) { vector<bool> vis(n, 0); vis[s] = 1; queue<pair<int, int>> Q; Q.push({s, 0}); ll res = 0; while (Q.size()) { auto [v, d] = Q.front(); Q.pop(); res += d; for (auto u : G[v]) { if (!vis[u]) { vis[u] = 1; Q.push({u, d + 1}); } } } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; vector<int> p(n); for (auto& x : p) cin >> x; for (int i = 0; i < n; ++i) { for (int j = 0; j < i; ++j) { if (p[j] > p[i]) { G[j].pb(i); G[i].pb(j); } } } for (int i = 0; i < n; ++i) cout << BFS(i) << ' '; 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 52 53 54 55 56 57 58 59 60 61 62 63 | /* Zadanie: Chodzenie po linie Autor: Tomasz Kwiatkowski */ #include <bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; typedef long long ll; const int MAXN = 2e5 + 7; const int INF = 1e9 + 7; vector<int> G[MAXN]; int n; ll BFS(int s) { vector<bool> vis(n, 0); vis[s] = 1; queue<pair<int, int>> Q; Q.push({s, 0}); ll res = 0; while (Q.size()) { auto [v, d] = Q.front(); Q.pop(); res += d; for (auto u : G[v]) { if (!vis[u]) { vis[u] = 1; Q.push({u, d + 1}); } } } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; vector<int> p(n); for (auto& x : p) cin >> x; for (int i = 0; i < n; ++i) { for (int j = 0; j < i; ++j) { if (p[j] > p[i]) { G[j].pb(i); G[i].pb(j); } } } for (int i = 0; i < n; ++i) cout << BFS(i) << ' '; cout << '\n'; return 0; } |