text
stringlengths
49
983k
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; int n, m, first, second; vector<int> v[5005]; int main() { ios_base ::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (; m; m--) { cin >> first >> second; v[first].push_back(second); } for (int i = 1; i <= n; i++) { int ans = 0; for (int j = 1; j <= n; j++) { int mn = n + 5; if (v[j].empty()) continue; for (auto &k : v[j]) { if (k < j) mn = min(mn, n - j + k); else mn = min(mn, k - j); } ans = max(ans, mn + (j >= i ? j - i : n - i + j) + n * ((int)v[j].size() - 1)); } cout << ans << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> istream &operator>>(istream &is, const pair<A, B> &p) { return is >> p.first >> p.second; } template <typename T_container, typename T = typename enable_if< !is_same<T_container, string>::value, typename T_container::value_type>::type> istream &operator>>(istream &is, T_container &v) { for (T &x : v) is >> x; return is; } bool debug; template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return debug ? os << '(' << p.first << ", " << p.second << ')' : os << p.first << " " << p.second; } template <typename T_container, typename T = typename enable_if< !is_same<T_container, string>::value, typename T_container::value_type>::type> ostream &operator<<(ostream &os, const T_container &v) { if (debug) { os << "{"; string sep; for (const T &x : v) os << sep << x, sep = ", "; os << '}'; } else { bool f = false; for (const T &x : v) { if (f) { os << " "; } os << x, f = true; } } return os; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } const string YES = "Yes"; const string NO = "No"; void solve() { long long int n, m; cin >> n >> m; long long int same = m; map<long long int, vector<long long int>> store1; while (m--) { long long int a, b; cin >> a >> b; store1[a].push_back(b); } long long int cycles = 0; auto get = [&](int a, int b) { return (b - a) + (b < a ? n : 0); }; for (auto &p : store1) { sort(p.second.begin(), p.second.end(), [&](const int &i, const int &j) { int di = get(p.first, i); int dj = get(p.first, j); return di > dj; }); cycles = max(cycles, (long long)p.second.size()); } for (int st = 1; st <= n; st++) { long long int max_d_in_last_cycle = 0; for (auto &p : store1) { if (int(p.second.size()) == cycles) { max_d_in_last_cycle = max(max_d_in_last_cycle, get(st, p.first) + get(p.first, p.second.back())); } if (cycles >= 2 && int(p.second.size()) >= cycles - 1) { if (get(st, p.second[cycles - 2]) < get(p.first, p.second[cycles - 2])) max_d_in_last_cycle = max(max_d_in_last_cycle, get(st, p.second[cycles - 2])); } } int ans = (cycles - 1) * n + max_d_in_last_cycle; cout << ans << " \n"[st == n]; } } int main() { int t = 1; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> a[n + 1]; while (m--) { int t1, t2; scanf("%d %d", &t1, &t2); a[t1].push_back(t2); } vector<int> d(n + 1); for (int i = 0; i < n + 1; i++) { if (a[i].size()) { int m1 = INT_MAX; for (int j = 0; j < a[i].size(); j++) { int t = a[i][j] - i; if (t < 0) { t += n; } m1 = min(t, m1); } d[i] = (a[i].size() - 1) * n + m1; } } for (int i = 0; i < n; i++) { int m2 = 0; for (int j = 0; j < n; j++) { int t = (j + i) % n + 1; if (d[t]) { m2 = max(m2, d[t] + j); } } cout << m2 << " "; } return 0; }
#include <bits/stdc++.h> int main() { std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0); int n, m; std::cin >> n >> m; std::vector<int> a(m), b(m); for (int i = 0; i < m; i++) { std::cin >> a[i] >> b[i]; a[i]--, b[i]--; } std::vector<std::vector<int>> to(n); for (int i = 0; i < m; i++) { to[a[i]].push_back(b[i]); } std::vector<int> num(n), end(n); for (int i = 0; i < n; i++) { num[i] = int(to[i].size()); if (num[i] == 0) { end[i] = 0; } else { end[i] = n; for (int nxt : to[i]) { if (nxt > i) { end[i] = std::min(end[i], nxt - i); } else { end[i] = std::min(end[i], nxt + n - i); } } } } for (int i = 0; i < n; i++) { int ans = 0; for (int j = 0; j < n; j++) { if (!num[j]) continue; int cur = n * (num[j] - 1); if (i <= j) { cur += j - i; } else { cur += n + j - i; } cur += end[j]; ans = std::max(ans, cur); } std::cout << ans << ' '; } std::cout << '\n'; }
#include <bits/stdc++.h> using namespace std; int cnt[5001], mini[5001]; int main() { int n, m; scanf("%d %d", &n, &m); vector<pair<int, int> > v; for (int i = 1; i <= m; i++) { int a, b; scanf("%d %d", &a, &b); a--; b--; cnt[a]++; if (mini[a] == 0) mini[a] = a > b ? n - a + b : b - a; else mini[a] = min(mini[a], a > b ? n - a + b : b - a); v.push_back(pair<int, int>(a, b)); } sort(v.begin(), v.end()); for (int i = 0; i < n; i++) { int now = 0; if (cnt[i]) now = (cnt[i] - 1) * n + mini[i]; for (int j = i + 1, c = 1;; j++, c++) { j %= n; if (j == i) break; if (cnt[j] > 0) now = max(now, (cnt[j] - 1) * n + mini[j] + c); } printf("%d ", now); } return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n, m; cin >> n >> m; vector<int> adj[n + 1]; int mx = 0; for (int i = 0; i < m; ++i) { int start, end; cin >> start >> end; adj[start].push_back(end); } for (int i = 1; i <= n; ++i) { sort(adj[i].begin(), adj[i].end()); } vector<int> short_dis(n + 1); for (int x = 1; x <= n; ++x) { auto it = lower_bound(adj[x].begin(), adj[x].end(), x); if (it != adj[x].end()) { short_dis[x] = (*it) - x; if (short_dis[x] < 0) short_dis[x] += n; } else { it = lower_bound(adj[x].begin(), adj[x].end(), 1); if (it != adj[x].end()) { short_dis[x] = (*it) - x; if (short_dis[x] < 0) short_dis[x] += n; } } } for (int i = 1; i <= n; ++i) { int ans = 0; for (int j = 1; j <= n; ++j) { if (short_dis[j] == 0) continue; int dis = j - i; if (dis < 0) dis += n; ans = max(ans, ((int)adj[j].size() - 1) * n + short_dis[j] + dis); } cout << ans << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; int dist(int a, int b, int n) { if (a <= b) return b - a; return b + n - a; } int main() { int n, m, i, a, b; cin >> n; cin >> m; int mini[n], nume[n]; for (i = 0; i < n; i++) { mini[i] = n + 3; nume[i] = 0; } for (i = 0; i < m; i++) { cin >> a; cin >> b; a--; b--; nume[a]++; if (dist(a, b, n) < mini[a]) mini[a] = dist(a, b, n); } for (i = 0; i < n; i++) { a = 0; for (b = 0; b < n; b++) { if ((n * (nume[b] - 1) + mini[b] + dist(i, b, n) > a) && (nume[b] != 0)) { a = n * (nume[b] - 1) + mini[b] + dist(i, b, n); } } cout << a << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007LL; long long large = 2000000000000000000LL; int main() { vector<vector<int> > adj; int n, m; cin >> n >> m; adj.assign(n, vector<int>()); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; b = (b - a + n) % n; adj[a].push_back(b); } for (int i = 0; i < n; i++) sort(adj[i].begin(), adj[i].end()); long long ans = 0; vector<int> len(n, -1); for (int i = 0; i < n; i++) { if (adj[i].size()) len[i] = ((int)adj[i].size() - 1) * n + adj[i][0]; } for (int i = 0; i < n; i++) { if (len[i] != -1) ans = max(ans, (len[i] + i) * 1LL); } for (int i = 0; i < n; i++) { if (i) { ans--; if (len[i - 1] != -1) ans = max(ans, 1LL * (len[i - 1] + n - 1)); } cout << ans << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; long long MOD = 10e9 + 7; void solve() { int n, m; cin >> n >> m; vector<int> a(m), b(m); vector<int> mn(n, INT_MAX), sz(n, 0); for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; int dist = (b[i] - a[i] + n) % n; if (mn[a[i] - 1] == INT_MAX) { mn[a[i] - 1] = dist; sz[a[i] - 1]--; } else if (mn[a[i] - 1] > dist) { mn[a[i] - 1] = dist; } sz[a[i] - 1]++; } for (int i = 0; i < n; i++) { int mx = -1; for (int j = 0; j < n; j++) { int ind = (i + j) % n; int time = mn[ind] == INT_MAX ? 0 : sz[ind] * n + mn[ind] + j; if (time > mx) mx = time; } cout << mx << " "; } } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int n, m; int circleDist(int i, int j) { if (i <= j) return j - i; else return j - i + n; } int main() { scanf("%d %d", &n, &m); vector<vector<int>> candies(n); for (int i = 0; i < m; ++i) { int a, b; scanf("%d %d", &a, &b); --a; --b; candies[a].push_back(b); } for (int i = 0; i < n; ++i) { sort(candies[i].begin(), candies[i].end(), [&i](int x, int y) { return circleDist(i, x) < circleDist(i, y); }); } vector<ll> worstDist(n); for (int i = 0; i < n; ++i) { if (candies[i].empty()) continue; else worstDist[i] = (ll(candies[i].size()) - 1) * n + circleDist(i, *candies[i].begin()); } for (int i = 0; i < n; ++i) { ll ans = 0; for (int j = 0; j < n; ++j) { if (worstDist[j] > 0) ans = max(ans, circleDist(i, j) + worstDist[j]); } printf("%lld ", ans); } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; int dist(int a, int b) { if (a < b) return b - a; return n - a + b; } int main() { cin >> n >> m; vector<pair<int, int>> table(n + 1); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; table[a].first++; if (table[a].second == 0) table[a].second = INT_MAX; table[a].second = min(table[a].second, dist(a, b)); } int ans; for (int i = 1; i <= n; i++) { ans = 0; for (int j = 0; j < n; j++) ans = max(ans, (table[(i + j - 1) % n + 1].first - 1) * n + table[(i + j - 1) % n + 1].second + j); cout << ans << ' '; } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> bool ckmn(T &a, T b) { T c = a; a = min(a, b); return a != c; } template <typename T> bool ckmx(T &a, T b) { T c = a; a = max(a, b); return a != c; } const int N = 100050; const int inf = 1e9 + 7; int a[N], b[N], mn[N], cnt[N]; long long ans[N]; int main() { int n, m, i, j; scanf("%i %i", &n, &m); for (i = 1; i <= n; i++) mn[i] = inf; for (i = 1; i <= m; i++) { scanf("%i %i", &a[i], &b[i]); cnt[a[i]]++; mn[a[i]] = min(mn[a[i]], a[i] > b[i] ? b[i] + n : b[i]); } for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (cnt[j] == 0) continue; int dis = 0; if (j < i) dis = n - (i - j); else dis = j - i; ans[i] = max(ans[i], (long long)(cnt[j] - 1) * n + dis + mn[j] - j); } } for (i = 1; i <= n; i++) printf("%lld ", ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; int gn(int id) { return (id + 1) % n; } const int MAXN = 5100; int lst[MAXN]; int sed[MAXN]; vector<int> v[MAXN]; int dis(int st, int ed) { if (ed < st) ed += n; return ed - st; } int go(int st, int step) { return (st + step) % n; } int cnt[5100] = {0}; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int st, ed; scanf("%d%d", &st, &ed); st--, ed--; v[st].push_back(dis(st, ed)); } int maxx = 0; for (int i = 0; i < n; i++) { sort(v[i].begin(), v[i].end()); maxx = max(maxx, (int)v[i].size()); } for (int i = 0; i < n; i++) { long long ans = 0; for (int j = 0; j < n; j++) { long long now = 0; if (v[j].size() == 0) now = 0; else now = dis(i, j) + (v[j].size() - 1) * n * 1LL + v[j][0]; ans = max(ans, now); } if (i) printf(" "); printf("%I64d", ans); } }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; vector<int> v[maxn]; int T[maxn]; int ans[maxn]; int main() { ios::sync_with_stdio(false); int N; int M; cin >> N >> M; for (int i = 1; i <= M; i++) { int a; int b; cin >> a >> b; a -= 1; b -= 1; v[a].push_back(b); } for (int i = 0; i < N; i++) { if (v[i].size() == 0) continue; int t = v[i].size() - 1; T[i] = t * N; int pmin = 1e9; for (int j = 0; j < v[i].size(); j++) { int to = v[i][j]; if (to > i) pmin = min(pmin, to - i); else pmin = min(pmin, N - (i - to)); } T[i] += pmin; } for (int i = 0; i < N; i++) { int t = T[i]; for (int j = (i + 1) % N, num = 1; num <= N - 1; num++, j = (j + 1) % N) { if (T[j] == 0) continue; t = max(t, num + T[j]); } ans[i] = t; } for (int i = 0; i < N; i++) cout << ans[i] << ' '; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> struct rge { T b, e; }; template <class T> rge<T> range(T i, T j) { return rge<T>{i, j}; } struct debug { template <class T> debug& operator<<(const T&) { return *this; } }; using ll = long long; template <typename T> void min_self(T& a, T b) { a = min(a, b); } template <typename T> void max_self(T& a, T b) { a = max(a, b); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<vector<int>> g(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; --a, --b; g[a].push_back(b); } auto Dist = [&](int x, int y) { int d = y - x; if (d < 0) { d += n; } return d; }; for (int i = 0; i < n; i++) { sort(g[i].begin(), g[i].end(), [&](int x, int y) { return Dist(i, x) < Dist(i, y); }); } for (int i = 0; i < n; i++) { int ans = 0; for (int j = 0; j < n; j++) { if (g[j].empty()) { continue; } int cur = Dist(i, j); cur += n * ((int)g[j].size() - 1); cur += Dist(j, g[j][0]); max_self(ans, cur); } cout << ans << " "; } cout << "\n"; }
#include <bits/stdc++.h> using namespace std; template <typename T, typename S> ostream &operator<<(ostream &os, const pair<T, S> &p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type * = nullptr> ostream &operator<<(ostream &os, const C &c) { bool f = true; os << "["; for (const auto &x : c) { if (!f) os << ", "; f = false; os << x; } return os << "]"; } template <typename T> void debug(string s, T x) { cerr << s << " = " << x << "\n"; } template <typename T, typename... Args> void debug(string s, T x, Args... args) { for (int i = 0, b = 0; i < (int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) { cerr << s.substr(0, i) << " = " << x << " | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<int> cnt(n), mn(n, INT_MAX); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; cnt[a]++; mn[a] = min(mn[a], (b - a + n) % n); } for (int i = 0; i < n; i++) { int ret = 0; for (int j = 0; j < n; j++) { int p = (i + j) % n; if (cnt[p] > 0) ret = max(ret, j + n * (cnt[p] - 1) + mn[p]); } cout << ret << " "; } cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m; long long dist(long long a, long long b) { long long dist = (b - a); if (dist < 0) dist += n; return dist; } int main() { ios_base::sync_with_stdio(false); cin >> n >> m; vector<long long> c(n, 0); vector<long long> closest(n, n + 1); long long MAX_C = -1; for (int i = 0; i < m; ++i) { long long a, b; cin >> a >> b; --a, --b; ++c[a]; MAX_C = max(MAX_C, c[a]); closest[a] = min(dist(a, b), closest[a]); } for (long long start = 0; start < n; ++start) { long long ans = 0; for (long long i = 0; i < n; ++i) { long long curr = (start + i) % n; if (c[curr] > 0) { ans = max(ans, i + (c[curr] - 1) * n + closest[curr]); } } cout << (ans) << ' '; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; pair<int, int> A[20005]; vector<int> v[5005]; int atleast[5005], r[5005], vis[5005], mindist[5005]; int main() { scanf("%d %d", &n, &m); for (int i = (0); i < (m); i++) { int x, y; scanf("%d %d", &x, &y); v[x].push_back(y); } for (int i = (1); i <= (n); i++) { for (int j = (1); j <= (n); j++) mindist[j] = r[j] = vis[j] = 0; for (int cur = (1); cur <= (n); cur++) { int j = i + cur - 1; if (j > n) j -= n; vis[j] = 1; r[j] = v[j].size(); for (int k : v[j]) { int dist; if (vis[k] == 0) { if (k > j) dist = k - j; else dist = k + n - j; } else { if (k > j) dist = k - j; else dist = k + n - j; } if (mindist[j] == 0) mindist[j] = dist; else mindist[j] = min(mindist[j], dist); } } long long res = 0; int t = 0; for (int cur = (1); cur <= (n); cur++) { int j = i + cur - 1; if (j > n) j -= n; long long tmp = r[j]; if (tmp == 0) { t++; continue; } tmp = 1ll * (tmp - 1) * n + t + mindist[j]; res = max(res, tmp); t++; } printf("%lld ", res); } puts(""); return 0; }
#include <bits/stdc++.h> using namespace std; struct data { int mmin, times; } da[5010]; int n, m; int idx[5010], now; bool cmp(int a, int b) { if (da[a].times == 0) return 0; if (da[b].times == 0) return 1; return (da[a].times - da[b].times) * n + (a + n - now) % n + da[a].mmin > (b + n - now) % n + da[b].mmin; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) da[i].mmin = 10000, da[i].times = 0, idx[i] = i; while (m--) { int a, b; cin >> a >> b; da[a].times++; da[a].mmin = min(da[a].mmin, (b + n - a) % n); } now = 1; int id = n, mmax = 0; sort(idx + 1, idx + 1 + n, cmp); for (int i = 1; i <= n; i++) { mmax = max(mmax, da[idx[i]].times); if (da[idx[i]].times - mmax < -1 || da[idx[i]].times == 0) { id = i; break; } } id--; for (int i = 1; i <= n; i++) { now = i; sort(idx + 1, idx + 1 + id, cmp); cout << (da[idx[1]].times - 1) * n + (idx[1] + n - now) % n + da[idx[1]].mmin << " "; } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/stack:247474112") #pragma GCC optimize("Ofast") using namespace std; const int INF = 0x3f3f3f3f; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<int> D[n]; vector<int> cnt(n, 0); for (long long i = 0; i < m; ++i) { int x, y; cin >> x >> y; ++cnt[x - 1]; D[x - 1].push_back(y - 1); } for (long long i = 0; i < n; ++i) { int maxd = 0; for (long long x = 0; x < n; ++x) { if (D[x].size() == 0) { continue; } int mind = INF; for (auto y : D[x]) { int t = (y >= x ? y - x : y - x + n); mind = min(t, mind); } int tmp = x - i; if (tmp < 0) { tmp += n; } tmp += (cnt[x] - 1) * n; tmp += mind; maxd = max(maxd, tmp); } cout << maxd << " "; } cout << endl; }
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007LL; long long large = 2000000000000000000LL; int main() { int n, m; cin >> n >> m; vector<int> a(m, 0), b(m, 0); for (int i = 0; i < m; i++) { scanf("%d%d", &a[i], &b[i]); a[i]--; b[i]--; } for (int i = 0; i < n; i++) { vector<int> cnt(n, 0); int maxi = 0; int ans = 0; for (int j = 0; j < m; j++) { int st = a[j]; int en = b[j]; st -= i; en -= i; st += n; st %= n; en += n; en %= n; if (en < st) en += n; cnt[st]++; maxi = max(maxi, cnt[st]); } vector<int> pos(n, 2 * n); for (int j = 0; j < m; j++) { int st = a[j]; int en = b[j]; st -= i; en -= i; st += n; st %= n; en += n; en %= n; if (en < st) en += n; if (cnt[st] == maxi) { pos[st] = min(pos[st], en); } if (cnt[st] == maxi - 1) { pos[st] = min(pos[st], en - n); } } for (int i = 0; i < n; i++) { if (pos[i] < 2 * n) ans = max(ans, pos[i]); } cout << n * (maxi - 1) + ans << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; bool comparator(pair<int, int> a, pair<int, int> b) { if (a.first == b.first) { int distA = (a.second - a.first + n) % n; int distB = (b.second - b.first + n) % n; return distA > distB; } return a.first < b.first; } int main() { cin >> n >> m; vector<pair<int, int> > a(m); for (int i = 0; i < m; i++) { cin >> a[i].first; cin >> a[i].second; } sort(a.begin(), a.end(), comparator); vector<int> work(n + 1, 0); int start = 1; int j = 1; int prev = 1; for (int i = 0; i < m; i++) { if (a[i].first != j) { j = a[i].first; prev = j; } work[j] += (a[i].first - prev + n) % n; work[j] += (a[i].second - a[i].first + n) % n; prev = a[i].second; } for (int i = 1; i < n + 1; i++) { int ans = 0; for (int j = 1; j < n + 1; j++) { if (work[j] > 0) ans = max(ans, work[j] + ((j - i + n) % n)); } cout << ans << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; const int INFL = (int)1e9; const long long int INFLL = (long long int)1e18; const double INFD = numeric_limits<double>::infinity(); const double PI = 3.14159265358979323846; bool nearlyeq(double x, double y) { return abs(x - y) < 1e-9; } bool inrange(int x, int t) { return x >= 0 && x < t; } long long int rndf(double x) { return (long long int)(x + (x >= 0 ? 0.5 : -0.5)); } long long int floorsqrt(long long int x) { long long int m = (long long int)sqrt((double)x); return m + (m * m <= x ? 0 : -1); } long long int ceilsqrt(long long int x) { long long int m = (long long int)sqrt((double)x); return m + (x <= m * m ? 0 : 1); } long long int rnddiv(long long int a, long long int b) { return (a / b + (a % b * 2 >= b ? 1 : 0)); } long long int ceildiv(long long int a, long long int b) { return (a / b + (a % b == 0 ? 0 : 1)); } long long int gcd(long long int m, long long int n) { if (n == 0) return m; else return gcd(n, m % n); } long long int lcm(long long int m, long long int n) { return m * n / gcd(m, n); } class Max_Queue { private: stack<pair<long long int, long long int> > stk0, stk1; void rebuffer() { while (stk1.size()) { long long int x = stk1.top().first; long long int y = stk0.size() ? stk0.top().second : LLONG_MIN; stk0.push({x, max(x, y)}); stk1.pop(); } } public: void push(long long int x) { long long int y = stk1.size() ? stk1.top().second : LLONG_MIN; stk1.push({x, max(x, y)}); } long long int front() { if (stk0.empty()) rebuffer(); return stk0.top().first; } void pop() { if (stk0.empty()) rebuffer(); stk0.pop(); } size_t size() { return stk0.size() + stk1.size(); } void clear() { while (stk0.size()) stk0.pop(); while (stk1.size()) stk1.pop(); } long long int get_max() { long long int x = LLONG_MIN, y = LLONG_MIN; if (stk0.size()) x = stk0.top().second; if (stk1.size()) y = stk1.top().second; return max(x, y); } }; long long int dist(long long int s, long long int t, long long int n) { if (s <= t) return t - s; else return t + n - s; } int main() { int n, m; cin >> n >> m; vector<long long int> a(n, LLONG_MAX); vector<long long int> cnt(n, 0); for (int i = 0; i < (int)(m); i++) { long long int s, t; cin >> s >> t; s--; t--; a[s] = min(a[s], dist(s, t, n)); cnt[s]++; } vector<long long int> ans(n, 0); Max_Queue mq; for (int j = 0; j < (int)(n); j++) { long long int buf = -n; if (cnt[j] > 0) buf = dist(0, j, n) + (cnt[j] - 1) * n + a[j]; mq.push(buf); } for (int i = 0; i < (int)(n); i++) { ans[i] = mq.get_max() - i; long long int x = mq.front(); mq.pop(); mq.push(x + n); } for (int extra_i = 0; extra_i < (int)(ans.size()); extra_i++) { cout << ans[extra_i] << " "; } cout << endl; ; }
#include <bits/stdc++.h> using namespace std; int n, m; int jl(int x, int y) { if (x <= y) return y - x; return y + n - x; } int CNT[5005], ZJ[5005]; int ans; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) ZJ[i] = n; for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); CNT[a]++; ZJ[a] = min(ZJ[a], jl(a, b)); } for (int i = 1; i <= n; i++) { ans = 0; if (CNT[i]) ans = max(ans, (CNT[i] - 1) * n + ZJ[i]); for (int j = 1; j <= n; j++) { if ((!CNT[j]) || (i == j)) continue; ans = max(ans, jl(i, j) + (CNT[j] - 1) * n + ZJ[j]); } printf("%d ", ans); } }
#include <bits/stdc++.h> using namespace std; const int SIZE = 1e5 + 10; const int INF = 0x3f3f3f3f; const double eps = 1e-5; const double PI = atan(1.0) * 4.0; const long long mod = 1e9 + 7; int T = 1, n, m, x, y; int dis[5005], v[5005]; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); ; while (T--) { cin >> n >> m; memset(dis, INF, sizeof(dis)); for (int i = 1; i <= m; i++) { cin >> x >> y; dis[x] = min(dis[x], (y - x + n) % n); v[x]++; } for (int i = 1; i <= n; i++) { int ans = 0; for (int j = 1; j <= n; j++) { if (!v[j]) continue; ans = max(ans, (j - i + n) % n + (v[j] - 1) * n + dis[j]); } printf("%d ", ans); } } return 0; }
#include <bits/stdc++.h> using namespace std; int Freq[5005], Closest[5005]; int Distance(int N, int u, int v) { if (v > u) return (v - u); else return (N - (u - v)); } int main() { int N, m, u, v, i, j; scanf("%d%d", &N, &m); fill(Closest + 1, Closest + N + 1, N + 1); fill(Freq + 1, Freq + N + 1, 0); for (i = 1; i <= m; ++i) { scanf("%d%d", &u, &v); ++Freq[u]; Closest[u] = min(Closest[u], Distance(N, u, v)); } int Ans; for (i = 1; i <= N; ++i) { Ans = 0; for (j = 1; j <= N; ++j) { if (Freq[j] == 0) continue; if (j < i) Ans = max(Ans, (Freq[j] - 1) * N + N - (i - j) + Closest[j]); else Ans = max(Ans, (Freq[j] - 1) * N + j - i + Closest[j]); } printf("%d ", Ans); } puts(""); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; int a[20010]; int b[20010]; int cc[5010]; int ttl[5010]; int ans[5010]; int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; } for (int i = 1; i <= n; i++) { ttl[i] = n; } for (int i = 0; i < m; i++) { cc[a[i]]++; int nt; if (b[i] > a[i]) { nt = b[i] - a[i]; } else { nt = n - a[i] + b[i]; } ttl[a[i]] = min(ttl[a[i]], nt); } for (int i = 1; i <= n; i++) { if (cc[i] == 0) { ttl[i] = 0; } else { ttl[i] += (cc[i] - 1) * n; } } multiset<int> s; for (int i = 1; i <= n; i++) { if (ttl[i] > 0) { s.insert(ttl[i] + i - 1); } } ans[0] = *(--s.end()); for (int i = 1; i < n; i++) { if (ttl[i] > 0) { s.erase(s.find(ttl[i] + i - 1)); s.insert(ttl[i] + i + n - 1); } ans[i] = *(--s.end()) - i; } for (int i = 0; i < n; i++) { cout << ans[i] << " "; } cout << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 4; template <typename T, typename... V> inline void wonl(T t, V... v) { ww(t); if (sizeof...(v)) putchar(' '); wonl(v...); } void solve() { int n, m; cin >> n >> m; vector<int> graph[n + 1]; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; if (b < a) b = b - a + n; else b -= a; graph[a].push_back(b); } for (int i = 0; i <= n; i++) { sort(graph[i].begin(), graph[i].end()); } vector<int> minans(n); vector<int> ans(n); for (int i = 0; i < n; i++) { if (graph[i + 1].size()) { minans[i] = (graph[i + 1].size() - 1) * n + graph[i + 1][0]; } else minans[i] = 0; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (minans[(i + j) % n] > 0) ans[i] = max(ans[i], j + minans[(i + j) % n]); } } for (int i = 0; i < n; i++) cout << ans[i] << " "; cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int FFTMOD = 119 << 23 | 1; const int INF = (int)1e9 + 23111992; const long long LINF = (long long)1e18 + 23111992; const long double PI = acos((long double)-1); const long double EPS = 1e-9; inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } inline long long fpow(long long n, long long k, int p = MOD) { long long r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } template <class T> inline int chkmin(T& a, const T& val) { return val < a ? a = val, 1 : 0; } template <class T> inline int chkmax(T& a, const T& val) { return a < val ? a = val, 1 : 0; } inline unsigned long long isqrt(unsigned long long k) { unsigned long long r = sqrt(k) + 1; while (r * r > k) r--; return r; } inline long long icbrt(long long k) { long long r = cbrt(k) + 1; while (r * r * r > k) r--; return r; } inline void addmod(int& a, int val, int p = MOD) { if ((a = (a + val)) >= p) a -= p; } inline void submod(int& a, int val, int p = MOD) { if ((a = (a - val)) < 0) a += p; } inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; } inline int sign(long double x, long double y) { return sign(x - y); } mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count()); inline int myrand() { return abs((int)mt()); } const int maxn = 1e5 + 5; int n, m; int a[maxn]; int b[maxn]; int f[maxn]; int g[maxn]; int ff(int u, int v) { if (u > v) v += n; return v - u; } void chemthan() { cin >> n >> m; fill_n(g, maxn, INF); for (int i = (0); i < (m); ++i) { cin >> a[i] >> b[i], a[i]--, b[i]--; if (a[i] > b[i]) b[i] += n; f[a[i]]++; chkmin(g[a[i]], b[i] - a[i]); } for (int i = (0); i < (n); ++i) { long long res = 0; for (int k = (0); k < (n); ++k) if (f[k]) { chkmax(res, ff(i, k) + (long long)n * (f[k] - 1) + g[k]); } cout << res << " \n"[i == n - 1]; } } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(0), cin.tie(0); if (argc > 1) { assert(freopen(argv[1], "r", stdin)); } if (argc > 2) { assert(freopen(argv[2], "wb", stdout)); } chemthan(); cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
#include <bits/stdc++.h> using std::bitset; using std::cerr; using std::cin; using std::cout; using std::deque; using std::endl; using std::fixed; using std::ios_base; using std::make_pair; using std::make_tuple; using std::map; using std::nth_element; using std::pair; using std::queue; using std::reverse; using std::set; using std::sort; using std::stack; using std::string; using std::tuple; using std::unordered_map; using std::unordered_set; using std::vector; const int MAXN = 5005; const int INF = 1e9; const int MOD = 1e9 + 7; const int64_t L_INF = 4e18; int n, m; vector<int> g[MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; g[a].push_back(a < b ? b - a : n - (a - b)); } for (int i = 0; i < n; i++) { sort(g[i].rbegin(), g[i].rend()); } for (int i = 0; i < n; i++) { int ans = 0; for (int j = 0; j < n; j++) { int p = (i + j) % n; if (!g[p].empty()) { ans = std::max(ans, n * (int(g[p].size()) - 1) + j + g[p].back()); } } cout << ans << ' '; } cout << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; ifstream in; ofstream out; const long long kk = 1000; const long long ml = kk * kk; const long long mod = ml * kk + 7; const long long inf = ml * ml * ml + 7; long long n, i, j, mx, k; vector<pair<long long, long long>> m; vector<long long> v, v2, add; vector<vector<long long>> have; bool viv = false; long long dist(long long a, long long b) { long long res = 0; res += b; res -= a; if (res < 0) res += n; return res; } void counter(long long st) { long long res = n; for (auto st2 : have[st]) { long long nres = dist(st, st2); res = min(res, nres); } add[st] = res; } long long solve(long long s) { long long ans = n * (mx - 1); long long addans = 0; for (auto st : v) { addans = max(addans, dist(s, st) + add[st]); } for (auto st : v2) { addans = max(addans, dist(s, st) + add[st] - n); } ans += addans; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; have.resize(n); for (i = 0; i < k; i++) { long long a, b; cin >> a >> b; a--, b--; m.push_back({a, b}); have[a].push_back(b); } mx = 0; for (long long i = 0; i < n; i++) mx = max(mx, (long long)have[i].size()); for (long long i = 0; i < n; i++) if (have[i].size() == mx) v.push_back(i); for (long long i = 0; i < n; i++) if (have[i].size() + 1 == mx && have[i].size()) v2.push_back(i); add.resize(n, -1); for (auto s : v) counter(s); for (auto s : v2) counter(s); vector<long long> ans; for (long long i = 0; i < n; i++) ans.push_back(solve(i)); for (auto i : ans) cout << i << ' '; return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long n, m; cin >> n >> m; vector<vector<long long>> v(n); for (long long i = 0, x, y; i < m; i++) { cin >> x >> y; v[x - 1].push_back(y - 1); } for (long long i = 0; i < n; i++) { long long ans = 0; vector<long long> pos(5005, 0); for (long long j = i, c = 0;; j = (j + 1) % n, c++) { pos[j] = c; if ((j + 1) % n == i) break; } for (long long j = i;; j = (j + 1) % n) { long long tans = 0; if (v[j].size() != 0) { long long fin, dist = INT_MAX; for (auto x : v[j]) { long long tdist; if (pos[x] < pos[j]) tdist = pos[x] + n - pos[j]; else tdist = pos[x] - pos[j]; if (tdist < dist) dist = tdist, fin = x; } if (pos[fin] < pos[j]) tans = pos[fin] + (v[j].size()) * n; else tans = (v[j].size() - 1) * n + pos[fin]; ans = max(ans, tans); } if ((j + 1) % n == i) break; } cout << ans << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double pi = acos(-1.0); const long long INF = 0x3f3f3f3f3f3f3f3f; const int inf = 0x3f3f3f3f; const int N = 5000 + 5; int n, m; int a[N]; int b[N]; vector<int> e[N]; int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int inx[N]; void INIT() { for (int i = 1; i <= n; i++) { e[i].clear(); } } void solve() { INIT(); cin >> m; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; int num = (y - x + n) % n; e[x].push_back(num); } for (int i = 1; i <= n; i++) { sort(e[i].begin(), e[i].end()); } for (int i = 1; i <= n; i++) { long long ans = 0; for (int j = 1; j <= n; j++) { long long sz = e[j].size(); if (sz == 0) continue; long long res = (j - i + n) % n; res += (sz - 1) * (long long)n; res += e[j][0]; ans = max(res, ans); } cout << (ans), cout << " "; } cout << '\n'; } int main() { ios::sync_with_stdio(false); while (cin >> n) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 5005; long long cnt[MAXN]; long long mn[MAXN]; long long tim[MAXN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < (int)(n); i++) { mn[i] = 1000000000; } for (int i = 0; i < (int)(m); i++) { long long x, y; cin >> x >> y; x--; y--; cnt[x]++; mn[x] = min(mn[x], (y - x + n) % n); } for (int i = 0; i < (int)(n); i++) { if (cnt[i] > 0) { tim[i] = (cnt[i] - 1) * n + mn[i]; } } for (int i = 0; i < (int)(n); i++) { long long res = 0; for (int j = 0; j < (int)(n); j++) { if (tim[(i + j) % n] > 0) { res = max(res, j + tim[(i + j) % n]); } } cout << res << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> candy[5005]; int times[5005]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; candy[a - 1].push_back(b - 1); } for (int i = 0; i < n; i++) { int s = candy[i].size(); int d = n; for (int j = 0; j < s; j++) { d = min(d, (candy[i][j] - i + 2 * n) % n); } times[i] = (s - 1) * n + d; if (s == 0) { times[i] = 0; } } for (int i = 0; i < n; i++) { int ans = 0; for (int j = 0; j < n; j++) { if (times[j] > 0) { ans = max(ans, times[j] + (j - i + 2 * n) % n); } } if (i != n - 1) { cout << ans << " "; } else { cout << ans; } } }
#include <bits/stdc++.h> using namespace std; int mod; vector<int> g[5001]; int moddec(int a, int b) { if (a - b < 0) return a - b + mod; return (a - b) % mod; } int main() { int n, m; cin >> n >> m; mod = n; for (int i = 0; i < m; i++) { int v, t; cin >> v >> t; v--; t--; g[v].push_back(t); } for (int st = 0; st < n; st++) { int ans = 0; for (int i = 0; i < n; i++) { int curdist = moddec(i, st), sz = g[i].size(); if (sz == 0) continue; int mindist = INT_MAX; for (auto j : g[i]) mindist = min(mindist, moddec(j, i)); ans = max(ans, mindist + curdist + n * (sz - 1)); } cout << ans << " "; } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 5005; vector<long long> candies[MAXN]; int main() { int n, m; cin >> n >> m; while (m--) { int a, b; cin >> a >> b; if (b < a) b += n; candies[a].push_back(b); } for (int i = 1; i <= n; i++) sort(candies[i].begin(), candies[i].end()); for (int st = 1; st <= n; st++) { int ans = 0, del = 0; for (int i = 0; i < n; i++) { int cur = st + i; if (cur > n) cur -= n; if (candies[cur].empty()) { del++; continue; } int cst = del + (n * (candies[cur].size() - 1) + candies[cur][0] - cur); ans = max(ans, cst); del++; } cout << ans << " "; } cout << endl; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(0); int n, m; cin >> n >> m; vector<vector<int> > a(n + 2); function<int(int, int)> dist = [&](int x, int y) { return (x >= y) ? x - y : x - y + n; }; for (int i = 1, x, y; i <= m; i++) { cin >> x >> y; a[x].push_back(dist(y, x)); } for (int i = 1; i <= n; i++) { sort(a[i].begin(), a[i].end()); } for (int i = 1; i <= n; i++) { int res = 0, mx = 0; for (int k = 1, j = i; k <= n; k++, j++) { if (!a[j].empty()) { int xtra = max(0, (int)a[j].size() - 1) * n; mx = max(mx, xtra + dist(j, i) + 1 + a[j][0]); } if (j == n) j = 0; } cout << mx - 1 << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; const int M = 20005; int a[M], b[M]; vector<int> c[M]; void solve(int n, int second) { int ans = 0, i = second; do { int i1 = (i - second + n) % n; int cst = (c[i].empty() ? 0 : 1 << 30); for (int j : c[i]) cst = min(cst, (j - second + n) % n + ((j - second + n) % n < i1 ? n : 0)); ans = max(ans, cst + (int)(c[i].size() - 1) * n); i = (i + 1) % n; } while (i != second); printf("%d ", ans); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { scanf("%d%d", &a[i], &b[i]); c[--a[i]].push_back(--b[i]); } for (int i = 0; i < n; ++i) solve(n, i); return 0; }
#include <bits/stdc++.h> using namespace ::std; int ri() { int x; scanf("%d", &x); return x; } int64_t ri64() { int64_t x; scanf("%lld", &x); return x; } void wi(int x) { printf("%d\n", x); } void wvi(vector<int> &v) { for (int i = 0; i < v.size(); i++) printf("%d ", v[i]); printf("\n"); } void dbg(string &str, int x) { cout << str << ": "; wi(x); } void dbg(string &str, vector<int> &x) { cout << str << ": "; wvi(x); } int main() { int n = ri(); int m = ri(); vector<vector<int> > P(n); for (int i = 0; i < m; i++) { int x = ri(); int y = ri(); x--; y--; int t = y - x; if (t < 0) t += n; P[x].push_back(t); } for (int i = 0; i < n; i++) { if (!P[i].empty()) sort(P[i].begin(), P[i].end()); } vector<int> Q(n, 0); for (int i = 0; i < n; i++) { if (!P[i].empty()) Q[i] = P[i][0] + n * (P[i].size() - 1); else Q[i] = -100000000; } for (int i = 0; i < n; i++) { int ans = 0; for (int j = 0; j < n; j++) { int k = (i + j) % n; ans = max(ans, j + Q[k]); } cout << ans << " "; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long int N = 1e5 + 5; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, m; cin >> n >> m; vector<long long int> G[N]; for (long long int i = 0; i < m; ++i) { long long int a, b; cin >> a >> b; G[a].push_back(b); } long long int rnd[N]; for (long long int i = 1; i < n + 1; ++i) { long long int z = G[i].size(); rnd[i] = z - 1; } long long int far[N]; for (long long int i = 1; i < n + 1; ++i) { far[i] = N * N * N; if (rnd[i] >= 0) { for (long long int j = 0; j < G[i].size(); ++j) { long long int x = G[i][j]; if (x > i) far[i] = min(x - i, far[i]); else far[i] = min(n - i + x, far[i]); } } if (G[i].size() == 0) far[i] = 0; } for (long long int i = 1; i < n + 1; ++i) { long long int mx = 0; for (long long int j = 1; j < n + 1; ++j) { long long int s = 0; if (rnd[j] > 0 || far[j] > 0) { long long int z = j - i; if (j < i) z = n - i + j; s = z + rnd[j] * n + far[j]; } mx = max(s, mx); } cout << mx << " "; } cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; long long const MX = 2e4 + 10; long long const inf = 1e9; int res[MX]; vector<int> arr[MX]; int aa = 1; bool cmp(int x, int y) { if (x > aa && y > aa) return x > y; if (x > aa && y < aa) return 0; if (x<aa, y> aa) return 1; if (x < aa && y < aa) return x > y; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; arr[x].push_back(y); } for (int i = 1; i <= n; i++) { sort(arr[i].begin(), arr[i].end(), cmp); aa++; } int rnd = 0, stat = 0; for (int i = 1; i <= n; i++) { int siz = arr[i].size(); if (siz == 0) continue; int tmprnd = siz, tmpstat = arr[i][siz - 1]; if (tmpstat < i) tmprnd++; if (tmprnd > rnd) { rnd = tmprnd; stat = tmpstat; } else if (tmprnd == rnd) stat = max(stat, tmpstat); } res[1] = rnd * n - 1 - (n - stat); for (int i = 2; i <= n; i++) { int siz = arr[i - 1].size(); if (siz == 0) { res[i] = res[i - 1] - 1; continue; } int tmprnd = siz + 1, tmpstat = arr[i - 1][siz - 1]; if (tmpstat < i) tmprnd++; if (tmprnd > rnd) { rnd = tmprnd; stat = tmpstat; } else if (tmprnd == rnd) stat = max(stat, tmpstat); res[i] = rnd * n - 1 - (n - stat) - (i - 1); } for (int i = 1; i <= n; i++) cout << res[i] << " "; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, pos, dest; cin >> n >> m; vector<vector<int>> sweets(n); vector<long long> place_total_steps(n); for (int i = 0; i < m; i++) { cin >> pos >> dest; pos--; dest--; sweets[pos].push_back(dest); } for (int i = 0; i < n; i++) { long long min_steps = 1e18; for (int j = 0; j < sweets[i].size(); j++) { int far_pos_cand = sweets[i][j]; if (far_pos_cand < i) { far_pos_cand += n; } min_steps = min<long long>(min_steps, far_pos_cand - i); } if (!sweets[i].empty()) { long long total_steps = static_cast<long long>(sweets[i].size() - 1) * n + min_steps; place_total_steps[i] = total_steps; } } vector<long long> ans_vec(n); for (int start_pos = 0; start_pos < n; start_pos++) { long long ans = 0; int cur = start_pos; for (long long it = 0; it < n; it++) { if (!sweets[cur].empty()) ans = max(ans, it + place_total_steps[cur]); cur++; if (cur == n) cur = 0; } ans_vec[start_pos] = ans; } for (int i = 0; i < n; i++) { cout << ans_vec[i] << ' '; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, a, b, dis; int f[50000]; int h[50000] = {0}, vis[50000]; int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a >> b; f[a]++; dis = b - a; if (dis < 0) dis += n; if (f[a] == 1 || dis < h[a]) h[a] = dis; } for (int i = 1; i <= n; i++) { int ff = 0, ans = 0; for (int j = 0; j < n; j++) { int k = i + j; if (k > n) k -= n; if (f[k] > 0) { ans = max(ans, j + (f[k] - 1) * n + h[k]); } } cout << ans << " "; } }
#include <bits/stdc++.h> using namespace std; const int N = 5100; const long long mod = 1e9 + 7; int n, m; int dis(int x, int y) { if (y < x) return n - x + y; else return y - x; } vector<int> f[N]; int now; int cmp(int a, int b) { return dis(now, a) >= dis(now, b); } int head[N]; int fa[N]; int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } int base, cnt; int pp[N], cp[N]; int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) { int x, y; scanf("%d %d", &x, &y); f[x].push_back(y); } int mx = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j < f[i].size(); j++) pp[f[i][j]]++; int now = i; f[i].clear(); for (int j = 1; j <= n; j++) { while (pp[now]) f[i].push_back(now), pp[now]--; now = now + 1; if (now > n) now = 1; } reverse(f[i].begin(), f[i].end()); mx = max(mx, (int)f[i].size()); } mx = max(1, mx - 1); base = n * (mx - 1); for (int i = 1; i <= n; i++) { if (f[i].size() < mx) f[i].clear(); else { int x = f[i][f[i].size() - 1]; int ff = 0, y; if (f[i].size() == mx + 1) ff = 1, y = f[i][f[i].size() - 2]; f[i].clear(); if (ff) cnt++, f[i].push_back(y); f[i].push_back(x); cnt++; } } for (int i = 1; i <= n; i++) { now = i; for (int j = 1; j <= n; j++) pp[j] = cp[j] = 0; int ans = base; int tmp = cnt; while (tmp) { if (cp[now] < f[now].size()) pp[f[now][cp[now]]]++; cp[now]++; if (pp[now]) tmp -= pp[now], pp[now] = 0; now = now + 1; if (now > n) now = 1; ans++; } printf("%d", ans - 1); if (i != n) printf(" "); else printf("\n"); } return 0; }
#include <bits/stdc++.h> template <typename T> T gcd(T a, T b) { return ((a % b == 0) ? b : gcd(b, a % b)); } template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } using LL = long long; constexpr LL LINF = 334ll << 53; constexpr int INF = 15 << 26; constexpr LL MOD = 1E9 + 7; namespace Problem { using namespace std; class Solver2 { public: LL n, m; vector<vector<LL>> to; vector<pair<LL, LL>> dist; Solver2(LL n, LL m) : n(n), m(m), to(n), dist(n){}; pair<LL, LL> calc(int x) { pair<LL, LL> ret = {0ll, LINF}; ret.first = to[x].size(); for (LL y : to[x]) { ret.second = min(ret.second, ((y - x + n) % n)); } if (ret.first == 0) ret.second = 0; return ret; } void solve() { for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; to[a].push_back(b); } for (int i = 0; i < n; ++i) { dist[i] = calc(i); ; } vector<LL> ans(n); for (int s = 0; s < n; ++s) { LL tmp = 0; for (int i = 0; i < n; ++i) { tmp = max(tmp, n * (dist[i].first - 1) + (i - s + n) % n + dist[i].second); } ans[s] = tmp; } for (int i = 0; i < n; ++i) { cout << ans[i] << ' '; } cout << "\n"; } }; } // namespace Problem int main() { std::cin.tie(0); std::ios_base::sync_with_stdio(false); long long n = 0, m; std::cin >> n >> m; Problem::Solver2 sol(n, m); sol.solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int INFL = (int)1e9; const long long int INFLL = (long long int)1e18; const double INFD = numeric_limits<double>::infinity(); const double PI = 3.14159265358979323846; bool nearlyeq(double x, double y) { return abs(x - y) < 1e-9; } bool inrange(int x, int t) { return x >= 0 && x < t; } long long int rndf(double x) { return (long long int)(x + (x >= 0 ? 0.5 : -0.5)); } long long int floorsqrt(long long int x) { long long int m = (long long int)sqrt((double)x); return m + (m * m <= x ? 0 : -1); } long long int ceilsqrt(long long int x) { long long int m = (long long int)sqrt((double)x); return m + (x <= m * m ? 0 : 1); } long long int rnddiv(long long int a, long long int b) { return (a / b + (a % b * 2 >= b ? 1 : 0)); } long long int ceildiv(long long int a, long long int b) { return (a / b + (a % b == 0 ? 0 : 1)); } long long int gcd(long long int m, long long int n) { if (n == 0) return m; else return gcd(n, m % n); } long long int lcm(long long int m, long long int n) { return m * n / gcd(m, n); } int dist(int s, int t, int n) { if (s <= t) return t - s; else return t + n - s; } int main() { int n, m; cin >> n >> m; vector<vector<long long int>> a(n); for (int i = 0; i < (int)(m); i++) { int s, t; cin >> s >> t; s--; t--; a[s].push_back(dist(s, t, n)); } for (int i = 0; i < (int)(n); i++) { sort(a[i].begin(), a[i].end()); } vector<long long int> ans(n, 0); for (int i = 0; i < (int)(n); i++) { for (int j = 0; j < (int)(n); j++) { if (a[j].size()) { long long int buf = dist(i, j, n) + (a[j].size() - 1) * n + a[j][0]; ans[i] = max(ans[i], buf); } } } for (int extra_i = 0; extra_i < (int)(ans.size()); extra_i++) { cout << ans[extra_i] << " "; } cout << endl; ; }
#include <bits/stdc++.h> using namespace std; const int man = 5005; int ans[man]; int d[man]; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { ans[i] = man; } for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; d[u]++; ans[u] = min(ans[u], (v - u + n) % n); } for (int i = 1; i <= n; i++) { int r = 0; for (int j = 1; j <= n; j++) { if (d[j]) r = max(r, (j - i + n) % n + (d[j] - 1) * n + ans[j]); } cout << r << ' '; } return 0; }
#include <bits/stdc++.h> using namespace std; int a[5001], c[5001]; int n, m, x, y, mg = 0; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d %d", &x, &y); if (x == y) continue; if (c[x] == 0) a[x] = 1000000009; c[x]++; mg = max(mg, c[x]); int d = y; if (y < x) d += n; a[x] = min(a[x], d); } if (mg == 0) { for (int i = 1; i <= n; i++) { printf("0 "); } return 0; } for (int i = 1; i <= n; i++) { if (c[i] > 0) a[i] += (c[i] - 1) * n; } for (int i = 1; i <= n; i++) { int mx = 0; for (int j = 1; j <= n; j++) { a[j]--; if (c[j] > 0) mx = max(mx, a[j]); } printf("%d ", mx); a[i] += n; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; constexpr int N = 1e6 + 5, mod = 1e9 + 7; constexpr ld eps = 1e-9, inf = 1e50; vector<int> a[N]; int n, m; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { int x, y; scanf("%d%d", &x, &y); --x, --y; a[x].emplace_back(y); } for (int i = 0; i < n; ++i) sort(a[i].begin(), a[i].end(), [i](int x, int y) { return (x - i + n) % n < (y - i + n) % n; }); for (int i = 0; i < n; ++i) { ll ans = 0; for (int j = 0; j < n; ++j) if (a[j].size()) { ans = max(ans, (j - i + n) % n + 1ll * ((ll)a[j].size() - 1) * n + (a[j][0] - j + n) % n); } printf("%lld%c", ans, " \n"[i == n - 1]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5005; vector<int> st[maxn]; int n, m; int a, b; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d %d", &a, &b); int x; if (b >= a) x = b - a; else x = n - a + b; st[a].push_back(x); } for (int i = 1; i <= n; i++) { sort(st[i].begin(), st[i].end()); } for (int i = 1; i <= n; i++) { int res = -1; for (int j = 1; j <= n; j++) { if (!st[j].size()) continue; int x = 0; if (i <= j) x = j - i; else x = n - i + j; int y = st[j].size() - 1; x += y * n; x += st[j][0]; if (res < x) res = x; } printf("%d ", res); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; int minn[5005]; int br[5005]; int dis(int poc, int kraj) { if (poc <= kraj) return kraj - poc; return kraj + n - poc; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n; cin >> m; for (int i = 1; i <= n; i++) minn[i] = n + 10; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; minn[x] = min(minn[x], dis(x, y)); br[x]++; } for (int i = 1; i <= n; i++) { int sol = 0; for (int j = 1; j <= n; j++) { if (br[j] != 0) { sol = max(sol, dis(i, j) + (br[j] - 1) * n + minn[j]); } } cout << sol << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m; long long dist(long long x, long long y) { if (x <= y) return y - x; else return (n + 1 - x) + (y - 1); } int main() { cin >> n >> m; vector<long long> v[5050]; for (int i = 1; i <= m; i++) { long long a, b; cin >> a >> b; v[a].push_back(dist(a, b)); } for (int i = 1; i <= n; i++) { sort(v[i].begin(), v[i].end()); } for (int start = 1; start <= n; start++) { long long jwb = 0; for (int i = 1; i <= n; i++) { if (v[i].empty()) continue; long long jum = 0; jum += dist(start, i); jum += n * (v[i].size() - 1); jum += v[i][0]; jwb = max(jwb, jum); } cout << jwb << " "; } cout << endl; }
#include <bits/stdc++.h> using namespace std; void FastIO() { std::ios_base::sync_with_stdio(0); cin.tie(0); } int main() { FastIO(); int n, m; cin >> n >> m; vector<vector<int>> st(n, vector<int>()); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a, --b; st[a].push_back((b - a + n) % n); } std::multiset<int> all; for (int i = 0; i < n; ++i) { sort(st[i].begin(), st[i].end(), [](int a, int b) { return a > b; }); for (int j = 0; j < st[i].size(); ++j) { all.insert(i + n * j + st[i][j]); } } for (int i = 0; i < n; ++i) { cout << *all.rbegin() - i << " "; for (int j = 0; j < st[i].size(); ++j) { auto was = i + n * j + st[i][j]; all.erase(all.find(was)); all.insert(was + n); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int n, m, a, b, s[5100]; vector<int> g[5100]; int dis(int x, int y) { if (x <= y) return y - x; else return n - x + y; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d", &a, &b); g[a].push_back(b); } for (int i = 1; i <= n; i++) { if (int((g[i]).size())) s[i] = INF; for (auto t : g[i]) { s[i] = min(s[i], (int((g[i]).size()) - 1) * n + dis(i, t)); } } for (int i = 1; i <= n; i++) { int ans = 0; for (int j = 1; j <= n; j++) { if (s[j]) ans = max(ans, dis(i, j) + s[j]); } cout << ans << ' '; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j, k, a, b, ans; cin >> n >> m; int c[n + 1], d[n + 1]; fill(c, c + n + 1, 0); fill(d, d + n + 1, 5002); for (i = 0; i < m; i++) { cin >> a >> b; c[a]++; d[a] = min(d[a], (b - a + n) % n); } for (i = 1; i < n + 1; i++) { if (!c[i]) { d[i] = 0; } else { d[i] += (c[i] - 1) * n; } } for (i = 1; i < n + 1; i++) { ans = 0; for (j = 1; j < n + 1; j++) { if (d[j]) ans = max(ans, d[j] + (j - i + n) % n); } cout << ans << " "; } cout << '\n'; }
#include <bits/stdc++.h> const long long int MOD = 1e9 + 7; using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long int finish[5005] = {0}, freq[5005][5005] = {0}, total[5005] = {0}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int n, m; cin >> n >> m; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; total[a]++; freq[a][b]++; } for (int i = 0; i < n; ++i) { long long int tmp = max(0LL, (total[i] - 1) * n); int j = (i + 1 + n) % n; while (j != i) { if (freq[i][j] != 0) { if (j > i) tmp += j - i; else tmp += n - i + j; break; } j++; j %= n; } finish[i] = tmp; } for (int i = 0; i < n; ++i) { long long int ans = finish[i]; int j = (i + 1) % n; while (j != i) { int tmp; if (j > i) tmp = j - i; else tmp = n - i + j; if (finish[j] != 0) ans = max(ans, finish[j] + tmp); ++j; j %= n; } cout << ans << " "; } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize "-O3" using namespace std; vector<int> go[5100]; int n, m; int ds(int a, int b) { if (a <= b) return b - a; else return b - a + n; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a, --b; go[a].push_back(ds(a, b)); } for (int i = 0; i < n; ++i) sort((go[i]).begin(), (go[i]).end()); for (int i = 0; i < n; ++i) { int now = 0; for (int j = 0; j < n; ++j) if (!go[j].empty()) now = max(now, n * ((int)go[j].size() - 1) + ds(i, j) + go[j][0]); cout << now << " "; } cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; const long long int inf = 1e18; const long long int MAXN = 1000005; template <typename S, typename T> ostream& operator<<(ostream& out, pair<S, T> const& p) { out << '(' << p.f << ", " << p.s << ')'; return out; } template <typename T> ostream& operator<<(ostream& out, vector<T> const& v) { long long int l = v.size(); for (long long int i = 0; i < l - 1; i++) out << v[i] << ' '; if (l > 0) out << v[l - 1]; return out; } template <typename T> void trace(const char* name, T&& arg1) { cout << name << " : " << arg1 << endl; } template <typename T, typename... Args> void trace(const char* names, T&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; trace(comma + 1, args...); } void solve() { long long int n, m, u, v; cin >> n >> m; vector<long long int> dest[n + 1]; for (int i = 1; i <= m; i++) { cin >> u >> v; dest[u].push_back(v); } unordered_map<long long int, long long int> opt; for (int i = 1; i <= n; i++) { for (auto t : dest[i]) { long long int temp = t - i; if (temp < 0) { temp += n; } if (opt[i] == 0) { opt[i] = temp; } else { opt[i] = min(opt[i], temp); } } } vector<long long int> ans; for (int i = 1; i <= n; i++) { long long int optval = -1; for (int j = 1; j <= n; j++) { long long int temp = j - i; if (temp < 0) { temp += n; } long long int lulz = dest[j].size(); temp += (lulz - 1) * n + opt[j]; optval = max(temp, optval); } ans.push_back(optval); } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(50); solve(); }
#include <bits/stdc++.h> using namespace std; const int MAXN = 5005; int n, m, x, y; int N[5005], M[20005], r; int dis(int u, int v) { if (v >= u) return v - u; else return v + n - u; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> x >> y; if (N[x] == 0) N[x] = dis(x, y); else N[x] = min(N[x], dis(x, y)); M[x]++; } for (int i = 1; i <= n; i++) { r = 0; for (int j = 1; j <= n; j++) r = max(r, dis(i, j) + n * (M[j] - 1) + N[j]); cout << r << " "; } }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const long long M = 1e9 + 7; template <typename T> void print(vector<T>& v) { cout << "["; for (int i = 0; i < v.size(); ++i) { cout << v[i]; if (i != v.size() - 1) cout << ", "; } cout << "]\n"; } template <typename T> void print(set<T>& v) { cout << "["; for (auto it : v) { cout << it; if (it != *v.rbegin()) cout << ", "; } cout << "]\n"; } template <typename T, typename S> void print(map<T, S>& v) { for (auto it : v) cout << it.first << " : " << it.second << "\n"; } template <typename T, typename S> void print(pair<T, S>& v) { cout << "( " << v.first << ", " << v.second << " )" << '\n'; } template <typename T> void print(T x) { cout << x << '\n'; } int n, m; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; vector<vector<long long> > station(n, vector<long long>(0)); for (long long i = 0; i < m; i++) { long long a, b; cin >> a >> b; a--; b--; if (b > a) { station[a].push_back(b - a); } else { station[a].push_back(n - (a - b)); } } long long ms = 0; for (long long i = 0; i < n; i++) { ms = max((long long)station[i].size(), ms); sort(station[i].begin(), station[i].end()); } for (long long i = 0; i < n; i++) { long long torem = max(0ll, ms - 2); while (station[i].size() && torem > 0) { torem--; station[i].pop_back(); } } for (long long s = 0; s < n; s++) { vector<vector<long long> > temp = station; long long curdis = 0; long long dis = 0; for (long long i = s; i < s + 3 * n; i++) { if (temp[i % n].size()) { dis = max(curdis + temp[i % n].back(), dis); temp[i % n].pop_back(); } curdis++; } cout << dis + max(ms - 2, 0ll) * n << ' '; } cout << '\n'; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pi = pair<int, int>; using pll = pair<ll, ll>; int delta(int n, int a, int b) { return a <= b ? b - a : b - a + n; } int main() { ios_base::sync_with_stdio(false); cout << setprecision(12); cin.tie(nullptr); int n, m; cin >> n >> m; vector<vector<int>> candy(n); for (int i = 0; i < int(m); i++) { int a; cin >> a; a--; ; int b; cin >> b; b--; ; candy[a].push_back(b); } vector<int> maxt(n); for (int a = 0; a < int(n); a++) { int k = candy[a].size(); maxt[a] = n * k; for (int b : candy[a]) maxt[a] = min(maxt[a], n * (k - 1) + delta(n, a, b)); } for (int a = 0; a < int(n); a++) { int ans = 0; for (int b = 0; b < int(n); b++) if (maxt[b] > 0) ans = max(ans, delta(n, a, b) + maxt[b]); cout << ans << " "; } cout << endl; }
#include <bits/stdc++.h> using namespace std; int n, m, mn[20005]; vector<int> G[20005]; int dis(int a, int b) { return (b - a + n) % n; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); G[a].push_back(b); } for (int i = 1; i <= n; i++) if (G[i].size()) { mn[i] = 0x3f3f3f3f; for (int j = 0, sz = G[i].size(); j < sz; j++) mn[i] = min(mn[i], dis(i, G[i][j])); mn[i] += (G[i].size() - 1) * n; } for (int i = 1; i <= n; i++) { int ans = 0; for (int j = 1; j <= n; j++) if (G[j].size()) ans = max(ans, mn[j] + dis(i, j)); printf("%d%c", ans, i == n ? '\n' : ' '); } }
#include <bits/stdc++.h> using namespace std; const long long int N = 1e5 + 2; const long long int INF = 1e9 + 9; const long long int mod = 1e9 + 7; void solve() { long long int n, m; cin >> n >> m; long long int i, j, a, b, x, ans = 0, maxi = 0, mini = INF, dist[n + 1]; vector<long long int> vec; multiset<long long int> source[n + 1]; for (i = 1; i <= m; i++) { cin >> a >> b; x = (b % n - a % n + n) % n; source[a].insert(b); x = source[a].size(); maxi = max(maxi, x); } for (i = 1; i <= n; i++) { dist[i] = INF; for (auto it : source[i]) { dist[i] = min(dist[i], (it % n - i % n + n) % n); } } for (i = 1; i <= n; i++) { ans = 0; for (j = 1; j <= n; j++) { if (source[j].size()) { mini = (j % n - i % n + n) % n + (source[j].size() - 1) * n + dist[j]; ans = max(ans, mini); } } cout << ans << " "; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long int t = 1; for (long long int i = 1; i <= t; i++) { solve(); } }
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c *x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug &operator<<(const c &) { return *this; } }; void read(vector<long long> &a) { for (long long &i : a) cin >> i; } void read(vector<int> &a) { for (int &i : a) cin >> i; } long long INF64 = 1e18; long long mod = 1e9 + 7; int INF = 1e9; long long fastpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } void test_case(int tnum) { int n, m; cin >> n >> m; vector<int> cnt(n + 1); vector<int> mn(n + 1, INF); vector<vector<int>> adj(n + 1); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; cnt[u]++; ; mn[u] = min(mn[u], (v - u + n) % n); adj[u].push_back(v); } int mx = 0; for (int i = 1; i <= n; i++) { if (mn[i] == INF) continue; mx = max(mx, cnt[i]); } vector<int> mxsta; for (int i = 1; i < n + 1; i++) { if (cnt[i] == mx) { mxsta.push_back(i); } } for (int i = 1; i <= n; i++) { int best = -INF; pair<int, int> p; for (int j = 1; j <= n; j++) { if (cnt[j] == 0) { continue; } int mn = INF; int no = -1; for (int k : adj[j]) { int dist = (k - j + n) % n; dist += (j - i + n) % n; dist += (cnt[j] - 1) * n; if (dist < mn) no = k; mn = min(mn, dist); } if (mn > best) { p = {j, no}; } best = max(best, mn); } cout << best << ' '; } } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); int t = 1; for (int test = 1; test <= t; test++) { test_case(test); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5100, M = 21000; int n, m, x, y, rd[N], a[N], w[N]; int main() { scanf("%d%d", &n, &m); memset(a, 0x3f, sizeof(a)); for (int i = (1); i <= (m); i++) scanf("%d%d", &x, &y), rd[x]++, a[x] = min(a[x], (y - x + n) % n); for (int i = (1); i <= (n); i++) if (rd[i]) w[i] = (rd[i] - 1) * n + a[i]; for (int i = (1); i <= (n); i++) { int ans = 0; for (int j = (1); j <= (n); j++) if (rd[j]) ans = max(ans, w[j] + (j - i + n) % n); printf("%d ", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vd = vector<double>; using vs = vector<string>; using vpii = vector<pair<int, int>>; using vpll = vector<pair<ll, ll>>; using pii = pair<int, int>; using pll = pair<ll, ll>; const int N = 5007; vi adj[N]; void solve() { int n, m; cin >> n >> m; for (ll i = 0; i < (m); i++) { int a, b; cin >> a >> b; adj[a].push_back(b); } int d[N]; memset(d, 0, sizeof(d)); for (int i = 1; i <= n; i++) { if (adj[i].size() == 0) continue; d[i] = ((int)adj[i].size() - 1) * n; int mn = INT_MAX; for (int x : adj[i]) { int dist = x - i; if (dist < 0) dist += n; mn = min(mn, dist); } if (mn == INT_MAX) continue; d[i] += mn; } for (int i = 1; i <= n; i++) { int ans = 0; for (int j = 1; j <= n; j++) { if (d[j] == 0) continue; int dist = j - i; if (dist < 0) dist += n; ans = max(ans, d[j] + dist); } cout << ans << " "; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; bool prime[1000001]; long long spf[10000001]; long long f[300005]; long long pow1(long long x, long long y) { long long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } long long divide(long long n) { return pow1(n, mod - 2); } long long ncr(long long n, long long r) { if (n < r) return 0; return (f[n] * ((divide(f[r]) * divide(f[n - r])) % mod)) % mod; } void sieve() { memset(prime, true, sizeof(prime)); for (long long i = 2; i * i <= 1000000; i++) if (prime[i]) for (long long j = i * i; j <= 1000000; j += i) prime[j] = false; prime[0] = prime[1] = false; } void fastsieve() { spf[1] = 1; for (int i = 2; i <= 1e7; i++) spf[i] = i; for (int i = 4; i <= 1e7; i += 2) spf[i] = 2; for (int i = 3; i * i <= 1e7; i++) { if (spf[i] == i) { for (int j = i * i; j <= 1e7; j += i) if (spf[j] == j) spf[j] = i; } } } vector<long long> factorize(long long n) { long long count = 0; vector<long long> fac; while (!(n % 2)) { n >>= 1; count++; } if (count % 2) fac.push_back(2ll); for (long long i = 3; i <= sqrt(n); i += 2) { count = 0; while (n % i == 0) { count++; n = n / i; } if (count % 2) fac.push_back(i); } if (n > 2) fac.push_back(n); return fac; } vector<long long> fastfactorize(long long n) { vector<long long> v; long long prev = 0, cnt = 0; while (n != 1) { if (prev == spf[n]) cnt++; else { if (cnt % 2) v.push_back(prev); cnt = 1; prev = spf[n]; } n /= spf[n]; if (n == 1) { if (cnt % 2) v.push_back(prev); cnt = 1; prev = spf[n]; } } return v; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, i, j; cin >> n >> m; long long cnt[n + 1], val[n + 1]; memset(cnt, 0, sizeof(cnt)); for (i = 0; i <= n; i++) val[i] = 1e18; for (i = 1; i <= m; i++) { long long x, y; cin >> x >> y; cnt[x]++; val[x] = min(val[x], (y - x + n) % n); } for (i = 1; i <= n; i++) { long long ans = 0; for (j = 1; j <= n; j++) { if (!cnt[j]) continue; ans = max(ans, (j - i + n) % n + n * (cnt[j] - 1) + val[j]); } cout << ans << " "; } return 0; }
#include <bits/stdc++.h> template <typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; } template <typename First, typename... Rest> void MACRO_VAR_Scan(First& first, Rest&... rest) { std::cin >> first; MACRO_VAR_Scan(rest...); } template <typename T> void MACRO_VEC_ROW_Init(int n, T& t) { t.resize(n); } template <typename First, typename... Rest> void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) { first.resize(n); MACRO_VEC_ROW_Init(n, rest...); } template <typename T> void MACRO_VEC_ROW_Scan(int p, T& t) { std::cin >> t[p]; } template <typename First, typename... Rest> void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) { std::cin >> first[p]; MACRO_VEC_ROW_Scan(p, rest...); } template <class T> inline T CHMAX(T& a, const T b) { return a = (a < b) ? b : a; } template <class T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; } template <class T> using V = std::vector<T>; template <class T> using VV = V<V<T>>; template <typename S, typename T> std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; } using ll = long long; using ull = unsigned long long; using ld = long double; using PAIR = std::pair<ll, ll>; using PAIRLL = std::pair<ll, ll>; constexpr ll INFINT = 1 << 30; constexpr ll INFINT_LIM = (1LL << 31) - 1; constexpr ll INFLL = 1LL << 60; constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); constexpr double EPS = 1e-6; constexpr ll MOD = 1000000007; constexpr double PI = 3.141592653589793238462643383279; template <class T, size_t N> void FILL(T (&a)[N], const T& val) { for (auto& x : a) x = val; } template <class ARY, size_t N, size_t M, class T> void FILL(ARY (&a)[N][M], const T& val) { for (auto& b : a) FILL(b, val); } template <class T> void FILL(std::vector<T>& a, const T& val) { for (auto& x : a) x = val; } template <class ARY, class T> void FILL(std::vector<std::vector<ARY>>& a, const T& val) { for (auto& b : a) FILL(b, val); } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); ; ll n, m; MACRO_VAR_Scan(n, m); ; std::vector<ll> a, b; MACRO_VEC_ROW_Init(m, a, b); for (ll w_ = 0; w_ < m; ++w_) { MACRO_VEC_ROW_Scan(w_, a, b); }; std::vector<std::vector<ll>> g(n); for (ll i = 0; i < ll(m); ++i) { --a[i]; --b[i]; g[a[i]].emplace_back(b[i]); } std::vector<ll> min(n, INFLL); for (ll i = 0; i < ll(n); ++i) { for (ll x : g[i]) { ll cost = x - i; if (cost <= 0) cost += n; CHMIN(min[i], cost); } } for (ll i = 0; i < ll(n); ++i) { ll ans = 0; for (ll j = 0; j < ll(n); ++j) { ll J = (i + j) % n; if (g[J].empty()) continue; ll len = g[J].size(); ll sum = (len - 1) * n + j + min[J]; CHMAX(ans, sum); } std::cout << (ans); std::cout << (i + 1 == n ? '\n' : ' '); ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; const int inf = 0x3f3f3f3f; int n, m; int mi[maxn], cnt[maxn]; int main() { cin >> n >> m; memset(mi, 0x3f, sizeof(mi)); for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; mi[x] = min(mi[x], (y - x + n) % n); cnt[x]++; } for (int i = 1; i <= n; i++) { long long ans = 0; for (int j = 1; j <= n; j++) if (cnt[j]) ans = max(ans, (j - i + n) % n + (long long)n * (cnt[j] - 1) + mi[j]); cout << ans << ' '; } return 0; }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-ffloat-store") #pragma GCC optimize("-fno-defer-pop") long long int power(long long int a, long long int b, long long int m) { if (b == 0) return 1; if (b == 1) return a % m; long long int t = power(a, b / 2, m) % m; t = (t * t) % m; if (b & 1) t = ((t % m) * (a % m)) % m; return t; } long long int modInverse(long long int a, long long int m) { return power(a, m - 2, m); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int i, j, k, l, n, m; cin >> n >> m; long long int ar[m][2]; vector<long long int> vc[n + 1]; for (i = 0; i < m; i++) { cin >> ar[i][0] >> ar[i][1]; vc[ar[i][0]].push_back(ar[i][1]); } for (i = 1; i <= n; i++) { sort(vc[i].begin(), vc[i].end()); } long long int mx = 0; for (long long int z = 1; z <= n; z++) { mx = 0; for (i = 1; i <= n; i++) { l = 0; if (vc[i].size() > 0) { k = vc[i].size() - 1; l = k * n; if (z <= i) { l += i - z; } else { l += (i - 1) + (n - z) + 1; } long long int mn = 1e18, x; for (x = 0; x < vc[i].size(); x++) { k = vc[i][x]; if (k <= i) { mn = min(mn, (k - 1) + 1 + (n - i)); } else { mn = min(mn, (k - i)); } } l += mn; } mx = max(mx, l); } cout << mx << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 500; const long long mod = 1e9 + 7; const long long INF = 1LL << 57; const int LN = 22; long long n, m, u, v, k, t, q, x, d, p, a, b, c, s; vector<int> rt[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1; i <= m; ++i) { cin >> u >> v; --u, --v; rt[u].push_back((v - u + n) % n); } for (int i = 0; i < n; ++i) sort(rt[i].begin(), rt[i].end()); for (int i = 0; i < n; ++i) { long long best = (rt[i].size() > 0) ? n * ((int)rt[i].size() - 1) + rt[i][0] : 0; for (int j = i + 1, k = 1; k < n; ++j, ++k) { j %= n; best = max( best, ((rt[j].size() > 0) ? n * ((int)rt[j].size() - 1) + rt[j][0] + k : 0)); } cout << best << " "; } cout << endl; }
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << '\n'; err(++it, args...); } long long n, m; vector<int> g[5100]; int ds(int a, int b) { if (a <= b) return b - a; else return b - a + n; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for (long long i = 0; i < m; ++i) { long long a, b; cin >> a >> b; a--; b--; g[a].push_back(ds(a, b)); } for (long long i = 0; i < n; ++i) { sort((g[i]).begin(), (g[i]).end()); } for (long long i = 0; i < n; ++i) { long long now = 0; for (long long j = 0; j < n; ++j) { if (!g[j].empty()) { now = max(now, n * ((int)g[j].size() - 1) + ds(i, j) + g[j][0]); } } cout << now << " "; } cout << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> struct rge { T b, e; }; template <class T> rge<T> range(T i, T j) { return rge<T>{i, j}; } struct debug { template <class T> debug& operator<<(const T&) { return *this; } }; using ll = long long; template <typename T> void min_self(T& a, T b) { a = min(a, b); } template <typename T> void max_self(T& a, T b) { a = max(a, b); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<vector<int>> g(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; --a, --b; g[a].push_back(b); } auto Dist = [&](int x, int y) { int d = y - x; if (d < 0) { d += n; } return d; }; for (int i = 0; i < n; i++) { sort(g[i].begin(), g[i].end(), [&](int x, int y) { return Dist(i, x) < Dist(i, y); }); } for (int i = 0; i < n; i++) { int ans = 0; for (int j = 0; j < n; j++) { if (g[j].empty()) { continue; } int cur = Dist(i, j); cur += n * ((int)g[j].size() - 1); cur += Dist(j, g[j][0]); max_self(ans, cur); } cout << ans << " "; } cout << "\n"; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int k[n]; int nearest[n]; for (int i = 0; i < n; i++) { nearest[i] = 0; k[i] = 0; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; k[a]++; if (nearest[a] != 0) nearest[a] = min(nearest[a], (b - a + n) % n); else nearest[a] = (b - a + n) % n; } for (int i = 0; i < n; i++) { int t = 0; for (int j = 0; j < n; j++) { if (nearest[(j + i) % n] != 0) t = max(t, j + n * (k[(i + j) % n] - 1) + nearest[(j + i) % n]); } cout << t << ' '; } return 0; }
#include <bits/stdc++.h> using namespace std; using pi = pair<int, int>; using ll = long long; using pll = pair<ll, ll>; int n, m; vector<vector<int>> candy; struct Cmp { int i; Cmp(int i) : i(i) {} bool operator()(const int& c1, const int& c2) const { if (c1 >= i && c2 < i) return true; else if (c2 >= i && c1 < i) return false; else return c1 < c2; } }; int fix(int x) { if (x < 0) x += n; return x; } int main() { ios_base::sync_with_stdio(false); cout << setprecision(12); cin.tie(nullptr); cin >> n >> m; candy.resize(n); for (int i = 0; i < int(m); i++) { int a, b; cin >> a >> b; a--, b--; if (a == b) continue; candy[a].push_back(b); } for (int i = 0; i < int(n); i++) sort(candy[i].begin(), candy[i].end(), Cmp(i)); for (int i = 0; i < int(n); i++) { int ans = 0; for (int j = 0; j < n; j++) if (candy[j].size() > 0) ans = max(ans, fix(j - i) + n * (int)(candy[j].size() - 1) + fix(candy[j][0] - j)); cout << ans << " "; } cout << endl; }
#include <bits/stdc++.h> using namespace std; int n, m; int toycnt[5050]; int minway[5050]; int dist(int s, int t) { return (t >= s ? t - s : n + t - s); } int main(void) { memset(minway, 0x3f, sizeof(minway)); scanf("%d%d", &n, &m); while (m--) { int s, t; scanf("%d%d", &s, &t); toycnt[s]++; minway[s] = min(minway[s], dist(s, t)); } for (int sp = 1; sp <= n; sp++) { int ans = 0; for (int np = 1; np <= n; np++) { if (toycnt[np] == 0) continue; ans = max(ans, dist(sp, np) + n * (toycnt[np] - 1) + minway[np]); } printf("%d ", ans); } return 0; }
#include <bits/stdc++.h> using namespace ::std; const long double PI = acos(-1); const long long MOD = 1000000000 + 7; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long add(long long a, long long b, long long m = MOD) { if (a >= m) a %= m; if (b >= m) b %= m; if (a < 0) a += m; if (b < 0) b += m; long long res = a + b; if (res >= m or res <= -m) res %= m; if (res < 0) res += m; return res; } long long mul(long long a, long long b, long long m = MOD) { if (a >= m) a %= m; if (b >= m) b %= m; if (a < 0) a += m; if (b < 0) b += m; long long res = a * b; if (res >= m or res <= -m) res %= m; if (res < 0) res += m; return res; } long long pow_mod(long long a, long long b, long long m = MOD) { long long res = 1LL; a = a % m; while (b) { if (b & 1) res = mul(res, a, m); b >>= 1; a = mul(a, a, m); } return res; } long long fastexp(long long a, long long b) { long long res = 1LL; while (b) { if (b & 1) res = res * a; b >>= 1; a *= a; } return res; } int gcdExtendido(int a, int b, int *x, int *y) { if (a == 0) { *x = 0; *y = 1; return b; } int x1, y1; int gcd = gcdExtendido(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd; } int modInverso(int a, int m) { int x, y; int g = gcdExtendido(a, m, &x, &y); if (g != 1) return -1; else return (x % m + m) % m; } const int N = 5000; int n, m; int ans[N]; int closest[N]; int out[N]; int getDis(int a, int b) { if (a <= b) return b - a; return b + n - a; } int getTime(int pos, int start) { if (out[pos] == 0) return 0; int ans = n * (out[pos] - 1) + closest[pos] + getDis(start, pos); return ans; } int main() { scanf("%d %d", &(n), &(m)); int a, b; for (int i = 1; i <= n; i++) closest[i] = n + 2; for (int i = 0; i < m; i++) { scanf("%d %d", &(a), &(b)); out[a]++; closest[a] = min(closest[a], getDis(a, b)); } for (int i = 1; i <= n; i++) { int act = 0; for (int j = 1; j <= n; j++) { act = max(act, getTime(j, i)); } ans[i] = act; } for (int i = 1; i <= n; i++) printf("%d ", ans[i]); puts(""); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> v[5005]; int tmp[5005]; inline int dist(int a, int b) { return a <= b ? b - a : b + n - a; } int main() { cin >> n >> m; while (m--) { int a, b; cin >> a >> b; v[a].push_back(b); } for (int i = 1; i <= n; i++) for (int j = 0; j < v[i].size(); j++) v[i][j] = dist(i, v[i][j]); for (int i = 1; i <= n; i++) if (!v[i].empty()) tmp[i] = *min_element(v[i].begin(), v[i].end()); for (int i = 1; i <= n; i++) { int ans = 0; for (int j = 1; j <= n; j++) if (!v[j].empty()) ans = max(ans, ((int)v[j].size() - 1) * n + dist(i, j) + tmp[j]); cout << ans << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; long long max(long long a, long long b) { if (a > b) { return a; } else { return b; } } long long min(long long a, long long b) { if (a < b) { return a; } else { return b; } } long long rand_int(long long l, long long r) { static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); return uniform_int_distribution<long long>(l, r)(gen); } long long power(long long b, long long e) { if (e == 0) return 1; if (e % 2) return ((b * power((b) * (b), (e - 1) / 2))); else return power((b) * (b), e / 2); } long long modpower(long long b, long long e, long long q) { long long MOD = q; if (e == 0) return 1; if (e % 2) return ((b % MOD) * modpower((b % MOD) * (b % MOD), (e - 1) / 2, q)) % MOD; else return modpower((b % MOD) * (b % MOD), e / 2, q) % MOD; } void dpv(vector<long long> v) { for (long long i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << '\n'; } void dpv(vector<pair<long long, long long> > v) { for (long long i = 0; i < v.size(); i++) { cout << v[i].first << " " << v[i].second << '\n'; } } void dpv(set<long long> v) { for (auto i : v) { cout << i << " "; } cout << '\n'; } long long ceil1(long long x, long long y) { long long r = x / y; if (x % y == 0) { return r; } else { return r + 1; } } const long long N = 5e3 + 5; long long n, m; vector<long long> small(N, 1e18); vector<long long> out(N); void oblivious() { cin >> n >> m; for (long long i = 0; i < m; i++) { long long x, y; cin >> x >> y; out[x]++; long long dist = y - x; if (dist < 0) { dist += n; } small[x] = min(small[x], dist); } for (long long i = 1; i <= n; i++) { long long ans = 0; for (long long a = 1; a <= n; a++) { if (out[a]) { long long dist = a - i; if (dist < 0) { dist += n; } ans = max(ans, n * (out[a] - 1) + dist + small[a]); } } cout << ans << " "; } cout << '\n'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { oblivious(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long LLINF = 8e18; const int MOD = 1e9 + 7; const int INF = 2e9; const int N = 5000; int n, m, mx, ans, tmx; int a[N], b[N], cnt[N], mn1[N], mn2[N]; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; fill(mn1, mn1 + n, INF); fill(mn2, mn2 + n, INF); while (m--) { int x, y; cin >> x >> y; x--; y--; if (y < x) y += n; if (y < mn1[x]) { mn2[x] = mn1[x]; mn1[x] = y; } else if (y < mn2[x]) { mn2[x] = y; } cnt[x]++; mx = max(mx, cnt[x]); } for (int i = 0; i < n; i++) { ans = (mx - 1) * n; tmx = 0; for (int j = 0; j < n; j++) { if (mn1[j] != INF && cnt[j] == mx) tmx = max(tmx, mn1[j]); if (mn1[j] != INF && cnt[j] == mx - 1) tmx = max(tmx, mn1[j] - n); } ans += tmx - i; if (mn1[i] != INF) mn1[i] += n; if (mn2[i] != INF) mn2[i] += n; cout << ans << ' '; } return 0; }
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main() { ios::sync_with_stdio(0); vector<vector<int> > v; vector<int> mn; int n, m; scanf("%d %d ", &n, &m); v.assign(n, vector<int>()); mn.assign(n, 1e9); while (m--) { int a, b; scanf("%d %d ", &a, &b); a--; b--; v[a].push_back(b); mn[a] = min(mn[a], (((b - a) % n) + n) % n); } int mc = 0; for (int i = 0; i < v.size(); i++) { mc = max(mc, (int)v[i].size()); } for (int i = 0; i < n; i++) { int ex = 0; for (int j = n - 1; j >= 0; j--) { int id = (j + i) % n; if (v[id].size() > 0) { ex = max(ex, mn[id] + j + (int(v[id].size()) - 1) * n); } } if (i > 0) { printf(" "); } printf("%d", ex); } printf("\n"); }
#include <bits/stdc++.h> using namespace std; int i, j, k, n, m; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; vector<int> cnt(5002), a(5002), mn(5002); int x, y; cin >> n >> m; for (i = 0; i <= n; i++) { mn[i] = 5001; } for (i = 0; i < m; i++) { cin >> x >> y; cnt[x]++; mn[x] = min(mn[x], (y + n - x) % n); } for (i = 1; i <= n; i++) { int ans = 0; for (j = 1; j <= n; j++) { if (cnt[j]) { ans = max(ans, (j + n - i) % n + (cnt[j] - 1) * n + mn[j]); } } cout << ans << " "; } return 0; }
#include <bits/stdc++.h> int n, m; std::vector<std::vector<int> > g; std::vector<int> kt; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n >> m; g.resize(n); for (int i = 0; i < (m); ++i) { int a, b; std::cin >> a >> b; a--; b--; g[a].push_back(b); } for (int i = 0; i < (n); ++i) std::sort(g[i].begin(), g[i].end(), [&](const int &a, const int &b) { return a - i + (i > a ? n : 0) < b - i + (i > b ? n : 0); }); int mx = 0; for (int i = 0; i < (n); ++i) { mx = std::max(mx, (int)(g[i]).size()); } int todo = 0; int add = 0; add += n * std::max(0, mx - 3); for (int i = 0; i < (n); ++i) { for (int j = 0; j < (mx - 3); ++j) { if (g[i].empty()) break; g[i].pop_back(); } todo += (int)(g[i]).size(); } kt.resize(n); for (int s = 0; s < (n); ++s) { for (int i = 0; i < (n); ++i) kt[i] = (int)(g[i]).size() - 1; int td = todo; int cnt = 0; int mxwait = 0; int ps = s; while (td > 0 || mxwait > 0) { if (kt[ps] >= 0) { mxwait = std::max(mxwait, g[ps][kt[ps]] - ps + (g[ps][kt[ps]] < ps ? n : 0)); kt[ps]--; td--; } ps = (ps + 1) % n; mxwait = std::max(0, mxwait - 1); cnt++; } std::cout << cnt + add << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> d[5555]; int s[5555]; int main() { int n, m; cin >> n >> m; while (m--) { int a, b; cin >> a >> b; int len = b - a; if (len < 0) len += n; d[a - 1].push_back(len); } for (int i = 0; i < n; i++) { sort(d[i].rbegin(), d[i].rend()); if (d[i].size()) { s[i] = n * (d[i].size() - 1) + d[i].back(); } } for (int i = 0; i < n; i++) { long long int ans = 0; for (int j = 0; j < n; j++) { if (!s[j]) continue; int d = j - i; if (d < 0) d += n; ans = max(ans, 0ll + d + s[j]); } cout << ans << ' '; } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> struct rge { T b, e; }; template <class T> rge<T> range(T i, T j) { return rge<T>{i, j}; } struct debug { template <class T> debug& operator<<(const T&) { return *this; } }; using ll = long long; template <typename T> void min_self(T& a, T b) { a = min(a, b); } template <typename T> void max_self(T& a, T b) { a = max(a, b); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<vector<int>> g(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; --a, --b; g[a].push_back(b); } auto Dist = [&](int x, int y) { int d = y - x; if (d < 0) { d += n; } return d; }; for (int i = 0; i < n; i++) { sort(g[i].begin(), g[i].end(), [&](int x, int y) { return Dist(i, x) < Dist(i, y); }); } for (int i = 0; i < n; i++) { int ans = 0; for (int j = 0; j < n; j++) { if (g[j].empty()) { continue; } int cur = Dist(i, j); cur += n * ((int)g[j].size() - 1); cur += Dist(j, g[j][0]); max_self(ans, cur); } cout << ans << " "; } cout << "\n"; }
#include <bits/stdc++.h> using namespace std; template <class T> using vec = vector<T>; const long long inf = 1LL << 62; const long long mod = 1e9 + 7; const long long dx[8] = {1, 0, -1, 0, -1, 1, -1, 1}; const long long dy[8] = {0, 1, 0, -1, -1, -1, 1, 1}; const string alphabet = "abcdefghijklmnopqrstuvwxyz"; long long input() { long long tmp; cin >> tmp; return tmp; } string raw_input() { string tmp; cin >> tmp; return tmp; } template <class T> void printx(T n) { cout << n; } template <class T, class U> void printx(pair<T, U> p) { cout << "(" << p.first << "," << p.second << ")"; } template <class T, class U, class V> void printx(tuple<T, U, V> t) { cout << "{" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << "}" << endl; } template <class T> void printx(vector<T> v) { cout << "{"; for (long long i = 0; i < v.size(); i++) { printx(v[i]); if (i != v.size() - 1) printx(","); } cout << "}"; } template <class T> void print(T n) { printx(n); cout << endl; } template <class T> void print(set<T> s) { cout << "{"; for (auto(e) = s.begin(); (e) != s.end(); (e)++) { if (e != s.begin()) printx(","); printx(*e); } cout << "}" << endl; } template <class T, class U> void print(map<T, U> mp) { cout << "{"; for (auto(e) = mp.begin(); (e) != mp.end(); (e)++) { cout << "(" << e->first << "," << e->second << ")"; } cout << "}" << endl; } template <class T> void printans(vec<T> v) { for (long long i = 0; i < (long long)v.size(); i++) { cout << v[i] << (i == (long long)v.size() - 1 ? "" : " "); } cout << endl; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <class T> T cut(T &a, long long l, long long r) { return T(a.begin() + l, a.begin() + r); } long long n, m; vec<vector<long long> > candies(5010); void in() { cin >> n >> m; for (long long i = 0; i < m; i++) { long long f, t; cin >> f >> t; f--; t--; candies[f].push_back(t > f ? t - f : n - (f - t)); } for (long long i = 0; i < (long long)candies.size(); i++) { sort((candies[i]).begin(), (candies[i]).end()); } } void solve() { for (long long i = 0; i < n; i++) { long long ans = 0; for (long long j = 0; j < n; j++) { long long pos = (i + j) % n; long long cnt = (long long)candies[pos].size(); if (cnt == 0) continue; chmax(ans, j + candies[pos][0] + n * (cnt - 1)); } print(ans); } } signed main() { cin.tie(0); ios::sync_with_stdio(false); in(); solve(); }
#include <bits/stdc++.h> int main() { int n, m; scanf("%d %d", &n, &m); int ma[n], vu[n]; memset(ma, 0, sizeof ma); memset(vu, 0, sizeof vu); int po, de, ca; while (m--) { scanf("%d %d", &po, &de); po--; de--; if (de < po) de += n; ca = de - po; vu[po]++; if (ma[po] == 0 || ca < ma[po]) ma[po] = ca; } int i, j, j2, maxi; for (i = 0; i < n; i++) { maxi = 0; for (j = 0; j < n; j++) if (vu[j]) { j2 = j; if (j2 < i) j2 += n; ca = (vu[j] - 1) * n + ma[j] + j2 - i; if (ca > maxi) maxi = ca; } printf("%d", maxi); if (i < n - 1) printf(" "); else printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 20000 + 5; int A[N], B[N]; vector<int> adjA[N], adjB[N]; int F[N]; int n; inline int inc(int at, int d) { at += d; if (at == 0) at = n; if (at == n + 1) at = 1; return at; } inline int diff(int a, int b) { int d = b - a; if (d < 0) d += n; return d; } int main() { ios::sync_with_stdio(0); cin.tie(0); int m; cin >> n >> m; int mx = 0; for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; adjA[a].push_back(i), adjB[b].push_back(i); A[i] = a, B[i] = b; F[a]++; mx = max(mx, F[a]); } for (int i = 1; i <= n; i++) { int at = i; int ans = 0; while (1) { if (F[at] == 0 or F[at] < mx - 1) { at = inc(at, 1); if (at == i) break; continue; } int cnt = 1e9; for (auto it : adjA[at]) { if (F[at] == mx - 1) { if (diff(i, B[it]) + diff(B[it], A[it]) == diff(i, A[it])) { cnt = min(cnt, diff(i, B[it])); } else { cnt = 0; break; } } else { if (diff(i, B[it]) + diff(B[it], A[it]) == diff(i, A[it])) { cnt = min(cnt, n + diff(i, B[it])); } else { cnt = min(cnt, diff(i, B[it])); } } } ans = max(ans, cnt); at = inc(at, 1); if (at == i) break; } ans = (mx - 1) * n + ans; cout << ans << " "; } cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, Cnt[5000 + 5], Min[5000 + 5]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) Min[i] = n - 1; for (int i = 1, u, v; i <= m; i++) { scanf("%d%d", &u, &v); Cnt[u]++, Min[u] = min(Min[u], (v + n - u) % n); } for (int i = 1; i <= n; i++) { int res = 0; for (int j = 1; j <= n; j++) if (Cnt[j]) res = max(res, Min[j] + (Cnt[j] - 1) * n + (j + n - i) % n); printf("%d%c", res, i == n ? '\n' : ' '); } return 0; }
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); } vector<string> vec_splitter(string s) { s += ','; vector<string> res; while (!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) int idx, __attribute__((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } double get_time() { return 1.0 * clock() / CLOCKS_PER_SEC; } int main() { fast(); int n, q; cin >> n >> q; string s; cin >> s; vector<vector<int>> nxt(26, vector<int>(n + 2, n + 1)); for (int i = n - 1; i >= 0; i--) { nxt[s[i] - 'a'][i] = i; } for (int i = 0; i < 26; i++) { for (int j = n - 1; j >= 0; j--) nxt[i][j] = min(nxt[i][j], nxt[i][j + 1]); } vector<vector<vector<int>>> dp( 256, vector<vector<int>>(256, vector<int>(256, n + 1))); dp[0][0][0] = 0; vector<int> l(3); vector<string> t(3, ""); while (q--) { char ch, c; int idx; cin >> ch >> idx; idx--; if (ch == '+') { cin >> c; l[idx]++; t[idx] += c; } 42; int lim0 = (idx == 0 ? l[0] : 0); int lim1 = (idx == 1 ? l[1] : 0); int lim2 = (idx == 2 ? l[2] : 0); for (int i = lim0; i <= l[0]; i++) { for (int j = lim1; j <= l[1]; j++) { for (int k = lim2; k <= l[2]; k++) { dp[i][j][k] = n + 1; if (ch == '+') { if (i > 0) dp[i][j][k] = min(dp[i][j][k], nxt[t[0][i - 1] - 'a'][dp[i - 1][j][k]] + 1); if (j > 0) dp[i][j][k] = min(dp[i][j][k], nxt[t[1][j - 1] - 'a'][dp[i][j - 1][k]] + 1); if (k > 0) dp[i][j][k] = min(dp[i][j][k], nxt[t[2][k - 1] - 'a'][dp[i][j][k - 1]] + 1); } } } } if (ch == '-') { l[idx]--; t[idx] = t[idx].substr(0, (int)t[idx].size() - 1); } if (dp[l[0]][l[1]][l[2]] < n + 1) cout << "YES" << '\n'; else cout << "NO" << '\n'; } }
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) using namespace std; const int N = 1e5 + 5; char s[N]; int nxt[N][26], n, q, x; char op[10], cc[10]; int dp[255][255][255]; string a, b, c; int main() { scanf("%d %d", &n, &q); scanf("%s", s + 1); for (int i = 0; i < 26; ++i) nxt[n + 1][i] = n + 1, nxt[n + 2][i] = n + 1; for (int i = n; i >= 1; --i) { for (int j = 0; j < 26; ++j) { if (j == s[i] - 'a') nxt[i][j] = i; else nxt[i][j] = nxt[i + 1][j]; } } while (q--) { scanf("%s %d", op, &x); if (op[0] == '+') { scanf("%s", cc); if (x == 1) { a.push_back(cc[0]); for (int i = 0; i <= b.size(); ++i) { for (int j = 0; j <= c.size(); ++j) { dp[a.size()][i][j] = n + 1; if (a.size() > 0) dp[a.size()][i][j] = min(dp[a.size()][i][j], nxt[dp[a.size() - 1][i][j] + 1][a.back() - 'a']); if (i > 0) dp[a.size()][i][j] = min(dp[a.size()][i][j], nxt[dp[a.size()][i - 1][j] + 1][b[i - 1] - 'a']); if (j > 0) dp[a.size()][i][j] = min(dp[a.size()][i][j], nxt[dp[a.size()][i][j - 1] + 1][c[j - 1] - 'a']); } } } else if (x == 2) { b.push_back(cc[0]); for (int i = 0; i <= a.size(); ++i) { for (int j = 0; j <= c.size(); ++j) { dp[i][b.size()][j] = n + 1; if (b.size() > 0) dp[i][b.size()][j] = min(dp[i][b.size()][j], nxt[dp[i][b.size() - 1][j] + 1][b.back() - 'a']); if (i > 0) dp[i][b.size()][j] = min(dp[i][b.size()][j], nxt[dp[i - 1][b.size()][j] + 1][a[i - 1] - 'a']); if (j > 0) dp[i][b.size()][j] = min(dp[i][b.size()][j], nxt[dp[i][b.size()][j - 1] + 1][c[j - 1] - 'a']); } } } else if (x == 3) { c.push_back(cc[0]); for (int i = 0; i <= a.size(); ++i) { for (int j = 0; j <= b.size(); ++j) { dp[i][j][c.size()] = n + 1; if (c.size() > 0) dp[i][j][c.size()] = min(dp[i][j][c.size()], nxt[dp[i][j][c.size() - 1] + 1][c.back() - 'a']); if (i > 0) dp[i][j][c.size()] = min(dp[i][j][c.size()], nxt[dp[i - 1][j][c.size()] + 1][a[i - 1] - 'a']); if (j > 0) dp[i][j][c.size()] = min(dp[i][j][c.size()], nxt[dp[i][j - 1][c.size()] + 1][b[j - 1] - 'a']); } } } } else { if (x == 1) a.pop_back(); else if (x == 2) b.pop_back(); else if (x == 3) c.pop_back(); } if (dp[a.size()][b.size()][c.size()] > n) printf("NO\n"); else printf("YES\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 7; const long long L = 256; const long long INF = 1e9 + 7; const long long C = 26; long long dp[L][L][L]; string a[3]; long long mn[3]; long long to[N][C]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); for (long long i = 0; i < L; ++i) { for (long long j = 0; j < L; ++j) { for (long long k = 0; k < L; ++k) { dp[i][j][k] = INF; } } } dp[0][0][0] = 0; long long n, q; cin >> n >> q; string s; cin >> s; for (long long i = 0; i < C; ++i) { to[n][i] = INF; } for (long long i = n - 1; i >= 0; --i) { for (long long j = 0; j < C; ++j) { to[i][j] = to[i + 1][j]; } to[i][s[i] - 'a'] = i; } while (q--) { char t; cin >> t; if (t == '+') { long long i; char c; cin >> i >> c; --i; a[i] += c; for (long long t = 0; t < 3; ++t) { mn[t] = 0; } mn[i] = a[i].size(); for (long long p0 = mn[0]; p0 <= a[0].size(); ++p0) { for (long long p1 = mn[1]; p1 <= a[1].size(); ++p1) { for (long long p2 = mn[2]; p2 <= a[2].size(); ++p2) { if (p0 && dp[p0 - 1][p1][p2] != INF) { long long pos = to[dp[p0 - 1][p1][p2]][a[0][p0 - 1] - 'a']; if (pos != INF) { dp[p0][p1][p2] = min(dp[p0][p1][p2], pos + 1); } } if (p1 && dp[p0][p1 - 1][p2] != INF) { long long pos = to[dp[p0][p1 - 1][p2]][a[1][p1 - 1] - 'a']; if (pos != INF) { dp[p0][p1][p2] = min(dp[p0][p1][p2], pos + 1); } } if (p2 && dp[p0][p1][p2 - 1] != INF) { long long pos = to[dp[p0][p1][p2 - 1]][a[2][p2 - 1] - 'a']; if (pos != INF) { dp[p0][p1][p2] = min(dp[p0][p1][p2], pos + 1); } } } } } } else { long long i; cin >> i; --i; for (long long t = 0; t < 3; ++t) { mn[t] = 0; } mn[i] = a[i].size(); for (long long p0 = mn[0]; p0 <= a[0].size(); ++p0) { for (long long p1 = mn[1]; p1 <= a[1].size(); ++p1) { for (long long p2 = mn[2]; p2 <= a[2].size(); ++p2) { dp[p0][p1][p2] = INF; } } } a[i].pop_back(); } if (dp[a[0].size()][a[1].size()][a[2].size()] == INF) { cout << "NO\n"; } else { cout << "YES\n"; } } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; string s1, s2, s3; int n, q, nxt[MAXN][26], lst[26], dp[255][255][255]; char s[MAXN], opt[5], c[5]; int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); scanf("%d %d", &n, &q); scanf("%s", s + 1); for (int j = 0; j < 26; j++) nxt[n + 1][j] = lst[j] = n + 1; for (int i = n; i >= 0; i--) { for (int j = 0; j < 26; j++) nxt[i][j] = lst[j]; if (i) lst[s[i] - 'a'] = i; } for (int i = 0; i <= 250; i++) for (int j = 0; j <= 250; j++) for (int k = 0; k <= 250; k++) dp[i][j][k] = n + 1; dp[0][0][0] = 0; s1 = s2 = s3 = ""; for (int task = 1; task <= q; task++) { int x; scanf("%s %d", opt + 1, &x); if (opt[1] == '-') { if (x == 1) s1.pop_back(); if (x == 2) s2.pop_back(); if (x == 3) s3.pop_back(); } else { scanf("%s", c + 1); if (x == 1) { s1 += c[1]; int i = s1.size(); for (int j = 0; j <= s2.size(); j++) for (int k = 0; k <= s3.size(); k++) { dp[i][j][k] = n + 1; if (i) dp[i][j][k] = min(dp[i][j][k], nxt[dp[i - 1][j][k]][s1[i - 1] - 'a']); if (j) dp[i][j][k] = min(dp[i][j][k], nxt[dp[i][j - 1][k]][s2[j - 1] - 'a']); if (k) dp[i][j][k] = min(dp[i][j][k], nxt[dp[i][j][k - 1]][s3[k - 1] - 'a']); } } if (x == 2) { s2 += c[1]; int j = s2.size(); for (int i = 0; i <= s1.size(); i++) for (int k = 0; k <= s3.size(); k++) { dp[i][j][k] = n + 1; if (i) dp[i][j][k] = min(dp[i][j][k], nxt[dp[i - 1][j][k]][s1[i - 1] - 'a']); if (j) dp[i][j][k] = min(dp[i][j][k], nxt[dp[i][j - 1][k]][s2[j - 1] - 'a']); if (k) dp[i][j][k] = min(dp[i][j][k], nxt[dp[i][j][k - 1]][s3[k - 1] - 'a']); } } if (x == 3) { s3 += c[1]; int k = s3.size(); for (int i = 0; i <= s1.size(); i++) for (int j = 0; j <= s2.size(); j++) { dp[i][j][k] = n + 1; if (i) dp[i][j][k] = min(dp[i][j][k], nxt[dp[i - 1][j][k]][s1[i - 1] - 'a']); if (j) dp[i][j][k] = min(dp[i][j][k], nxt[dp[i][j - 1][k]][s2[j - 1] - 'a']); if (k) dp[i][j][k] = min(dp[i][j][k], nxt[dp[i][j][k - 1]][s3[k - 1] - 'a']); } } } puts(dp[s1.size()][s2.size()][s3.size()] <= n ? "YES" : "NO"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); template <class T, class S> inline void add(T& a, S b) { a += b; if (a >= mod) a -= mod; } template <class T, class S> inline void sub(T& a, S b) { a -= b; if (a < 0) a += mod; } template <class T, class S> inline bool chkmax(T& a, S b) { return a < b ? a = b, true : false; } template <class T, class S> inline bool chkmin(T& a, S b) { return a > b ? a = b, true : false; } int n, q, who; int dp[256][256][256]; int len[3], nex[N][26]; char s[N], op[3], t[3][N]; inline int getNex(int p, char c) { return nex[p][c - 'a']; } int main() { scanf("%d%d", &n, &q); scanf("%s", s + 1); for (int c = 0; c < 26; c++) nex[n][c] = n + 1; for (int i = n - 1; i >= 0; i--) { for (int c = 0; c < 26; c++) nex[i][c] = nex[i + 1][c]; nex[i][s[i + 1] - 'a'] = i + 1; } for (int i = 0; i < 256; i++) for (int j = 0; j < 256; j++) for (int k = 0; k < 256; k++) dp[i][j][k] = n + 1; dp[0][0][0] = 0; while (q--) { scanf("%s%d", op, &who); who--; if (op[0] == '+') { scanf("%s", op); char ch = op[0]; t[who][++len[who]] = ch; if (who == 0) { for (int y = 0; y <= len[1]; y++) for (int z = 0; z <= len[2]; z++) dp[len[who]][y][z] = n + 1; for (int x = len[0] - 1; x <= len[0]; x++) { for (int y = 0; y <= len[1]; y++) { for (int z = 0; z <= len[2]; z++) { if (dp[x][y][z] >= n) continue; if (x < len[0]) chkmin(dp[x + 1][y][z], getNex(dp[x][y][z], t[0][x + 1])); if (y < len[1]) chkmin(dp[x][y + 1][z], getNex(dp[x][y][z], t[1][y + 1])); if (z < len[2]) chkmin(dp[x][y][z + 1], getNex(dp[x][y][z], t[2][z + 1])); } } } } else if (who == 1) { for (int x = 0; x <= len[0]; x++) for (int z = 0; z <= len[2]; z++) dp[x][len[who]][z] = n + 1; for (int x = 0; x <= len[0]; x++) { for (int y = len[1] - 1; y <= len[1]; y++) { for (int z = 0; z <= len[2]; z++) { if (dp[x][y][z] >= n) continue; if (x < len[0]) chkmin(dp[x + 1][y][z], getNex(dp[x][y][z], t[0][x + 1])); if (y < len[1]) chkmin(dp[x][y + 1][z], getNex(dp[x][y][z], t[1][y + 1])); if (z < len[2]) chkmin(dp[x][y][z + 1], getNex(dp[x][y][z], t[2][z + 1])); } } } } else if (who == 2) { for (int x = 0; x <= len[0]; x++) for (int y = 0; y <= len[1]; y++) dp[x][y][len[who]] = n + 1; for (int x = 0; x <= len[0]; x++) { for (int y = 0; y <= len[1]; y++) { for (int z = len[2] - 1; z <= len[2]; z++) { if (dp[x][y][z] >= n) continue; if (x < len[0]) chkmin(dp[x + 1][y][z], getNex(dp[x][y][z], t[0][x + 1])); if (y < len[1]) chkmin(dp[x][y + 1][z], getNex(dp[x][y][z], t[1][y + 1])); if (z < len[2]) chkmin(dp[x][y][z + 1], getNex(dp[x][y][z], t[2][z + 1])); } } } } } else { len[who]--; } puts(dp[len[0]][len[1]][len[2]] <= n ? "YES" : "NO"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int infi = 1e9 + 7; const long long infl = (long long)1e18 + (long long)7; int dp[255][255][255]; int nxt[100500][26]; void upd(int& a, int n, int b, char c) { if (b >= n) return; a = min(a, nxt[b][c - 'a'] + 1); } int32_t main() { cin.sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; string s; cin >> s; for (int i = 0; i < (26); ++i) nxt[n][i] = infi; for (int i = n - 1; i >= 0; --i) { for (int j = 0; j < (26); ++j) nxt[i][j] = nxt[i + 1][j]; nxt[i][s[i] - 'a'] = i; } string tmp[3]; for (int i = 0; i < (251); ++i) for (int j = 0; j < (251); ++j) for (int k = 0; k < (251); ++k) dp[i][j][k] = infi; dp[0][0][0] = 0; for (int qwe = 0; qwe < (q); ++qwe) { char t, c; int num; cin >> t >> num; --num; if (t == '-') { tmp[num].pop_back(); int len = dp[tmp[0].size()][tmp[1].size()][tmp[2].size()]; if (len <= n) { cout << "YES\n"; } else { cout << "NO\n"; } continue; } cin >> c; tmp[num] += c; int k = tmp[num].size(); if (num == 0) { for (int i = 0; i < (tmp[1].size() + 1); ++i) { for (int j = 0; j < (tmp[2].size() + 1); ++j) { dp[k][i][j] = infi; upd(dp[k][i][j], n, dp[k - 1][i][j], tmp[0][k - 1]); if (i) upd(dp[k][i][j], n, dp[k][i - 1][j], tmp[1][i - 1]); if (j) upd(dp[k][i][j], n, dp[k][i][j - 1], tmp[2][j - 1]); } } } else if (num == 1) { for (int i = 0; i < (tmp[0].size() + 1); ++i) { for (int j = 0; j < (tmp[2].size() + 1); ++j) { dp[i][k][j] = infi; upd(dp[i][k][j], n, dp[i][k - 1][j], tmp[1][k - 1]); if (i) upd(dp[i][k][j], n, dp[i - 1][k][j], tmp[0][i - 1]); if (j) upd(dp[i][k][j], n, dp[i][k][j - 1], tmp[2][j - 1]); } } } else { for (int i = 0; i < (tmp[0].size() + 1); ++i) { for (int j = 0; j < (tmp[1].size() + 1); ++j) { dp[i][j][k] = infi; upd(dp[i][j][k], n, dp[i][j][k - 1], tmp[2][k - 1]); if (i) upd(dp[i][j][k], n, dp[i - 1][j][k], tmp[0][i - 1]); if (j) upd(dp[i][j][k], n, dp[i][j - 1][k], tmp[1][j - 1]); } } } int len = dp[tmp[0].size()][tmp[1].size()][tmp[2].size()]; if (len <= n) { cout << "YES\n"; } else { cout << "NO\n"; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f; char s[maxn], op[5], t[5]; int ne[maxn][30], f[300][300][300]; string a[5]; void init(int n) { for (int i = 0; i < 26; ++i) ne[n + 1][i] = ne[n][i] = n + 1; for (int i = n - 1; i >= 0; --i) { for (int j = 0; j < 26; ++j) ne[i][j] = ne[i + 1][j]; ne[i][s[i + 1] - 'a'] = i + 1; } } inline void upd(int i, int j, int k) { f[i][j][k] = INF; if (i) f[i][j][k] = min(f[i][j][k], ne[f[i - 1][j][k]][a[1][i - 1] - 'a']); if (j) f[i][j][k] = min(f[i][j][k], ne[f[i][j - 1][k]][a[2][j - 1] - 'a']); if (k) f[i][j][k] = min(f[i][j][k], ne[f[i][j][k - 1]][a[3][k - 1] - 'a']); } int main() { int n, q; cin >> n >> q; scanf("%s", s + 1); init(n); while (q--) { int id; scanf("%s%d", t, &id); if (t[0] == '+') { scanf("%s", t); a[id].push_back(t[0]); if (id == 1) { for (int i = 0; i <= int(a[2].size()); ++i) for (int j = 0; j <= int(a[3].size()); ++j) upd(int(a[id].size()), i, j); } else if (id == 2) { for (int i = 0; i <= int(a[1].size()); ++i) for (int j = 0; j <= int(a[3].size()); ++j) upd(i, int(a[id].size()), j); } else { for (int i = 0; i <= int(a[1].size()); ++i) for (int j = 0; j <= int(a[2].size()); ++j) upd(i, j, int(a[id].size())); } } else a[id].pop_back(); if (f[int(a[1].size())][int(a[2].size())][int(a[3].size())] <= n) printf("YES\n"); else printf("NO\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; char s[N]; vector<int> p[26]; vector<int> rel[4]; int dp[255][255][255]; int get(int t, int v) { auto it = upper_bound(p[v].begin(), p[v].end(), t); if (it == p[v].end()) return 0x3f3f3f3f; else return *it; } int main() { int n, q; scanf("%d%d", &n, &q); scanf(" %s", s + 1); for (int i = 1; i <= n; i++) p[s[i] - 'a'].push_back(i); memset(dp, 0x3f, sizeof dp); dp[0][0][0] = 0; while (q--) { char op, c; int id; scanf(" %c%d", &op, &id); if (op == '+') { scanf(" %c", &c); rel[id].push_back(c - 'a'); int x = rel[1].size(), y = rel[2].size(), z = rel[3].size(); if (id == 1) { for (int i = 0; i <= y; i++) for (int j = 0; j <= z; j++) { int mi = 0x3f3f3f3f; mi = min(mi, get(dp[x - 1][i][j], rel[1][x - 1])); if (i) mi = min(mi, get(dp[x][i - 1][j], rel[2][i - 1])); if (j) mi = min(mi, get(dp[x][i][j - 1], rel[3][j - 1])); dp[x][i][j] = mi; } } else if (id == 2) { for (int i = 0; i <= x; i++) for (int j = 0; j <= z; j++) { int mi = 0x3f3f3f3f; if (i) mi = min(mi, get(dp[i - 1][y][j], rel[1][i - 1])); mi = min(mi, get(dp[i][y - 1][j], rel[2][y - 1])); if (j) mi = min(mi, get(dp[i][y][j - 1], rel[3][j - 1])); dp[i][y][j] = mi; } } else { for (int i = 0; i <= x; i++) for (int j = 0; j <= y; j++) { int mi = 0x3f3f3f3f; if (i) mi = min(mi, get(dp[i - 1][j][z], rel[1][i - 1])); if (j) mi = min(mi, get(dp[i][j - 1][z], rel[2][j - 1])); mi = min(mi, get(dp[i][j][z - 1], rel[3][z - 1])); dp[i][j][z] = mi; } } } else { int x = rel[1].size(), y = rel[2].size(), z = rel[3].size(); if (id == 1) { for (int i = 0; i <= y; i++) for (int j = 0; j <= z; j++) dp[x][i][j] = 0x3f3f3f3f; } else if (id == 2) { for (int i = 0; i <= x; i++) for (int j = 0; j <= z; j++) dp[i][y][j] = 0x3f3f3f3f; } else { for (int i = 0; i <= x; i++) for (int j = 0; j <= y; j++) dp[i][j][z] = 0x3f3f3f3f; } rel[id].pop_back(); } int x = rel[1].size(), y = rel[2].size(), z = rel[3].size(); if (dp[x][y][z] <= n) printf("YES\n"); else printf("NO\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; void recalc(const vector<char>& a, const vector<char>& b, const vector<char>& c, vector<vector<vector<long long> > >& dp, const vector<vector<long long> >& next, long long st1, long long st2, long long st3) { dp[0][0][0] = -1; for (long long i = st1; i <= a.size(); i++) { for (long long j = st2; j <= b.size(); j++) { for (long long k = st3; k <= c.size(); k++) { if (i + j + k == 0) continue; long long x = 1e10; if (i != 0 && dp[i - 1][j][k] < 1e7) x = max(dp[i - 1][j][k], next[dp[i - 1][j][k] + 1][a[i - 1] - 'a']); long long y = 1e10; if (j != 0 && dp[i][j - 1][k] < 1e7) y = max(dp[i][j - 1][k], next[dp[i][j - 1][k] + 1][b[j - 1] - 'a']); long long z = 1e10; if (k != 0 && dp[i][j][k - 1] < 1e7) z = max(dp[i][j][k - 1], next[dp[i][j][k - 1] + 1][c[k - 1] - 'a']); dp[i][j][k] = min(x, min(y, z)); } } } } int32_t main() { std::ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long n, q; cin >> n >> q; string s; cin >> s; vector<vector<long long> > next(n + 1, vector<long long>(26, 1e9)); vector<long long> now(26, 1e10); for (long long i = n - 1; i >= 0; i--) { now[s[i] - 'a'] = i; for (long long j = 0; j < 26; j++) next[i][j] = now[j]; } vector<vector<vector<long long> > > dp( 255, vector<vector<long long> >(255, vector<long long>(255, 1e9))); dp[0][0][0] = 0; vector<char> a, b, c; while (q--) { char req; cin >> req; if (req == '+') { long long pos; cin >> pos; char kek; cin >> kek; if (pos == 1) a.push_back(kek); else if (pos == 2) b.push_back(kek); else c.push_back(kek); long long st1 = a.size(); long long st2 = b.size(); long long st3 = c.size(); if (pos == 1) { st2 = 0; st3 = 0; } if (pos == 2) { st1 = 0; st3 = 0; } if (pos == 3) { st1 = 0; st2 = 0; } recalc(a, b, c, dp, next, st1, st2, st3); } else { long long pos; cin >> pos; if (pos == 1) a.pop_back(); else if (pos == 2) b.pop_back(); else c.pop_back(); } if (dp[a.size()][b.size()][c.size()] > 1e7) cout << "NO\n"; else cout << "YES\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; char s[100005], opt[5]; int f[255][255][255]; int p[30], q[100005][30]; int st[5]; int g[5][255]; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } inline void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) write(x / 10); putchar(x % 10 + '0'); } int main() { n = read(), m = read(); scanf("%s", s + 1); for (int i = 0; i < 26; i++) { p[i] = n + 1; q[n + 1][i] = n + 1; } for (int i = n; i >= 1; i--) { for (int j = 0; j < 26; j++) q[i][j] = p[j]; p[s[i] - 'a'] = i; } for (int i = 0; i < 26; i++) q[0][i] = p[i]; while (m--) { scanf("%s", opt + 1); int a; a = read(); if (opt[1] == '+') { scanf("%s", opt + 1); g[a][++st[a]] = opt[1] - 'a'; int mi = 0, mj = 0, mk = 0; int u = st[1], v = st[2], w = st[3]; if (a == 1) mi = u; if (a == 2) mj = v; if (a == 3) mk = w; for (int i = mi; i <= u; i++) for (int j = mj; j <= v; j++) for (int k = mk; k <= w; k++) f[i][j][k] = n + 1; for (int i = mi; i <= u; i++) for (int j = mj; j <= v; j++) for (int k = mk; k <= w; k++) { if (i) f[i][j][k] = min(q[f[i - 1][j][k]][g[1][i]], f[i][j][k]); if (j) f[i][j][k] = min(q[f[i][j - 1][k]][g[2][j]], f[i][j][k]); if (k) f[i][j][k] = min(q[f[i][j][k - 1]][g[3][k]], f[i][j][k]); } } else st[a]--; if (f[st[1]][st[2]][st[3]] <= n) printf("YES\n"); else printf("NO\n"); } return 0; }