#include <bits/stdc++.h> #define fi first #define sc second #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k);i>=(p);--i) #define ALL(v) begin(v), end(v) #define RET(smth) return void(cout<<smth<<'\n'); #define SIZ(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif int main() { #ifndef DEBUG ios_base::sync_with_stdio(0); cin.tie(0); #endif int n; cin >> n; vector<int> per(n+1),odl(n+1); vector<long long> ans(n+1); for(int i=1;i<=n;i++) cin >> per[i]; vector<bool> vis(n+1); queue<int> Q; auto bfs = [&](int src) { fill(ALL(vis), false); fill(ALL(odl), 0); long long res = 0; vis[src] = true; Q.push(src); while(!Q.empty()) { auto w = Q.front(); Q.pop(); for(int i=1;i<w;i++) if(!vis[i] && per[w] <= per[i]) { vis[i] = true; odl[i] = odl[w] + 1; res += odl[i]; Q.push(i); } for(int i=w+1;i<=n;i++) if(!vis[i] && per[i] <= per[w]) { vis[i] = true; odl[i] = odl[w] + 1; res += odl[i]; Q.push(i); } } return res; }; for(int i=1;i<=n;i++) ans[i] = bfs(i); for(int i=1;i<=n;i++) cout << ans[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 | #include <bits/stdc++.h> #define fi first #define sc second #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k);i>=(p);--i) #define ALL(v) begin(v), end(v) #define RET(smth) return void(cout<<smth<<'\n'); #define SIZ(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif int main() { #ifndef DEBUG ios_base::sync_with_stdio(0); cin.tie(0); #endif int n; cin >> n; vector<int> per(n+1),odl(n+1); vector<long long> ans(n+1); for(int i=1;i<=n;i++) cin >> per[i]; vector<bool> vis(n+1); queue<int> Q; auto bfs = [&](int src) { fill(ALL(vis), false); fill(ALL(odl), 0); long long res = 0; vis[src] = true; Q.push(src); while(!Q.empty()) { auto w = Q.front(); Q.pop(); for(int i=1;i<w;i++) if(!vis[i] && per[w] <= per[i]) { vis[i] = true; odl[i] = odl[w] + 1; res += odl[i]; Q.push(i); } for(int i=w+1;i<=n;i++) if(!vis[i] && per[i] <= per[w]) { vis[i] = true; odl[i] = odl[w] + 1; res += odl[i]; Q.push(i); } } return res; }; for(int i=1;i<=n;i++) ans[i] = bfs(i); for(int i=1;i<=n;i++) cout << ans[i] << ' '; cout << '\n'; return 0; } |