#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #ifdef LOCAL #include "./debug.cpp" #else #define debug(...) #define print_line #endif using namespace std; using ll = long long; template vector dijkstra(const vector>> &G, int start = 0) { int n = G.size(); vector dst(n, numeric_limits::max()); priority_queue> pq; dst[start] = 0; pq.push(make_pair(0, start)); while (!pq.empty()) { auto [dst_sum, now] = pq.top(); pq.pop(); dst_sum = -dst_sum; if (dst[now] < dst_sum) continue; for (auto [nxt, cost] : G[now]) { if (dst[nxt] > dst[now] + cost) { dst[nxt] = dst[now] + cost; pq.push(make_pair(-dst[nxt], nxt)); } } } return dst; } int main() { int N, M; cin >> N >> M; vector A(M); for (int i = 0; i < M; i++) { cin >> A[i]; A[i]--; } vector>> G(N * (M + 1)); vector idx(N); iota(idx.begin(), idx.end(), 0); for (int i = 0; i <= M; i++) { for (int j = 0; j < N - 1; j++) { G[i * N + j].push_back({i * N + j + 1, 1}); G[i * N + j + 1].push_back({i * N + j, 1}); } if (i == M) { break; } G[i * N + A[i]].push_back({(i + 1) * N + A[i] + 1, 0}); G[(i + 1) * N + A[i] + 1].push_back({i * N + A[i], 0}); G[i * N + A[i] + 1].push_back({(i + 1) * N + A[i], 0}); G[(i + 1) * N + A[i]].push_back({i * N + A[i] + 1, 0}); for (int j = 0; j < N; j++) { if (j == A[i] || j == A[i] + 1) { continue; } G[i * N + j].push_back({(i + 1) * N + j, 0}); G[(i + 1) * N + j].push_back({i * N + j, 0}); } } auto dst = dijkstra(G, M * N); for (int i = 1; i < N; i++) { if (i > 1) { cout << ' '; } cout << dst[i]; } cout << endl; }