#include<bits/stdc++.h> #define st first #define nd second #define pb(x) push_back(x) #define pp(x) pop_back(x) #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); const int MN = 10009; int tab[MN]; int n; vi G[MN]; void read(){ cin >> n; for(int i = 1; i <= n; i++) cin >> tab[i]; for(int i = 1; i <= n; i++){ for(int j = 1; j < i; j++) if(tab[j] > tab[i]) G[i].pb(j); for(int j = i+1; j <=n; j++) if(tab[j] < tab[i]) G[i].pb(j); } /*for(int i =1; i <=n ; i++){ cout << i << ": "; for(auto x: G[i]) cout << x << ' '; cout << "\n"; }*/ } bitset<MN> wrzuc; int dist[MN]; queue <int> Q; void bfs(int v){ ll result = 0; wrzuc.reset(); dist[v] = 0; wrzuc[v] = 1; Q.push(v); int w; while(!Q.empty()){ w = Q.front(); Q.pop(); for(auto x: G[w]){ if(!wrzuc[x]){ wrzuc[x] = 1; dist[x] = dist[w] + 1; result += dist[w] + 1; Q.push(x); } } } cout << result << ' '; } int main(){ BOOST read(); for(int i =1; i <= n; i++) bfs(i); 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include<bits/stdc++.h> #define st first #define nd second #define pb(x) push_back(x) #define pp(x) pop_back(x) #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); const int MN = 10009; int tab[MN]; int n; vi G[MN]; void read(){ cin >> n; for(int i = 1; i <= n; i++) cin >> tab[i]; for(int i = 1; i <= n; i++){ for(int j = 1; j < i; j++) if(tab[j] > tab[i]) G[i].pb(j); for(int j = i+1; j <=n; j++) if(tab[j] < tab[i]) G[i].pb(j); } /*for(int i =1; i <=n ; i++){ cout << i << ": "; for(auto x: G[i]) cout << x << ' '; cout << "\n"; }*/ } bitset<MN> wrzuc; int dist[MN]; queue <int> Q; void bfs(int v){ ll result = 0; wrzuc.reset(); dist[v] = 0; wrzuc[v] = 1; Q.push(v); int w; while(!Q.empty()){ w = Q.front(); Q.pop(); for(auto x: G[w]){ if(!wrzuc[x]){ wrzuc[x] = 1; dist[x] = dist[w] + 1; result += dist[w] + 1; Q.push(x); } } } cout << result << ' '; } int main(){ BOOST read(); for(int i =1; i <= n; i++) bfs(i); return 0; } |