intmain() { cin >> T; while (T--) { cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; int res = 0, sum = 0; for (int i = 0; i < n; i++) { if (a[i] >= k) sum += a[i]; elseif (!a[i] && sum) res++, sum--; } cout << res << endl; }
boolcheck(int x) { int res = 0; bool flag = false; for (int i = 0; i < n; i++) { if (a[i] == maxv && !flag) { flag = true; continue; } if (a[i] < (sum + x + n * 2 - 1) / (n * 2)) res++; } return res > n / 2; }
signedmain() { cin >> T; while (T--) { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sum = maxv = 0; for (int i = 0; i < n; i++) { sum += a[i]; maxv = max(maxv, a[i]); } int l = 0, r = 1e15; while (l < r) { int mid = l + r >> 1; if (!check(mid)) l = mid + 1; else r = mid; } if (check(l)) cout << l << endl; else cout << -1 << endl; }
intmain() { cin >> T; while (T--) { L.clear(), R.clear(); cin >> n >> d >> k; for (int i = 1; i <= k; i++) { int l, r; cin >> l >> r; L[l]++, R[r]++; } int sum = 0, maxv = 0, minv = 1e9, r1, r2; for (int i = 1; i <= n; i++) { sum += L[i]; if (i < d) continue; if (i > d) sum -= R[i - d]; if (maxv < sum) maxv = sum, r1 = i - d + 1; if (minv > sum) minv = sum, r2 = i - d + 1; } cout << r1 << ' ' << r2 << endl; }
#define int long long #define x first #define y second
usingnamespace std;
typedef pair<int, int> PII;
constint N = 200010;
int T; int n, c; int a[N]; vector<int> h[N]; int f[N][2];
voiddfs(int u, int father) { f[u][0] = 0, f[u][1] = a[u]; for (int i = 0; i < h[u].size(); i++) { int v = h[u][i]; if (v == father) continue; dfs(v, u); f[u][0] += max(f[v][0], f[v][1]); f[u][1] += max(f[v][0], f[v][1] - c * 2); } }
signedmain() { cin >> T; while (T--) { cin >> n >> c; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; h[u].push_back(v); h[v].push_back(u); } dfs(1, 0); cout << max(f[1][0], f[1][1]) << endl; for (int i = 1; i <= n; i++) h[i].clear(); }
int T; int n, q; int len, sum; int a[N], b[N], c[N]; int res[N];
boolcmp(Q A, Q B) { if ((A.l + len - 1) / len != (B.l + len - 1) / len) return A.l < B.l; if ((A.l + len - 1) / len & 1) return A.r < B.r; return A.r > B.r; }
intmain() { cin >> T; while (T--) { cin >> n >> q; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; qry[i] = {i, l, r}; } for (int i = 1; i <= n; i++) b[i] = a[i]; sort(b + 1, b + 1 + n); int bn = unique(b + 1, b + 1 + n) - b - 1; for (int i = 1; i <= n; i++) { int l = 1, r = bn; while (l < r) { int mid = l + r >> 1; if (b[mid] < a[i]) l = mid + 1; else r = mid; } a[i] = l; } len = sqrt(n); sort(qry + 1, qry + q + 1, cmp); sum = 0; for (int i = 1, l = 1, r = 0; i <= q; i++) { while (l > qry[i].l) change(a[--l]); while (r < qry[i].r) change(a[++r]); while (l < qry[i].l) change(a[l++]); while (r > qry[i].r) change(a[r--]); res[qry[i].id] = sum; } for (int i = 1; i <= q; i++) { if (res[i]) cout << "NO" << endl; else cout << "YES" << endl; } for (int i = 1; i <= bn; i++) c[i] = 0; }