#include <cstdio> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <sstream> #include <iostream> #include <map> #include <cmath> #include <set> #include <queue> #include <numeric> #include <ctime> #include <sys/time.h> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define INF 1000000000 #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define FORE(i,a,b) for(int i=(a);i<=(b);i++) #define FS first #define SD second #define MP make_pair #define ZERO(t) memset(t, 0, sizeof(t)) #define MINUS(t) memset(t, -1, sizeof(t)) #define MOD 1000000007 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) #define MAX(_a,_b) ((_a)>(_b)?(_a):(_b)) vector<vector<int> > G; int USED[200000]; LL go(int v) { queue<pair<int, int> > q; LL result = 0; int label = v + 1; USED[v] = label; q.push(MP(v, 0)); while (!q.empty()) { auto p = q.front(); q.pop(); v = p.first; int d = p.second; FOR(i,0,SZ(G[v])) { int u = G[v][i]; if (USED[u] != label) { result += d + 1; q.push(MP(u, d + 1)); USED[u] = label; } } } return result; } int main() { int n; scanf("%d", &n); vector<int> p; FOR(i,0,n) { int a; scanf("%d", &a); p.push_back(a); G.push_back(vector<int>()); } FOR(i,0,n) { FOR(j,0,i) if (p[j] > p[i]) G[i].push_back(j); FOR(j,i+1,n) if (p[j] < p[i]) G[i].push_back(j); } FOR(i,0,n) printf("%lld ", go(i)); printf("\n"); }
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 | #include <cstdio> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <sstream> #include <iostream> #include <map> #include <cmath> #include <set> #include <queue> #include <numeric> #include <ctime> #include <sys/time.h> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define INF 1000000000 #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define FORE(i,a,b) for(int i=(a);i<=(b);i++) #define FS first #define SD second #define MP make_pair #define ZERO(t) memset(t, 0, sizeof(t)) #define MINUS(t) memset(t, -1, sizeof(t)) #define MOD 1000000007 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) #define MAX(_a,_b) ((_a)>(_b)?(_a):(_b)) vector<vector<int> > G; int USED[200000]; LL go(int v) { queue<pair<int, int> > q; LL result = 0; int label = v + 1; USED[v] = label; q.push(MP(v, 0)); while (!q.empty()) { auto p = q.front(); q.pop(); v = p.first; int d = p.second; FOR(i,0,SZ(G[v])) { int u = G[v][i]; if (USED[u] != label) { result += d + 1; q.push(MP(u, d + 1)); USED[u] = label; } } } return result; } int main() { int n; scanf("%d", &n); vector<int> p; FOR(i,0,n) { int a; scanf("%d", &a); p.push_back(a); G.push_back(vector<int>()); } FOR(i,0,n) { FOR(j,0,i) if (p[j] > p[i]) G[i].push_back(j); FOR(j,i+1,n) if (p[j] < p[i]) G[i].push_back(j); } FOR(i,0,n) printf("%lld ", go(i)); printf("\n"); } |