#include <cstdio> #include <queue> #include <bitset> #include <cstring> #include <cassert> #include <algorithm> #include <random> const int N = 50005; int n, a[N], top[N], dep[N], dist[N]; std::bitset<N> g[N]; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", a + i); --a[i]; } for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) if (a[i] > a[j]) g[i][j] = g[j][i] = 1; std::bitset<N> active; for (int i = 0; i < n; ++i) { active.set(); active[i] = 0; std::queue<int> q; q.push(i); for (int i = 0; i < n; ++i) dist[i] = 0; while (!q.empty()) { int v = q.front(); q.pop(); int to; while ((to = (active & g[v])._Find_first()) < N) { dist[to] = dist[v] + 1; q.push(to); active[to] = 0; } } int ans = 0; for (int i = 0; i < n; ++i) ans += dist[i]; printf("%d ", ans); } 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 | #include <cstdio> #include <queue> #include <bitset> #include <cstring> #include <cassert> #include <algorithm> #include <random> const int N = 50005; int n, a[N], top[N], dep[N], dist[N]; std::bitset<N> g[N]; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", a + i); --a[i]; } for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) if (a[i] > a[j]) g[i][j] = g[j][i] = 1; std::bitset<N> active; for (int i = 0; i < n; ++i) { active.set(); active[i] = 0; std::queue<int> q; q.push(i); for (int i = 0; i < n; ++i) dist[i] = 0; while (!q.empty()) { int v = q.front(); q.pop(); int to; while ((to = (active & g[v])._Find_first()) < N) { dist[to] = dist[v] + 1; q.push(to); active[to] = 0; } } int ans = 0; for (int i = 0; i < n; ++i) ans += dist[i]; printf("%d ", ans); } printf("\n"); return 0; } |