boolbeat(char s, char t){ if (s == 'R' && t == 'S') return1; if (s == 'S' && t == 'P') return1; if (s == 'P' && t == 'R') return1; return0; }
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); int T; cin >> T; while (T--) { string s; cin >> s; stack<int> sk; int n = s.size(); for (int i = 0; i < n; ++i) { while (!sk.empty() && !beat(s[sk.top()], s[i])) sk.pop(); sk.push(i); } int ans = 0; while (!sk.empty()) ans = sk.top(), sk.pop(); cout << s[ans] << '\n'; }
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to) #define dbg(...) fprintf(stderr, __VA_ARGS__) #define fi first #define se second
using ll = longlong; using ull = unsignedlonglong; using pii = pair<int, int>;
template <typename A, typename B> boolsmax(A &a, const B &b){ return a < b ? a = b, 1 : 0; } template <typename A, typename B> boolsmin(A &a, const B &b){ return b < a ? a = b, 1 : 0; }
constexprint N = 2e5 + 7; constexprint M = 13; constexpr ll INF = LLONG_MAX / 2;
int n, k; int a[N], b[N]; ll dp[N][M][M]; bool vis[N][M][M];
ll dfs(int x, int c1, int c2){ if (x == n * 2 + 1) return0; if (vis[x][c1][c2]) return dp[x][c1][c2]; vis[x][c1][c2] = 1; ll &ans = dp[x][c1][c2]; int r1 = x / 2, r2 = (x - 1) / 2; int ban1 = r2 - c2, ban2 = r1 - c1; int p1 = c1 + ban1 + 1, p2 = c2 + ban2 + 1; if (x & 1) ans = max(p2 <= n ? -dfs(x + 1, c1, c2) : -INF, p1 <= n && c1 < k ? a[p1] - dfs(x + 1, c1 + 1, c2) : -INF); else ans = max(p1 <= n ? -dfs(x + 1, c1, c2) : -INF, p2 <= n && c2 < k ? b[p2] - dfs(x + 1, c1, c2 + 1) : -INF); return ans; }
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) cin >> b[i]; sort(a + 1, a + n + 1, greater<int>()); sort(b + 1, b + n + 1, greater<int>());
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to) #define dbg(...) fprintf(stderr, __VA_ARGS__) #define fi first #define se second
using ll = longlong; using ull = unsignedlonglong; using pii = pair<int, int>;
template <typename A, typename B> boolsmax(A &a, const B &b){ return a < b ? a = b, 1 : 0; } template <typename A, typename B> boolsmin(A &a, const B &b){ return b < a ? a = b, 1 : 0; }
constexprint N = 1e5 + 7; constexprint M = 460 + 7; constexpr ll INF = LLONG_MAX; constexprint P = 998244353;
int n, m, q, blo; int deg[N], a[N], b[N]; ll dp[N]; bool mk[N]; vector<pii> g[N];
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n >> m >> q; blo = sqrt(m); for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1, x, y, z; i <= m; ++i) cin >> x >> y >> z, g[x].emplace_back(y, z), g[y].emplace_back(x, z), ++deg[x], ++deg[y]; for (int i = q; i; --i) cin >> b[i];
for (int i = 1; i <= n; ++i) if (deg[i] > blo) nth_element(g[i].begin(), g[i].begin() + blo, g[i].end(), [](const pii &x, const pii &y) { return dp[x.fi] + x.se < dp[y.fi] + y.se; }); for (int i = 1; i <= q; ++i) { int x = b[i]; dp[x] = INF; for (int i = 0; i < min(blo, deg[x]); ++i) smin(dp[x], dp[g[x][i].fi] + g[x][i].se); if (i % blo == 0) { for (int i = 1; i <= n; ++i) if (deg[i] > blo) nth_element(g[i].begin(), g[i].begin() + blo, g[i].end(), [](const pii &x, const pii &y) { return dp[x.fi] + x.se < dp[y.fi] + y.se; }); } }
int ans = 0; for (int i = 1; i <= n; ++i) ans = (ans + a[i] * dp[i]) % P; cout << ans << endl; return0; }
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to) #define dbg(...) fprintf(stderr, __VA_ARGS__) #define fi first #define se second
using ll = longlong; using ull = unsignedlonglong; using pii = pair<int, int>;
template <typename A, typename B> boolsmax(A &a, const B &b){ return a < b ? a = b, 1 : 0; } template <typename A, typename B> boolsmin(A &a, const B &b){ return b < a ? a = b, 1 : 0; }
constexprint N = 1e6 + 7; constexprdouble eps = 1e-10;
int n; pair<double, ll> a[N]; ll pres[N], sufs[N];
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i].fi >> a[i].se;
sort(a + 1, a + n + 1, greater<pair<double, int>>()); int cnt = 0; for (int i = 1; i <= n; ++i) if (i == 1 || fabs(a[i].fi - a[i - 1].fi) > eps) a[++cnt] = a[i]; else a[cnt].se += a[i].se; n = cnt; for (int i = 1; i <= n; ++i) pres[i] = pres[i - 1] + a[i].se; for (int i = n; i; --i) sufs[i] = sufs[i + 1] + a[i].se; double ans = 0; for (int i = 1, p = n; i <= n; ++i) if (i == n || fabs(a[i].fi - a[i + 1].fi) > eps) { while (p > i && pres[i] / a[i].fi > sufs[p] / (1 - a[p].fi)) --p; smax(ans, pres[i] + sufs[p + 1] - pres[i] / a[i].fi); if (i >= p) break; smax(ans, pres[i] + sufs[p] - sufs[p] / (1 - a[p].fi)); } cout << fixed << setprecision(10) << ans << '\n'; return0; }
using ll = longlong; using ull = unsignedlonglong;
int n, m;
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); int T; cin >> T; while (T--) { ll n, A, B, C; cin >> n >> A >> B >> C; if (A < B) swap(A, B); if (A < C) swap(A, C); if (B < C) swap(B, C); ll s = (A + B + C) * n; auto calc = [&](ll x1, ll x2) { return n * C + x1 * (A - C) + x2 * (B - C); }; ll sta = LLONG_MAX, x1 = -1, x2 = -1; for (int i = 0; i <= n; ++i) { int l = 0, r = n - i; while (l < r) { int mid = (l + r) / 2; if (calc(i, mid) * 3 < s) l = mid + 1; else r = mid; } if (!(l == 0 || abs(calc(i, l) * 3 - s) <= abs(calc(i, l - 1) * 3 - s))) --l; ll p = abs(calc(i, l) * 3 - s); if (p < sta) x1 = i, x2 = l, sta = p; } for (int i = 1; i <= n; ++i) { if (i <= x1) cout << A << endl; elseif (i <= x1 + x2) cout << B << endl; else cout << C << endl; int x, y; cin >> x >> y; } }
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to) #define dbg(...) fprintf(stderr, __VA_ARGS__) #define fi first #define se second
using ll = longlong; using ull = unsignedlonglong; using pii = pair<int, int>;
template <typename A, typename B> boolsmax(A &a, const B &b){ return a < b ? a = b, 1 : 0; } template <typename A, typename B> boolsmin(A &a, const B &b){ return b < a ? a = b, 1 : 0; }
constexprint N = 1e5 + 7;
int n, m, q; int a[N], b[N]; int rv[N], fa[N], sz[N], c1[N]; vector<int> g[N];
structvHeap { int k; ll sum; Heap<pii> q; Heap<pii, vector<pii>, greater<pii>> nq; voidins(int x, int v){ q.push(pii(v, x)), sum += v; while (q.size() > k && !q.empty()) sum -= q.top().fi, nq.push(q.top()), q.pop(); } voiddel(int x, int v){ if (!q.empty() && pair(v, x) <= q.top()) { q.del(pii(v, x)), sum -= v; while (q.size() < k && !nq.empty()) sum += nq.top().fi, q.push(nq.top()), nq.pop(); } else nq.del(pii(v, x)); } voidmodify(int x, int v, int nv){ del(x, v), ins(x, nv); } voidmodify(int nk){ assert(abs(nk - k) <= 1); k = nk; while (q.size() > k && !q.empty()) sum -= q.top().fi, nq.push(q.top()), q.pop(); while (q.size() < k && !nq.empty()) sum += nq.top().fi, q.push(nq.top()), nq.pop(); } } h[N][2];
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n >> m >> q; for (int i = 1; i <= n; ++i) cin >> a[i] >> b[i];
for (int i = 1; i <= m; ++i) { int l, mx = 0, &cnt1 = c1[i]; cin >> l; vector<int> t; for (int j = 0, x; j < l; ++j) { cin >> x; if (x < 0) x = rv[-x]; if (x > mx) mx && (t.push_back(mx), cnt1 += a[mx]), mx = x; else t.push_back(x), cnt1 += a[x]; } rv[i] = mx, sz[i] = l - 1; assert(~t.size() & 1); if (cnt1 < t.size() / 2) h[i][0].k = t.size() / 2 - cnt1; else h[i][1].k = cnt1 - t.size() / 2; for (int x : t) h[i][a[x]].ins(x, b[x]), fa[x] = i; }
ll ans = 0; for (int i = 1; i <= m; ++i) ans += h[i][0].sum + h[i][1].sum; for (int i = 1; i <= q; ++i) { int x, y, z; cin >> x >> y >> z; int f = fa[x]; if (!f) goto end; ans -= h[f][0].sum + h[f][1].sum; if (y == a[x]) h[f][y].modify(x, b[x], z); else { h[f][a[x]].del(x, b[x]), h[f][y].ins(x, z); c1[f] += y - a[x]; h[f][0].modify(max(sz[f] / 2 - c1[f], 0)), h[f][1].modify(max(c1[f] - sz[f] / 2, 0)); } ans += h[f][0].sum + h[f][1].sum; end: a[x] = y, b[x] = z; cout << ans << '\n'; } return0; }
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to) #define dbg(...) fprintf(stderr, __VA_ARGS__) #define fi first #define se second
using ll = longlong; using ull = unsignedlonglong; using pii = pair<int, int>;
template <typename A, typename B> boolsmax(A &a, const B &b){ return a < b ? a = b, 1 : 0; } template <typename A, typename B> boolsmin(A &a, const B &b){ return b < a ? a = b, 1 : 0; }
constexprint N = 4e5 + 7; constexprint M = 640; constexprint LOG = 19;
int n, blo; structPeo { int l, r, k; } a[N]; int s[N], vis[N], intr[N]; queue<int> q;
#define lc o << 1 #define rc o << 1 | 1 vector<int> t[N << 2]; voidqins(int o, int L, int R, int l, int r, int k){ if (l <= L && R <= r) return t[o].push_back(k); int M = (L + R) / 2; if (l <= M) qins(lc, L, M, l, r, k); if (r > M) qins(rc, M + 1, R, l, r, k); } voiddel(int o){ vector<int> v; for (int i : t[o]) if (a[i].k) { --a[i].k; if (a[i].k) v.push_back(i); else q.push(i); } swap(t[o], v); } voidqdel(int o, int L, int R, int x){ del(o); if (L == R) return; int M = (L + R) / 2; if (x <= M) qdel(lc, L, M, x); elseqdel(rc, M + 1, R, x); }
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n; blo = sqrt(n); for (int i = 1; i <= n; ++i) cin >> a[i].l >> a[i].r >> a[i].k; for (int i = 1; i <= n; ++i) if (!a[i].k) q.push(i), intr[i] = 1; int cnt = 0; while (!q.empty()) { int x = q.front(); q.pop(); if (cnt % blo == 0) { for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + vis[i]; for (int i = 1; i <= n; ++i) if (!intr[i] && s[a[i].r] - s[a[i].l - 1] + blo >= a[i].k) { a[i].k -= s[a[i].r] - s[a[i].l - 1]; qins(1, 1, n, a[i].l, a[i].r, i); intr[i] = 1; } } ++cnt; qdel(1, 1, n, x), vis[x] = 1; } cout << cnt << endl; return0; }