Idea: sunbrey
Tutorial
Tutorial is loading...
Solution
#include <iostream>
#include <vector>
using namespace std;
void solve() {
int l, a, b;
cin >> l >> a >> b;
vector<char> aubrey(l, false);
vector<char> sunny(l, false);
int cur = 0;
while (!aubrey[cur]) {
aubrey[cur] = true;
cur = (cur + a) % l;
}
cur = 0;
while (!sunny[cur]) {
sunny[cur] = true;
cur = (cur + b) % l;
}
int answer = 0;
for (int i = 0; i < l; ++i) {
if (aubrey[i] && sunny[i]) {
++answer;
}
}
cout << answer << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Idea: sunbrey
Tutorial
Tutorial is loading...
Solution
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void solve() {
int n, k;
long long p;
cin >> n >> k >> p;
vector<long long> a(n);
long long total = 0;
for (long long &x : a) {
cin >> x;
total += x;
}
sort(a.begin(), a.end());
vector<long long> pref(n + 1, 0);
for (int i = 0; i < n; ++i) {
pref[i + 1] = pref[i] + a[i];
}
long long answer = total;
for (int glasses = 1; glasses <= n / k; ++glasses) {
long long fresh_sum = pref[glasses * k];
long long joy = total - fresh_sum + glasses * p;
answer = max(answer, joy);
}
cout << answer << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
2292C - Finding Shapes in the Clouds
Idea: sunbrey
Tutorial
Tutorial is loading...
Solution
#include <iostream>
#include <string>
using namespace std;
void solve() {
int n;
string s;
cin >> n >> s;
if (n % 2 == 1) {
cout << "NO\n";
return;
}
bool starts_with_zero = true;
bool starts_with_one = true;
for (int i = 0; i < n; ++i) {
char first = (i % 2 == 0 ? '0' : '1');
char second = (i % 2 == 0 ? '1' : '0');
if (s[i] != first) starts_with_zero = false;
if (s[i] != second) starts_with_one = false;
}
cout << (starts_with_zero || starts_with_one ? "NO\n" : "YES\n");
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
2292D - Unexpected Precipitation
Idea: sunbrey
Tutorial
Tutorial is loading...
Solution
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void solve() {
int n;
cin >> n;
vector<long long> x(n), y(n);
for (long long &value : x) cin >> value;
for (long long &value : y) cin >> value;
long long value = 0;
for (int i = n - 1; i >= 0; --i) {
long long delta = x[i] - y[i];
value = max(delta + value, -value);
}
if (value > 0) cout << "Sunny\n";
else if (value < 0) cout << "Aubrey\n";
else cout << "Draw\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Idea: sunbrey
Tutorial
Tutorial is loading...
Solution
#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
long long a, x, k;
cin >> a >> x >> k;
long long blocks = k + 1;
long long low = 0, high = 30 * blocks;
while (high - low > 1) {
long long rounds = (low + high) / 2;
long long q = rounds / blocks;
long long rem = rounds % blocks;
long long distance = (blocks + rem) * (1LL << q) - blocks;
if (distance < a + x * rounds) {
low = rounds;
} else {
high = rounds;
}
}
cout << a + x * low << '\n';
}
return 0;
}
Idea: sunbrey
Tutorial
Tutorial is loading...
Solution
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<vector<int>> graph(2 * n);
vector<vector<int>> original(n);
vector<int> indegree(2 * n, 0);
for (int i = 0; i < m; ++i) {
int u, v, c;
cin >> u >> v >> c;
--u;
--v;
--c;
original[u].push_back(v);
original[v].push_back(u);
int from = 2 * u + c;
int to = 2 * v + (1 - c);
graph[from].push_back(to);
++indegree[to];
from = 2 * v + c;
to = 2 * u + (1 - c);
graph[from].push_back(to);
++indegree[to];
}
vector<int> component(n, -1);
int component_count = 0;
for (int start = 0; start < n; ++start) {
if (component[start] != -1) {
continue;
}
vector<int> stack = {start};
component[start] = component_count;
while (!stack.empty()) {
int v = stack.back();
stack.pop_back();
for (int u : original[v]) {
if (component[u] == -1) {
component[u] = component_count;
stack.push_back(u);
}
}
}
++component_count;
}
queue<int> q;
for (int v = 0; v < 2 * n; ++v) {
if (indegree[v] == 0) {
q.push(v);
}
}
while (!q.empty()) {
int v = q.front();
q.pop();
for (int u : graph[v]) {
--indegree[u];
if (indegree[u] == 0) {
q.push(u);
}
}
}
vector<char> good(component_count, false);
for (int v = 0; v < 2 * n; ++v) {
if (indegree[v] > 0) {
good[component[v / 2]] = true;
}
}
int answer = 0;
for (char is_good : good) {
answer += is_good;
}
cout << answer << '\n';
}
return 0;
}
Idea: sunbrey
Tutorial
Tutorial is loading...
Solution
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void solve() {
int m;
long long k;
cin >> m >> k;
vector<long long> a(m);
for (long long &value : a) cin >> value;
sort(a.begin(), a.end());
long long answer = 0;
for (int power = 1; (1 << power) <= m; ++power) {
int size = 1 << power;
vector<long long> coeff(size, 0);
coeff[size - 1] = power;
coeff[size - 2] = power - 2;
int index = size - 3;
for (int level = power - 1; level >= 1; --level) {
int count = 1 << (power - level);
for (int i = 0; i < count; ++i) {
if (index >= 0) {
coeff[index] = level - 2;
--index;
}
}
}
sort(coeff.begin(), coeff.end());
long long current = -1LL * (size - 1) * k;
int negative_count = 0;
int positive_count = 0;
for (long long value : coeff) {
if (value < 0) ++negative_count;
if (value > 0) ++positive_count;
}
for (int i = 0; i < negative_count; ++i) {
current += coeff[i] * a[i];
}
for (int i = 0; i < positive_count; ++i) {
current += coeff[size - positive_count + i] * a[m - positive_count + i];
}
answer = max(answer, current);
}
cout << answer << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Idea: MikeMirzayanov
Tutorial
Tutorial is loading...
Solution
#include <iostream>
#include <vector>
struct Edge {
int left, right;
};
struct Difference {
int left, right;
};
std::vector<Difference> scan_log(const std::vector<Edge>& edges,
const std::vector<int>& calls, bool backward) {
const int m = static_cast<int>(calls.size());
std::vector<int> known(edges.size() + 1, 1);
std::vector<int> shared(edges.size(), 0);
std::vector<Difference> difference(m);
for (int step = 0; step < m; ++step) {
const int time = backward ? m - 1 - step : step;
const int id = calls[time];
const auto [u, v] = edges[id];
difference[time] = {known[u] - shared[id], known[v] - shared[id]};
const int combined = known[u] + difference[time].right;
known[u] = combined;
known[v] = combined;
shared[id] = combined;
}
return difference;
}
void solve() {
int n, m;
std::cin >> n >> m;
std::vector<Edge> edges(n - 1);
for (auto& [u, v] : edges) {
std::cin >> u >> v;
--u;
--v;
}
std::vector<int> calls(m);
for (int& id : calls) {
std::cin >> id;
--id;
}
const auto before = scan_log(edges, calls, false);
const auto after = scan_log(edges, calls, true);
for (int i = 0; i < m; ++i) {
const long long loss = 1LL * before[i].left * after[i].right
+ 1LL * before[i].right * after[i].left;
std::cout << loss << (i + 1 == m ? '\n' : ' ');
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int tests;
std::cin >> tests;
while (tests-- > 0) {
solve();
}
}








Why are there no comments?