#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair <int,int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 200005;
int n, i, j, tab[N], w, dist[N], res;
VI graf[N];
queue<int> q;
int main() {
scanf("%d", &n);
for (i=1;i<=n;i++) scanf("%d", &tab[i]);
for (i=1;i<=n;i++) for (j=i+1;j<=n;j++) if (tab[i] > tab[j]) {
graf[i].pb(j);
graf[j].pb(i);
}
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) dist[j] = -1;
while (!q.empty()) q.pop();
q.push(i);
dist[i] = 0;
while (!q.empty()) {
w = q.front();
q.pop();
for (auto& v : graf[w]) if (dist[v] == -1) {
dist[v] = dist[w] + 1;
q.push(v);
}
}
res = 0;
for (j=1;j<=n;j++) if (dist[j] != -1) res += dist[j];
printf("%d ", res);
}
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 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 200005; int n, i, j, tab[N], w, dist[N], res; VI graf[N]; queue<int> q; int main() { scanf("%d", &n); for (i=1;i<=n;i++) scanf("%d", &tab[i]); for (i=1;i<=n;i++) for (j=i+1;j<=n;j++) if (tab[i] > tab[j]) { graf[i].pb(j); graf[j].pb(i); } for (i=1;i<=n;i++) { for (j=1;j<=n;j++) dist[j] = -1; while (!q.empty()) q.pop(); q.push(i); dist[i] = 0; while (!q.empty()) { w = q.front(); q.pop(); for (auto& v : graf[w]) if (dist[v] == -1) { dist[v] = dist[w] + 1; q.push(v); } } res = 0; for (j=1;j<=n;j++) if (dist[j] != -1) res += dist[j]; printf("%d ", res); } printf("\n"); return 0; } |
English