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
| #include <iostream> #include <algorithm> #include <cstdio> using namespace std; typedef long long ll; #define wfor(i,j,k) for(i=j;i<k;++i) #define mfor(i,j,k) for(i=j;i>=k;--i)
const ll INF = 0x7f7f7f7f; struct st { ll id; ll num; }; struct rule { bool operator ()(const st &x, const st &y) { return x.num > y.num; } }; st station[5005]; ll minnum[5005]; int main() { std::ios::sync_with_stdio(false); #ifdef test freopen("F:\\Desktop\\question\\in.txt", "r", stdin); #endif #ifdef ubuntu freopen("/home/time/debug/debug/in", "r", stdin); freopen("/home/time/debug/debug/out", "w", stdout); #endif ll n, m; cin >> n >> m; ll i; fill(minnum, minnum + n + 1, INF); wfor(i, 0, m) { ll a, cad; cin >> a >> cad; station[a].num++; station[a].id = a; minnum[a] = min(minnum[a], cad - a >= 0 ? cad - a : n + cad - a); } sort(station, station + n + 1, rule()); ll j; wfor(i, 1, n + 1) { ll ans = 0; ll num = station[0].num; ll lastst = station[0].id; ll lastcha = 0; ll maxnum = station[0].num; wfor(j, 0, n) { if (station[j].num == maxnum || (station[j].num == maxnum - 1 && station[j].num > 0)) { ll cha = station[j].id - i >= 0 ? station[j].id - i : n + station[j].id - i; cha += minnum[station[j].id]; if (station[j].num == station[0].num) cha += n; if (cha > lastcha) { lastcha = cha; lastst = station[j].id; num = station[j].num; } } else break; } lastcha = lastst - i >= 0 ? lastst - i : n + lastst - i; ll fix = minnum[lastst]; ans = (num - 1) * n + fix + lastcha; cout << ans << " "; } return 0; }
|