Educational Codeforces Round 98 (Rated for Div. 2)

A - Robot Program

int main() {
	IOS;
	for (cin >> _; _; --_) {
		ll a, b, ans = 0; cin >> a >> b;
                cout << a + b + max(abs(b - a) - 1, 0ll) << '
';
        }
	return 0;
}

B - Toy Blocks

对每个都允许, 先设最少x, 原先总和为 sum, 则

(sum + x) / (n - 1) 是整数

其次保证 max(ai) <= (sum + x) / (n - 1)

同时 max(ai) * (n - 1) >= sum

答案就有了

int main() {
	IOS;
	for (cin >> _; _; --_) {
		cin >> n; ll mx = -1, s = 0;
                rep (i, 1, n) cin >> a[i], umax(mx, a[i]), s += a[i];
                ll ans = mx * (n - 1ll) - s;
                if (ans < 0) ans = (ans % (n - 1) + (n - 1)) % (n - 1);
                cout << ans << '
';
        }
	return 0;
}

C - Two Brackets

比b简单

int main() {
	IOS;
	for (cin >> _; _; --_) {
		string s; cin >> s;
        ll ans = 0, x = 0, y = 0;
        for (auto c : s) 
            if (c == '(') ++x;
            else if (c == '[') ++y;
            else if (c == ')') {
                if (x) ++ans, --x;
            } else if (c == ']') {
                if (y) ++ans, --y;
            }
        cout << ans << '
';
    }
	return 0;
}

D - Radio Towers

dp, 设 d[i] 为正好包住 i 的方案数量

ll d[N], a[N];
 
ll qpow(ll a, ll b) {
    ll ans = 1;
    for (; b; b >>= 1, a = a * a % mod)
        if (b & 1) ans = ans * a % mod;
    return ans;
}
 
int main() {
	IOS; cin >> n; a[0] = d[0] = 1;
        rep (i, 1, n) a[i] = d[i - 1], d[i] = (a[i] + d[i - 2]) % mod;
        cout << a[n] * qpow(qpow(2, mod - 2), n) % mod;
	return 0;
}

F - Divide Powers

暴力模拟

int main() {
	IOS; cin >> n >> m; v[0] = 1;
	rep (i, 1, n - 1) v[i] = v[i - 1] << 1;
    rep (i, 0, n - 1) cin >> a[i];
    rep (i, 1, m) {
        ll op, x, y; cin >> op >> x >> y;
        if (op == 1) a[x] = y;
        else {
            rep (i, 0, n - 1) c[i] = a[i];
			ll ans = 0, cur = 0, sum = 0;
			rep (i, 0, x) cur += c[i], sum += c[i] * v[i];
			while (1) {
				int ls = 0;
				if (cur >= y) break;
				rep (i, x + 1, n - 1) {
					ll s = min(c[i], (y - cur) / v[i - x]), cs = s * v[i - x];
					c[i] -= s, c[x] += cs;
					if (!ls && c[i]) ls = i;
					ans += cs - s, cur += cs, sum += cs * v[x];
				}
				if (cur >= y) break;
				if (sum >= y) { ans += y - cur; break; }
				if (!ls) { ans = -1; break; }
				ans += ls - x; cur += 2; sum += v[x + 1]; c[x] += 2;
				rep (i, x + 1, ls - 1) ++c[i]; --c[ls];
			}
			cout << ans << '
';
        }
    }
	return 0;
}
原文地址:https://www.cnblogs.com/2aptx4869/p/14009313.html