#include <bits/stdc++.h> using namespace std; typedef long long ll; //Autor: Michał Szeliga #ifdef LOCAL #define debug(...) __VA_ARGS__; #else #define debug(...) {} #endif vector<ll> sas[200001]; ll n; bool vis[200001]; ll dist[200001]; ll solve(ll start){ for (ll i = 1; i < n+1; i++) dist[i] = vis[i] = 0; queue<ll> q; q.push(start); while (q.size()){ ll v = q.front(); vis[v] = 1; q.pop(); for (ll u : sas[v]){ if (vis[u]) continue; vis[u] = 1; dist[u] = dist[v]+1; q.push(u); } } ll suma = 0; for (ll i = 1; i < n+1; i++){ //cout<<dist[i]<<" "; suma += dist[i]; } //cout<<" # "; return suma; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll i; cin>>n; ll tab[n+1]; for (i = 1; i < n+1; i++) cin>>tab[i]; for (i = 1; i < n+1; i++){ for (ll j = i+1; j < n+1; j++){ if ((i<j) != (tab[i]<tab[j])){ //cout<<i<<" "<<j<<"\n"; sas[i].push_back(j); sas[j].push_back(i); } } } for (i = 1; i < n+1; i++){ cout<<solve(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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; //Autor: Michał Szeliga #ifdef LOCAL #define debug(...) __VA_ARGS__; #else #define debug(...) {} #endif vector<ll> sas[200001]; ll n; bool vis[200001]; ll dist[200001]; ll solve(ll start){ for (ll i = 1; i < n+1; i++) dist[i] = vis[i] = 0; queue<ll> q; q.push(start); while (q.size()){ ll v = q.front(); vis[v] = 1; q.pop(); for (ll u : sas[v]){ if (vis[u]) continue; vis[u] = 1; dist[u] = dist[v]+1; q.push(u); } } ll suma = 0; for (ll i = 1; i < n+1; i++){ //cout<<dist[i]<<" "; suma += dist[i]; } //cout<<" # "; return suma; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll i; cin>>n; ll tab[n+1]; for (i = 1; i < n+1; i++) cin>>tab[i]; for (i = 1; i < n+1; i++){ for (ll j = i+1; j < n+1; j++){ if ((i<j) != (tab[i]<tab[j])){ //cout<<i<<" "<<j<<"\n"; sas[i].push_back(j); sas[j].push_back(i); } } } for (i = 1; i < n+1; i++){ cout<<solve(i)<<" "; } cout<<"\n"; return 0; } |