#include <bits/stdc++.h> #define ft first #define sc second #define pb push_back #define FOR(i,n) for(int i=0; i<n; i++) #define FORE(i,a,b) for(int i=a; i<=b; i++) #define watch(x) cout << (#x) << " == " << (x) << endl typedef long long ll; typedef long double ld; using namespace std; const ll N =2e5; int tab[N+5]; vector<int> v[N+5]; int dist[N+5]; bool vis[N+5]; ll total = 0; void bfs(int x){ queue<int> q; q.push(x); while (!q.empty()){ x = q.front(); q.pop(); if (vis[x]) {continue;} vis[x] = 1; for (int a : v[x]){ if (!vis[a] && dist[a]==0){ dist[a] = dist[x] + 1; total += dist[a]; q.push(a); } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; FORE(i,1,n){ cin>>tab[i]; } FORE(i,1,n){ FORE(j,i+1,n){ if ( (j<i && tab[i]<tab[j]) || (i<j && tab[j]<tab[i]) ){ v[i].pb(j); v[j].pb(i); } } } FORE(i,1,n){ memset(vis, 0, (n+1)*sizeof(int)); memset(dist, 0, (n+1)*sizeof(int)); total=0; bfs(i); cout<<total<<" "; } 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 | #include <bits/stdc++.h> #define ft first #define sc second #define pb push_back #define FOR(i,n) for(int i=0; i<n; i++) #define FORE(i,a,b) for(int i=a; i<=b; i++) #define watch(x) cout << (#x) << " == " << (x) << endl typedef long long ll; typedef long double ld; using namespace std; const ll N =2e5; int tab[N+5]; vector<int> v[N+5]; int dist[N+5]; bool vis[N+5]; ll total = 0; void bfs(int x){ queue<int> q; q.push(x); while (!q.empty()){ x = q.front(); q.pop(); if (vis[x]) {continue;} vis[x] = 1; for (int a : v[x]){ if (!vis[a] && dist[a]==0){ dist[a] = dist[x] + 1; total += dist[a]; q.push(a); } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; FORE(i,1,n){ cin>>tab[i]; } FORE(i,1,n){ FORE(j,i+1,n){ if ( (j<i && tab[i]<tab[j]) || (i<j && tab[j]<tab[i]) ){ v[i].pb(j); v[j].pb(i); } } } FORE(i,1,n){ memset(vis, 0, (n+1)*sizeof(int)); memset(dist, 0, (n+1)*sizeof(int)); total=0; bfs(i); cout<<total<<" "; } return 0; } |