Codeforces Round #733 (Div. 1 + Div. 2, based on VK Cup 2021 - Elimination (Engine))
A - Binary Decimal
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n;
for (k = 0; n; umax(k, n % 10), n /= 10);
cout << k << '\n';
}
return 0;
}
B - Putting Plates
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n >> m;
string s(m, '0'), t(m, '0'), p(m, '0');
for (int i = 0; i < m; i += 2)
s[i] = '1';
cout << s << '\n';
t[0] = t.back() = '1';
for (int i = 1; i < n - 1; ++i)
cout << ((i & 1) || i == n - 2 ? p : t) << '\n';
cout << s << "\n\n";
}
return 0;
}
C - Pursuit
二分, \(check\)是\(O(1)\)
bool check(VI& a, VI& b, int mid) {
int k = (n + mid) / 4;
if (n <= k)
return 1;
if (!k)
return a.back() + 100 * mid - b.back() >= 0;
if (k <= mid)
return a.back() - a[k - 1] + 100 * mid - b.back() >= 0;
return a.back() - a[k - 1] + 100 * mid - b.back() + b[k - mid - 1] >= 0;
}
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n;
VI a(n), b(n);
for (auto &i : a) cin >> i;
for (auto &i : b) cin >> i;
sort(all(a)); sort(all(b));
for (int i = 1; i < n; ++i)
a[i] += a[i - 1], b[i] += b[i - 1];
ll l = 0, r = n;
while (l < r) {
int mid = l + r >> 1;
if (check(a, b, mid)) r = mid;
else l = mid + 1;
}
cout << l << '\n';
}
return 0;
}
D - Secret Santa
先全部让\(b_i = a_i\), 统计每个人收到的礼物数\(c_i\)
然后再让所有\(c_i <= 1\)
就可以把人分成4堆
- 收了礼物没送
- 没收没送
- 送了没收
- 送了收了
不用考虑\(4\)
剩下的适配即可, 看代码注意分出前三堆人的顺寻, 和适配的顺序
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n;
VI a(n + 1), b(n + 1), c(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
b[i] = a[i];
++c[a[i]];
}
VI x, y, z;
for (int i = 1; i <= n; ++i)
if (c[i] && c[a[i]] > 1) {
--c[a[i]];
b[i] = 0;
x.emplace_back(i);
}
for (int i = 1; i <= n; ++i)
if (c[i] == 0 && c[a[i]] > 1) {
--c[a[i]];
b[i] = 0;
y.emplace_back(i);
} else if (c[i] == 0)
z.emplace_back(i);
while (!y.empty()) {
if (!z.empty()) {
b[y.back()] = z.back();
z.pop_back();
z.emplace_back(y.back());
y.pop_back();
} else if (y.size() > 1) {
x.emplace_back(y.back());
y.pop_back();
b[y.back()] = x.back();
z.emplace_back(y.back());
y.pop_back();
} else {
for (int i = 1; i <= n; ++i)
if (b[i]) {
b[y.back()] = b[i];
b[i] = y.back();
y.pop_back();
break;
}
}
}
while (!x.empty()) {
b[x.back()] = z.back();
x.pop_back();
z.pop_back();
}
k = 0;
for (int i = 1; i <= n; ++i)
k += a[i] == b[i];
cout << k << '\n';
for (int i = 1; i <= n; ++i)
cout << b[i] << char(" \n"[i == n]);
}
return 0;
}
E - Minimax
分类讨论
看代码怎么分类输出答案就行了
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> s;
map<char, int> cnt;
for (auto& c : s) ++cnt[c];
if (cnt.size() == 1) {
cout << s << '\n';
continue;
}
char f = 0;
for (auto &i : cnt)
if (i.second == 1) {
f = i.first;
break;
}
if (f) {
cnt.erase(f);
cout << f;
while (!cnt.empty())
if (cnt.begin()->second--)
cout << cnt.begin()->first;
else
cnt.erase(cnt.begin());
cout << '\n';
continue;
}
if (cnt.begin()->second - 1 << 1 <= s.size()) {
auto p = *cnt.begin();
cnt.erase(cnt.begin());
--p.second;
cout << p.first;
while (!cnt.empty()) {
if (p.second)
cout << p.first, --p.second;
--cnt.begin()->second;
cout << cnt.begin()->first;
if (cnt.begin()->second == 0)
cnt.erase(cnt.begin());
}
if (p.second)
cout << p.first;
cout << '\n';
continue;
}
if (cnt.size() == 2) {
auto p = *cnt.begin();
cnt.erase(cnt.begin());
--p.second;
cout << p.first;
while (cnt.begin()->second--)
cout << cnt.begin()->first;
while (p.second--)
cout << p.first;
cout << '\n';
continue;
}
pair<char, int> p = *cnt.begin();
cnt.erase(cnt.begin());
cout << p.first << cnt.begin()->first;
for (--p.second; p.second; --p.second)
cout << p.first;
p = *cnt.begin();
cnt.erase(cnt.begin());
cout << cnt.begin()->first;
--cnt.begin()->second;
cnt[p.first] = p.second - 1;
while (!cnt.empty())
if (cnt.begin()->second--)
cout << cnt.begin()->first;
else
cnt.erase(cnt.begin());
cout << '\n';
}
return 0;
}