Codeforces Round #721 (Div. 2)
Codeforces Round #721 (Div. 2)
A - And Then There Were K
打表找规律
int main() {
IOS; VI a(1);
while (a.back() < 1e9) {
ll cur = a.back() * 2 + 1;
if (cur >= 1e9) break;
a.pb(cur);
}
for (cin >> _; _; --_) {
cin >> n;
cout << *(lower_bound(all(a), n) -1) << '\n';
}
return 0;
}
B - Palindrome Game (easy version)
回文串,
1 0个0, 平局
2 偶数个0, 先手必填一个位置, 后手填对称位置, 先手又变成回文(偶数) ..., 剩最后两个, 后手反转, 先手多填2个
3 1个0, 后手胜
4 奇数个0, 先手下中间, 变成情况2, 后手多下一个
不是回文,
- 串长为奇数, 中间和某个位置为0, 不论怎么下平局
- 否则, 先手填个对称位置, 后手紧接填对称位置, 串还是非回文, 且双方消耗相同, 情况无变化, 所以对称0, 对答案无贡献(就算是中间为0,贡献也是-1), 但先手可以让后手把所有非对称全下(贡献大于1), 故先手胜
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n >> s + 1;
int x = 0, f = 0;
rep (i, 1, n >> 1) f += s[i] != s[n + 1 - i];
rep (i, 1, n) x += s[i] == '0';
if (f == 0) {
if (x == 0) cout << "DRAW\n";
else if (x % 2 == 0 || x == 1) cout << "BOB\n";
else cout << "ALICE\n";
}
else if (f == 1 && x == 2) cout << "DRAW\n";
else cout << "ALICE\n";
}
return 0;
}
C - Sequence Pair Weight
对于数字 1, 出现位置为
\(a_1, a_2, a_3, ..., a_k\)
贡献为 \(a_{k - 1} \times (n + 1 - a_k) + a_{k - 2} \times (n + 1 - a_k) + a_{k - 2} \times (n + 1 - a_{k - 1} ...)\)
倒着维护\((n + 1 - a_k) + (n + 1 - a_{k - 1})...\) 即可
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n; ll ans = 0; VI x;
rep (i, 1, n) cin >> a[i], x.pb(a[i]), c[i] = 0;
sort(all(x)); x.erase(unique(all(x)), x.end());
per (i, n, 1) {
a[i] = lower_bound(all(x), a[i]) - x.begin() + 1;
ans += (ll)i * c[a[i]];
c[a[i]] += n + 1 - i;
}
cout << ans << '\n';
}
return 0;
}