// #pragma GCC optimize("O3,unroll-loops") #include // #include using namespace std; #if __cplusplus >= 202002L using namespace numbers; #endif template T &ctmin(T &x){ return x; } template T &ctmin(T &x, const Head &h, const Tail &... t){ return ctmin(x = min(x, h), t...); } template T &ctmax(T &x){ return x; } template T &ctmax(T &x, const Head &h, const Tail &... t){ return ctmax(x = max(x, h), t...); } int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); int n, m; cin >> n >> m; vector> gift(m + 1, vector(n)); vector> pos(m + 1, vector(n)); iota(gift[0].begin(), gift[0].end(), 0); iota(pos[0].begin(), pos[0].end(), 0); for(auto i = 0; i < m; ++ i){ gift[i + 1] = gift[i]; int x; cin >> x, -- x; swap(gift[i + 1][x], gift[i + 1][x + 1]); for(auto j = 0; j < n; ++ j){ pos[i + 1][gift[i + 1][j]] = j; } } int obj = gift[m][0]; for(auto x = 1; x < n; ++ x){ int res = numeric_limits::max(); vector dp(n); for(auto c = 0; c < n; ++ c){ dp[c] = abs(pos[0][x] - pos[0][c]); } for(auto step = 1; step <= m; ++ step){ static vector dp_next(n); for(auto c = 0; c < n; ++ c){ dp_next[c] = dp[c]; } for(auto i = 1; i < n; ++ i){ ctmin(dp_next[gift[step][i]], dp_next[gift[step][i - 1]] + 1); } for(auto i = n - 2; i >= 0; -- i){ ctmin(dp_next[gift[step][i]], dp_next[gift[step][i + 1]] + 1); } swap(dp, dp_next); } cout << dp[obj] << " "; } cout << "\n"; return 0; } /* */