Suddenly, all problems expect A and D were invented by me. The author of A and D is MikeMirzayanov.
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
int n;
cin >> n;
int sum = 0;
for (int j = 0; j < n; ++j) {
int x;
cin >> x;
sum += x;
}
cout << (sum + n - 1) / n << endl;
}
return 0;
}
1234B1 - Social Network (easy version)
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n, k;
cin >> n >> k;
vector<int> ids;
for (int i = 0; i < n; ++i) {
int id;
cin >> id;
if (find(ids.begin(), ids.end(), id) == ids.end()) {
if (int(ids.size()) >= k) ids.pop_back();
ids.insert(ids.begin(), id);
}
}
cout << ids.size() << endl;
for (auto it : ids) cout << it << " ";
cout << endl;
return 0;
}
1234B2 - Social Network (hard version)
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n, k;
cin >> n >> k;
queue<int> q;
set<int> vals;
for (int i = 0; i < n; ++i) {
int id;
cin >> id;
if (!vals.count(id)) {
if (int(q.size()) >= k) {
int cur = q.front();
q.pop();
vals.erase(cur);
}
vals.insert(id);
q.push(id);
}
}
vector<int> res;
while (!q.empty()) {
res.push_back(q.front());
q.pop();
}
reverse(res.begin(), res.end());
cout << res.size() << endl;
for (auto it : res) cout << it << " ";
cout << endl;
return 0;
}
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
int n;
string s[2];
cin >> n >> s[0] >> s[1];
int row = 0;
int pos = 0;
for (pos = 0; pos < n; ++pos) {
if (s[row][pos] >= '3') {
if (s[row ^ 1][pos] < '3') {
break;
} else {
row ^= 1;
}
}
}
if (pos == n && row == 1) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
1234D - Distinct Characters Queries
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
string s;
cin >> s;
vector<set<int>> poss(26);
for (int i = 0; i < int(s.size()); ++i) {
poss[s[i] - 'a'].insert(i);
}
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
int tp;
cin >> tp;
if (tp == 1) {
int pos;
char c;
cin >> pos >> c;
--pos;
poss[s[pos] - 'a'].erase(pos);
s[pos] = c;
poss[s[pos] - 'a'].insert(pos);
} else {
int l, r;
cin >> l >> r;
--l, --r;
int cnt = 0;
for (int c = 0; c < 26; ++c) {
auto it = poss[c].lower_bound(l);
if (it != poss[c].end() && *it <= r) ++cnt;
}
cout << cnt << endl;
}
}
return 0;
}
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n, m;
cin >> n >> m;
vector<int> a(m);
for (int i = 0; i < m; ++i) {
cin >> a[i];
--a[i];
}
vector<long long> res(n);
for (int j = 0; j < m - 1; ++j) {
res[0] += abs(a[j] - a[j + 1]);
}
vector<int> cnt(n);
vector<vector<int>> adj(n);
for (int i = 0; i < m - 1; ++i) {
int l = a[i], r = a[i + 1];
if (l != r) {
adj[l].push_back(r);
adj[r].push_back(l);
}
if (l > r) swap(l, r);
if (r - l < 2) continue;
++cnt[l + 1];
--cnt[r];
}
for (int i = 0; i < n - 1; ++i) {
cnt[i + 1] += cnt[i];
}
for (int i = 1; i < n; ++i) {
res[i] = res[0] - cnt[i];
for (auto j : adj[i]) {
res[i] -= abs(i - j);
res[i] += j + (j < i);
}
}
for (int i = 0; i < n; ++i) {
cout << res[i] << " ";
}
cout << endl;
return 0;
}
1234F - Yet Another Substring Reverse
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
string s;
cin >> s;
vector<int> dp(1 << 20);
for (int i = 0; i < int(s.size()); ++i) {
vector<bool> used(20);
int mask = 0;
for (int j = 0; i + j < int(s.size()); ++j) {
if (used[s[i + j] - 'a']) break;
used[s[i + j] - 'a'] = true;
mask |= 1 << (s[i + j] - 'a');
dp[mask] = __builtin_popcount(mask);
}
}
for (int mask = 0; mask < (1 << 20); ++mask) {
for (int pos = 0; pos < 20; ++pos) {
if ((mask >> pos) & 1) {
dp[mask] = max(dp[mask], dp[mask ^ (1 << pos)]);
}
}
}
int ans = 0;
for (int mask = 0; mask < (1 << 20); ++mask) {
if (dp[mask] == __builtin_popcount(mask)) {
int comp = ~mask & ((1 << 20) - 1);
ans = max(ans, dp[mask] + dp[comp]);
}
}
cout << ans << endl;
return 0;
}
WA?
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int tt = clock();
string s;
cin >> s;
vector<int> dp(1 << 20);
vector<int> masks;
for (int i = 0; i < int(s.size()); ++i) {
vector<bool> used(20);
int mask = 0;
for (int j = 0; i + j < int(s.size()); ++j) {
if (used[s[i + j] - 'a']) break;
used[s[i + j] - 'a'] = true;
mask |= 1 << (s[i + j] - 'a');
masks.push_back(mask);
}
}
sort(masks.begin(), masks.end());
masks.resize(unique(masks.begin(), masks.end()) - masks.begin());
sort(masks.begin(), masks.end(), [](int x, int y){
return __builtin_popcount(x) > __builtin_popcount(y);
});
int ans = __builtin_popcount(masks[0]);
for (int i = 0; i < int(masks.size()); ++i) {
if (clock() - tt > 1900) {
break;
}
for (int j = i + 1; j < int(masks.size()); ++j) {
if (!(masks[i] & masks[j])) {
ans = max(ans, __builtin_popcount(masks[i]) + __builtin_popcount(masks[j]));
break;
}
}
}
cout << ans << endl;
return 0;
}