// Karol Kosinski 2022 #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define TIE(x...) int x;tie(x) #define X first #define Y second #ifndef ENABLE_DEBUG #define DEB(k,p,f,x...) #else #define DEB(k,p,f,x...) {if(k)printf("--------%4d : %s\n",__LINE__,__FUNCTION__);if(p)f(x);} #endif #define DEBL DEB(1,1,void,0) #define DEBF(f,x...) DEB(1,1,f,x) #define DEBC(p,x...) DEB(0,p,printf,x) #define DEBUG(x...) DEB(0,1,printf,x) using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; constexpr int NX = 200'005; namespace BFS { static constexpr int INFTY = 1e9 + 1; int n, D[NX]; vector<int> G[NX], Used; void add(int u, int v) { G[u].push_back(v); } void clear() { FOR(i,0,n) G[i].clear(); } LL init(int ng, int root) { n = ng; queue<int> Q; while ( not Used.empty() ) { D[ Used.back() ] = INFTY; Used.pop_back(); } D[root] = 0; LL sum = 0; Q.push(root); Used.push_back(root); while ( not Q.empty() ) { int u = Q.front(); Q.pop(); for ( int v : G[u] ) if ( D[v] == INFTY ) { D[v] = D[u] + 1; sum += D[v]; Q.push(v); Used.push_back(v); DEBUG("\n** %d --> %d (%lld)\n", u, v, sum); } } return sum; } } int A[NX]; int main() { int n; scanf("%d", &n); FR_(i,1,n) scanf("%d", A + i ); FR_(i,1,n) { int k = A[i]; FR_(j,i+1,n) { if ( A[j] < k ) { BFS::add( i - 1, j - 1 ); BFS::add( j - 1, i - 1 ); } } } FOR(i,0,n) { BFS::D[i] = BFS::INFTY; } FOR(i,0,n) { printf("%lld ", BFS::init(n, i)); } printf("\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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | // Karol Kosinski 2022 #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define TIE(x...) int x;tie(x) #define X first #define Y second #ifndef ENABLE_DEBUG #define DEB(k,p,f,x...) #else #define DEB(k,p,f,x...) {if(k)printf("--------%4d : %s\n",__LINE__,__FUNCTION__);if(p)f(x);} #endif #define DEBL DEB(1,1,void,0) #define DEBF(f,x...) DEB(1,1,f,x) #define DEBC(p,x...) DEB(0,p,printf,x) #define DEBUG(x...) DEB(0,1,printf,x) using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; constexpr int NX = 200'005; namespace BFS { static constexpr int INFTY = 1e9 + 1; int n, D[NX]; vector<int> G[NX], Used; void add(int u, int v) { G[u].push_back(v); } void clear() { FOR(i,0,n) G[i].clear(); } LL init(int ng, int root) { n = ng; queue<int> Q; while ( not Used.empty() ) { D[ Used.back() ] = INFTY; Used.pop_back(); } D[root] = 0; LL sum = 0; Q.push(root); Used.push_back(root); while ( not Q.empty() ) { int u = Q.front(); Q.pop(); for ( int v : G[u] ) if ( D[v] == INFTY ) { D[v] = D[u] + 1; sum += D[v]; Q.push(v); Used.push_back(v); DEBUG("\n** %d --> %d (%lld)\n", u, v, sum); } } return sum; } } int A[NX]; int main() { int n; scanf("%d", &n); FR_(i,1,n) scanf("%d", A + i ); FR_(i,1,n) { int k = A[i]; FR_(j,i+1,n) { if ( A[j] < k ) { BFS::add( i - 1, j - 1 ); BFS::add( j - 1, i - 1 ); } } } FOR(i,0,n) { BFS::D[i] = BFS::INFTY; } FOR(i,0,n) { printf("%lld ", BFS::init(n, i)); } printf("\n"); return 0; } |