Спасибо Rox и _overrated_ за помощь с идеями задач и их подготовкой!
1294A - Коллекционирование монет
Идея: MikeMirzayanov
Разбор
Tutorial is loading...
Решение
#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 a[3], n;
cin >> a[0] >> a[1] >> a[2] >> n;
sort(a, a + 3);
n -= 2 * a[2] - a[1] - a[0];
if (n < 0 || n % 3 != 0) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
}
return 0;
}
Идея: MikeMirzayanov
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
for (int tt = 0; tt < t; tt++) {
int n;
cin >> n;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i].first >> a[i].second;
}
sort(a.begin(), a.end());
pair<int, int> cur = make_pair(0, 0);
string ans;
bool ok = true;
for (int i = 0; i < n; ++i) {
int r = a[i].first - cur.first;
int u = a[i].second - cur.second;
if (r < 0 || u < 0) {
cout << "NO" << endl;
ok = false;
break;
}
ans += string(r, 'R');
ans += string(u, 'U');
cur = a[i];
}
if (ok)
cout << "YES" << endl << ans << endl;
}
return 0;
}
1294C - Произведение трех чисел
Идея: MikeMirzayanov
Разбор
Tutorial is loading...
Решение
#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;
set<int> used;
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0 && !used.count(i)) {
used.insert(i);
n /= i;
break;
}
}
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0 && !used.count(i)) {
used.insert(i);
n /= i;
break;
}
}
if (int(used.size()) < 2 || used.count(n) || n == 1) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
used.insert(n);
for (auto it : used) cout << it << " ";
cout << endl;
}
}
return 0;
}
Идея: vovuh
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int q, x;
cin >> q >> x;
vector<int> mods(x);
set<pair<int, int>> vals;
for (int i = 0; i < x; ++i) {
vals.insert(make_pair(mods[i], i));
}
for (int i = 0; i < q; ++i) {
int cur;
cin >> cur;
cur %= x;
vals.erase(make_pair(mods[cur], cur));
++mods[cur];
vals.insert(make_pair(mods[cur], cur));
cout << vals.begin()->first * x + vals.begin()->second << endl;
}
return 0;
}
Идея: vovuh
Разбор
Tutorial is loading...
Решение
#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<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
--a[i][j];
}
}
long long ans = 0;
for (int j = 0; j < m; ++j) {
vector<int> cnt(n);
for (int i = 0; i < n; ++i) {
if (a[i][j] % m == j) {
int pos = a[i][j] / m;
if (pos < n) {
++cnt[(i - pos + n) % n];
}
}
}
int cur = n - cnt[0];
for (int i = 1; i < n; ++i) {
cur = min(cur, n - cnt[i] + i);
}
ans += cur;
}
cout << ans << endl;
return 0;
}
Идея: MikeMirzayanov
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
vector<int> p;
vector<vector<int>> g;
pair<int, int> dfs(int v, int par = -1, int dist = 0) {
p[v] = par;
pair<int, int> res = make_pair(dist, v);
for (auto to : g[v]) {
if (to == par) continue;
res = max(res, dfs(to, v, dist + 1));
}
return res;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
p = vector<int>(n);
g = vector<vector<int>>(n);
for (int i = 0; i < n - 1; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].push_back(y);
g[y].push_back(x);
}
pair<int, int> da = dfs(0);
pair<int, int> db = dfs(da.y);
vector<int> diam;
int v = db.y;
while (v != da.y) {
diam.push_back(v);
v = p[v];
}
diam.push_back(da.y);
if (int(diam.size()) == n) {
cout << n - 1 << " " << endl << diam[0] + 1 << " " << diam[1] + 1 << " " << diam.back() + 1 << endl;
} else {
queue<int> q;
vector<int> d(n, -1);
for (auto v : diam) {
d[v] = 0;
q.push(v);
}
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto to : g[v]) {
if (d[to] == -1) {
d[to] = d[v] + 1;
q.push(to);
}
}
}
pair<int, int> mx = make_pair(d[0], 0);
for (int v = 1; v < n; ++v) {
mx = max(mx, make_pair(d[v], v));
}
cout << int(diam.size()) - 1 + mx.x << endl << diam[0] + 1 << " " << mx.y + 1 << " " << diam.back() + 1 << endl;
}
return 0;
}