#include <algorithm> #include <cstdio> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define INT(x) int x; scanf("%d", &x) typedef long long LL; const int INF = 1000000000; int p[200000], d[4000][4000]; int main() { INT(n); REP(i,n) scanf("%d", &p[i]); REP(i,n) --p[i]; REP(i,n) d[i][i] = 0; REP(i,n) FOR(j,i+1,n) d[i][j] = d[j][i] = p[i] > p[j] ? 1 : INF; REP(k,n) REP(i,n) REP(j,n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); REP(i,n) { if (i) printf(" "); LL s = 0; REP(j,n) if (d[i][j] < INF) s += d[i][j]; printf("%lld", s); } 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 | #include <algorithm> #include <cstdio> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define INT(x) int x; scanf("%d", &x) typedef long long LL; const int INF = 1000000000; int p[200000], d[4000][4000]; int main() { INT(n); REP(i,n) scanf("%d", &p[i]); REP(i,n) --p[i]; REP(i,n) d[i][i] = 0; REP(i,n) FOR(j,i+1,n) d[i][j] = d[j][i] = p[i] > p[j] ? 1 : INF; REP(k,n) REP(i,n) REP(j,n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); REP(i,n) { if (i) printf(" "); LL s = 0; REP(j,n) if (d[i][j] < INF) s += d[i][j]; printf("%lld", s); } printf("\n"); } |