voidsolve(){ int _; cin >> _; for (int ts = 0; ts < _; ++ts) { int n; cin >> n; int p = 1, zero = 0, mi = INT_MAX; for (int i = 0; i < n; ++i) { int tmp; cin >> tmp; p *= (tmp == 0 ? 1 : tmp); zero += tmp == 0; mi = min(mi, tmp); }
voidsolve(){ int _; cin >> _; for (int ts = 0; ts < _; ++ts) { int n, k; cin >> n >> k; string str; str.reserve(n); cin >> str; int last = -k - 1, ans = 0; for (int i = 0; i < n; ++i) { if (str[i] == 'W') continue; if (i - last + 1 <= k) continue; last = i; ans++; } cout << ans << endl; } }
voidsolve(){ int _; cin >> _; for (int ts = 0; ts < _; ++ts) { int n, x; cin >> n >> x; vector<int> data(n); for (auto &item: data) cin >> item; int l = 0, r = 2e9 + 10LL; while (l + 1 < r) { int mid = (l + r) >> 1; int sum = 0; for (auto &item: data) sum += item >= mid ? 0 : mid - item; if (sum > x) r = mid; else l = mid; } cout << l << endl; } }
voidsolve(){ int _; cin >> _; for (int ts = 0; ts < _; ++ts) { int n, k, ans = 0; cin >> n >> k; vector<int> a(n), h(n); for (auto &item: a) cin >> item; for (auto &item: h) cin >> item; for (auto &item: a) if (item <= k) ans = 1; int l = 0, r = n + 1; while (l + 1 < r) { int mid = (l + r) >> 1; if (mid == 1) { if (ans >= 1) l = mid; else r = mid; continue; } int x = 0, y = 0, sum = 0; bool flag = false;
auto findNext = [&]() { x = y; y += 1; sum = a[x]; while (y - x != mid && x < n && y < n) { if (h[y - 1] % h[y] != 0) { x = y; y += 1; sum = a[x]; } else { sum += a[y]; y++; } }
if (y - x == mid && sum <= k) { ans = max(ans, mid); flag = true; } return y - x == mid; }; auto move = [&]() { if (y == n) returnfalse; if (h[y - 1] % h[y] != 0) returnfalse; sum -= a[x]; sum += a[y]; y++; x++; if (sum <= k) { ans = max(ans, mid); flag = true; } returntrue; };
while (findNext()) while (move()); if (flag) l = mid; else r = mid; }
voidsolve(){ int _; cin >> _; for (int ts = 0; ts < _; ++ts) { int n, a, b; cin >> n >> a >> b; vector<int> deg(n + 1, 0); vector<bool> vis(n + 1, false); structnode { int v, n; }; vector<node> edge(n * 2); vector<int> head(n + 1, -1); for (int i = 0; i < n; ++i) { int u, v; cin >> u >> v; edge[i << 1] = {v, head[u]}; edge[(i << 1) | 1] = {u, head[v]}; head[u] = i << 1; head[v] = (i << 1) | 1; deg[u]++; deg[v]++; }
// find circle queue<int> q; for (int i = 1; i <= n; ++i) if (deg[i] == 1) q.push(i); while (!q.empty()) { int cur = q.front(); q.pop(); vis[cur] = true; for (int i = head[cur]; i != -1; i = edge[i].n) if ((--deg[edge[i].v]) == 1 && !vis[edge[i].v]) q.push(edge[i].v); }
// begin for b run away int cost1, cost2 = INT_MAX, target; queue<pair<int, int>> qs; vector<bool> flag(n + 1, false); qs.emplace(b, 0); flag[b] = true; while (!qs.empty()) { auto cur = qs.front(); qs.pop(); if (!vis[cur.first]) { cost1 = cur.second; target = cur.first; break; } for (int i = head[cur.first]; i != -1; i = edge[i].n) if (!flag[edge[i].v]) { qs.emplace(edge[i].v, cur.second + 1); flag[edge[i].v] = true; } }
while (!qs.empty()) qs.pop(); for (int i = 0; i <= n; ++i) flag[i] = false;
qs.emplace(a, 0); flag[a] = true; while (!qs.empty()) { auto cur = qs.front(); qs.pop(); if (cur.first == target) { cost2 = cur.second; break; } for (int i = head[cur.first]; i != -1; i = edge[i].n) if (!flag[edge[i].v]) { qs.emplace(edge[i].v, cur.second + 1); flag[edge[i].v] = true; } }