#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector <int> up(n);
vector <int> pa(n);
for (int i = 0; i < n; i++) {
cin >> pa[i];
pa[i]--;
up[pa[i]] = i;
}
for (int i = 0; i < n; i++) {
vector <bool> pol(n);
pol[i] = 1;
int res = 0;
int dl = i, dp = i, gl = pa[i], gp = pa[i];
for (int j = 0; j < i; j++) {
if (pa[j] > pa[i]) {
res++;
pol[j] = 1;
dl = min(dl, j);
gp = max(gp, pa[j]);
}
}
for (int j = i + 1; j < n; j++) {
if (pa[j] < pa[i]) {
res++;
pol[j] = 1;
dp = max(dp, j);
gl = min(gl, pa[j]);
}
}
int ndl = dl, ndp = dp, ngl = gl, ngp = gp;
// cout << dl << " " << dp << " " << gl << " " << gp << "\n";
for (int j = dl + 1; j < dp; j++) {
if (!pol[j]) {
res += 2;
pol[j] = 1;
ngp = max(ngp, pa[j]);
ngl = min(ngl, pa[j]);
}
}
for (int j = gl + 1; j < gp; j++) {
if (!pol[up[j]]) {
res += 2;
pol[up[j]] = 1;
ndl = min(ndl, up[j]);
ndp = max(ndp, up[j]);
}
}
int ile = 3;
while (dl != ndl || dp != ndp || gl != ngl || gp != ngp) {
int nndl = ndl, nndp = ndp, nngl = ngl, nngp = ngp;
for (int j = ndl + 1; j < dl; j++) {
if (!pol[j]) {
res += ile;
pol[j] = 1;
nngl = min(nngl, pa[j]);
nngp = max(nngp, pa[j]);
}
}
for (int j = dp + 1; j < ndp; j++) {
if (!pol[j]) {
res += ile;
pol[j] = 1;
nngl = min(nngl, pa[j]);
nngp = max(nngp, pa[j]);
}
}
for (int j = ngl + 1; j < gl; j++) {
if (!pol[up[j]]) {
res += ile;
pol[up[j]] = 1;
nndl = min(nndl, up[j]);
nndp = max(nndp, up[j]);
}
}
for (int j = gp + 1; j < ngp; j++) {
if (!pol[up[j]]) {
res += ile;
pol[up[j]] = 1;
nndl = min(nndl, up[j]);
nndp = max(nndp, up[j]);
}
}
dp = ndp, dl = ndl, gp = ngp, gl = ngl;
ndp = nndp, ndl = nndl, ngp = nngp, ngl = nngl;
ile++;
}
cout << res << " ";
}
}
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 96 97 98 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector <int> up(n); vector <int> pa(n); for (int i = 0; i < n; i++) { cin >> pa[i]; pa[i]--; up[pa[i]] = i; } for (int i = 0; i < n; i++) { vector <bool> pol(n); pol[i] = 1; int res = 0; int dl = i, dp = i, gl = pa[i], gp = pa[i]; for (int j = 0; j < i; j++) { if (pa[j] > pa[i]) { res++; pol[j] = 1; dl = min(dl, j); gp = max(gp, pa[j]); } } for (int j = i + 1; j < n; j++) { if (pa[j] < pa[i]) { res++; pol[j] = 1; dp = max(dp, j); gl = min(gl, pa[j]); } } int ndl = dl, ndp = dp, ngl = gl, ngp = gp; // cout << dl << " " << dp << " " << gl << " " << gp << "\n"; for (int j = dl + 1; j < dp; j++) { if (!pol[j]) { res += 2; pol[j] = 1; ngp = max(ngp, pa[j]); ngl = min(ngl, pa[j]); } } for (int j = gl + 1; j < gp; j++) { if (!pol[up[j]]) { res += 2; pol[up[j]] = 1; ndl = min(ndl, up[j]); ndp = max(ndp, up[j]); } } int ile = 3; while (dl != ndl || dp != ndp || gl != ngl || gp != ngp) { int nndl = ndl, nndp = ndp, nngl = ngl, nngp = ngp; for (int j = ndl + 1; j < dl; j++) { if (!pol[j]) { res += ile; pol[j] = 1; nngl = min(nngl, pa[j]); nngp = max(nngp, pa[j]); } } for (int j = dp + 1; j < ndp; j++) { if (!pol[j]) { res += ile; pol[j] = 1; nngl = min(nngl, pa[j]); nngp = max(nngp, pa[j]); } } for (int j = ngl + 1; j < gl; j++) { if (!pol[up[j]]) { res += ile; pol[up[j]] = 1; nndl = min(nndl, up[j]); nndp = max(nndp, up[j]); } } for (int j = gp + 1; j < ngp; j++) { if (!pol[up[j]]) { res += ile; pol[up[j]] = 1; nndl = min(nndl, up[j]); nndp = max(nndp, up[j]); } } dp = ndp, dl = ndl, gp = ngp, gl = ngl; ndp = nndp, ndl = nndl, ngp = nngp, ngl = nngl; ile++; } cout << res << " "; } } |
English