#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double db; typedef pair<int,int> pii; typedef vector<int> vi; const int N = 2e5+5; const int INF = 1e9; int lina[N]; int dist[N]; vector<vector<int>> g(N); void bfs(int v, int n){ queue<int> q; q.push(v); for(int i=0; i<=n; ++i){ dist[i] = INF; } dist[v] = 0; while(!q.empty()){ v = q.front(); q.pop(); for(auto &u : g[v]){ if(dist[u] >= INF){ dist[u] = dist[v] + 1; q.push(u); } } } } int main(){ ios_base::sync_with_stdio(0); int n; cin >> n; for(int i=0; i<n; ++i){ cin >> lina[i]; } for(int a=0; a<n; ++a){ for(int b=a+1; b<n; ++b){ if(a<b != lina[a]<lina[b]){ g[a].push_back(b); g[b].push_back(a); } } } for(int v=0; v<n; ++v){ bfs(v,n); ll res = 0; for(int u=0; u<n; ++u){ if(dist[u]<INF) res += dist[u]; } cout << res << ' '; } 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double db; typedef pair<int,int> pii; typedef vector<int> vi; const int N = 2e5+5; const int INF = 1e9; int lina[N]; int dist[N]; vector<vector<int>> g(N); void bfs(int v, int n){ queue<int> q; q.push(v); for(int i=0; i<=n; ++i){ dist[i] = INF; } dist[v] = 0; while(!q.empty()){ v = q.front(); q.pop(); for(auto &u : g[v]){ if(dist[u] >= INF){ dist[u] = dist[v] + 1; q.push(u); } } } } int main(){ ios_base::sync_with_stdio(0); int n; cin >> n; for(int i=0; i<n; ++i){ cin >> lina[i]; } for(int a=0; a<n; ++a){ for(int b=a+1; b<n; ++b){ if(a<b != lina[a]<lina[b]){ g[a].push_back(b); g[b].push_back(a); } } } for(int v=0; v<n; ++v){ bfs(v,n); ll res = 0; for(int u=0; u<n; ++u){ if(dist[u]<INF) res += dist[u]; } cout << res << ' '; } return 0; } |