#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int main () {
int n;
scanf("%d", &n);
vector<int> lines;
lines.reserve(n + 8);
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
--x;
lines.push_back(x);
}
vector<vector<int> > adj(n, vector<int>());
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (lines[j] < lines[i]) {
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
for (int i = 0; i < n; ++i) {
queue<int> q;
vector<int> dists(n, 0);
q.push(i);
int sum = 0;
while (!q.empty()) {
int item = q.front();
q.pop();
for (int j = 0; j < adj[item].size(); ++j) {
if (adj[item][j] == i) {
continue;
}
if (dists[adj[item][j]] != 0) {
continue;
}
dists[adj[item][j]] = dists[item] + 1;
sum += dists[item] + 1;
q.push(adj[item][j]);
}
}
printf("%d ", sum);
}
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 | #include <cstdio> #include <vector> #include <queue> using namespace std; int main () { int n; scanf("%d", &n); vector<int> lines; lines.reserve(n + 8); for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); --x; lines.push_back(x); } vector<vector<int> > adj(n, vector<int>()); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (lines[j] < lines[i]) { adj[i].push_back(j); adj[j].push_back(i); } } } for (int i = 0; i < n; ++i) { queue<int> q; vector<int> dists(n, 0); q.push(i); int sum = 0; while (!q.empty()) { int item = q.front(); q.pop(); for (int j = 0; j < adj[item].size(); ++j) { if (adj[item][j] == i) { continue; } if (dists[adj[item][j]] != 0) { continue; } dists[adj[item][j]] = dists[item] + 1; sum += dists[item] + 1; q.push(adj[item][j]); } } printf("%d ", sum); } printf("\n"); return 0; } |
English