diff --git a/solved/Codeforces/Codeforces Round #748 (Div. 3)/.vscode/settings.json b/solved/Codeforces/Codeforces Round #748 (Div. 3)/.vscode/settings.json new file mode 100644 index 00000000..959c2d55 --- /dev/null +++ b/solved/Codeforces/Codeforces Round #748 (Div. 3)/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "iostream": "cpp", + "numeric": "cpp", + "*.tcc": "cpp" + } +} \ No newline at end of file diff --git a/solved/Codeforces/Codeforces Round #748 (Div. 3)/A.cc b/solved/Codeforces/Codeforces Round #748 (Div. 3)/A.cc new file mode 100644 index 00000000..2add11f2 --- /dev/null +++ b/solved/Codeforces/Codeforces Round #748 (Div. 3)/A.cc @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() { + int cases; + cin >> cases; + for (int i = 0; i < cases; i++) { + int a, b, c; + cin >> a >> b >> c; + int mmax = max(max(a, b), c); + int num_winners = 0; + if (a == mmax) num_winners++; + if (b == mmax) num_winners++; + if (c == mmax) num_winners++; + auto best = [&](int x) -> int { + if (x == mmax) return (num_winners == 1) ? 0 : 1; + int left = mmax - x; + return left + 1; + }; + cout << best(a) << " " << best(b) << " " << best(c) << endl; + } + return 0; +} \ No newline at end of file diff --git a/solved/Codeforces/Codeforces Round #748 (Div. 3)/B.cc b/solved/Codeforces/Codeforces Round #748 (Div. 3)/B.cc new file mode 100644 index 00000000..e27ac597 --- /dev/null +++ b/solved/Codeforces/Codeforces Round #748 (Div. 3)/B.cc @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int to_target(const string& input, const string& ending) { + int cost = 0; + int j = 1; + for (int i = input.size() - 1; i >= 0; i--) { + if (input[i] == ending[j]) + j--; + else + cost++; + + if (j == -1) return cost; + } + + return 100; +} + +int main() { + int t; + cin >> t; + while (t--) { + string n; + cin >> n; + int best = 100; + best = min(best, to_target(n, "00")); + best = min(best, to_target(n, "25")); + best = min(best, to_target(n, "50")); + best = min(best, to_target(n, "75")); + assert(best != 100); + cout << best << endl; + } + return 0; +} \ No newline at end of file diff --git a/solved/Codeforces/Codeforces Round #748 (Div. 3)/C.cc b/solved/Codeforces/Codeforces Round #748 (Div. 3)/C.cc new file mode 100644 index 00000000..9cbdcd85 --- /dev/null +++ b/solved/Codeforces/Codeforces Round #748 (Div. 3)/C.cc @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int main() { + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + vector dist(k); + for (auto &d : dist) { + int tmp; + cin >> tmp; + d = (n - tmp); + } + sort(dist.begin(), dist.end()); + long long total = 0; + int last = 0; + for (int i = 0; i < k; i++) { + total += dist[i]; + if (total < n) { + last = i + 1; + } + } + cout << last << endl; + } + return 0; +} \ No newline at end of file diff --git a/solved/Codeforces/Codeforces Round #748 (Div. 3)/D.cc b/solved/Codeforces/Codeforces Round #748 (Div. 3)/D.cc new file mode 100644 index 00000000..e4b043d6 --- /dev/null +++ b/solved/Codeforces/Codeforces Round #748 (Div. 3)/D.cc @@ -0,0 +1,30 @@ +#include + +using namespace std; + +bool all_equal(const vector &a) { + for (auto i : a) + if (i != a[0]) return false; + return true; +} + +int main() { + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vector a(n); + for (auto &i : a) cin >> i; + int mmin = *min_element(a.begin(), a.end()); + if (all_equal(a)) { + cout << -1 << endl; + continue; + } + for (auto &i : a) i -= mmin; + int gcd = 0; + for (auto i : a) gcd = __gcd(gcd, i); + cout << gcd << endl; + } + return 0; +} \ No newline at end of file diff --git a/solved/Codeforces/Codeforces Round #748 (Div. 3)/D2.cc b/solved/Codeforces/Codeforces Round #748 (Div. 3)/D2.cc new file mode 100644 index 00000000..cf262fcb --- /dev/null +++ b/solved/Codeforces/Codeforces Round #748 (Div. 3)/D2.cc @@ -0,0 +1,61 @@ +#include + +using namespace std; + +const int inf = 2000000; + +int num_equal(const vector &a, int target) { + int total = 0; + for (auto i : a) + if (i == target) total++; + return total; +} + +set divisors(int n) { + set divs; + for (int d = 1; d * d <= n; d++) { + if ((n % d) == 0) { + divs.insert(d); + divs.insert(n / d); + } + } + return divs; +} + +void solve() { + int n; + cin >> n; + vector a(n); + for (auto &i : a) cin >> i; + + int best = -1; + for (auto start : a) { + int same = num_equal(a, start); + if (same >= (n / 2)) { + cout << -1 << endl; + return; + } + map freq_divs; + for (auto i : a) { + if (i < start) continue; + set divs = divisors((i - start)); + for (auto d : divs) freq_divs[d]++; + } + + for (auto [d, f] : freq_divs) { + if (f >= (n / 2) - same) { + best = max(best, d); + } + } + } + cout << best << endl; +} + +int main() { + int t; + cin >> t; + while (t--) { + solve(); + } + return 0; +} \ No newline at end of file diff --git a/solved/Codeforces/Codeforces Round #748 (Div. 3)/E.cc b/solved/Codeforces/Codeforces Round #748 (Div. 3)/E.cc new file mode 100644 index 00000000..ba69a902 --- /dev/null +++ b/solved/Codeforces/Codeforces Round #748 (Div. 3)/E.cc @@ -0,0 +1,91 @@ +#include + +using namespace std; + +const int INF = 1000000; + +struct Node { + void AddTo(Node *to) { + adj.push_back(to); + num_edges++; + } + + vector UnprocessedNeighbors() { + vector ans; + for (auto to : adj) { + if (!to->queued) ans.push_back(to); + } + return ans; + } + + int id; + int timestamp; + bool queued; + int num_edges; + vector adj; +}; + +struct Graph { + Graph(int num_nodes) { + for (int i = 0; i < num_nodes; i++) { + nodes.push_back( + Node{.id = i, .timestamp = 1, .queued = false, .num_edges = 0}); + } + } + + void AddEdge(int a, int b) { + nodes[a].AddTo(&nodes[b]); + nodes[b].AddTo(&nodes[a]); + } + + void MarkLeaves() { + for (auto &node : nodes) { + if (node.num_edges == 1) { + unprocessed_leaves.push_back(&node); + node.queued = true; + } + } + } + + vector nodes; + deque unprocessed_leaves; +}; + +void solve() { + int n, k; + cin >> n >> k; + Graph g(n); + for (int i = 0; i < (n - 1); i++) { + int a, b; + cin >> a >> b; + g.AddEdge(a - 1, b - 1); + } + g.MarkLeaves(); + while (g.unprocessed_leaves.size() > 0) { + Node *leave = g.unprocessed_leaves.front(); + g.unprocessed_leaves.pop_front(); + for (auto *to : leave->UnprocessedNeighbors()) { + to->timestamp = max(to->timestamp, leave->timestamp + 1); + to->num_edges--; + if (to->num_edges == 1) { + g.unprocessed_leaves.push_back(to); + to->queued = true; + } + } + leave->queued = true; + } + int ans = 0; + for (auto &node : g.nodes) { + if (node.timestamp > k) ans++; + } + cout << ans << endl; +} + +int main() { + int tc; + cin >> tc; + while (tc--) { + solve(); + } + return 0; +} \ No newline at end of file diff --git a/solved/Codeforces/Codeforces Round #748 (Div. 3)/F.cc b/solved/Codeforces/Codeforces Round #748 (Div. 3)/F.cc new file mode 100644 index 00000000..16909ce0 --- /dev/null +++ b/solved/Codeforces/Codeforces Round #748 (Div. 3)/F.cc @@ -0,0 +1,79 @@ +#include + +using namespace std; + +const int inf = 1000; + +void solve() { + int n, A, B; + cin >> n >> A >> B; + string number; + cin >> number; + vector digits(n); + for (int i = 0; i < n; i++) { + digits[i] = number[i] - '0'; + } + int dp[n + 1][n + 1][A][B]; + for (int i = 0; i < A; i++) { + for (int j = 0; j < B; j++) { + dp[n][n][i][j] = inf; + dp[n][0][i][j] = inf; + } + } + for (int reds = 1; reds < n; reds++) { + int blacks = n - reds; + for (int i = 0; i < A; i++) { + for (int j = 0; j < B; j++) { + dp[n][reds][i][j] = inf; + } + } + dp[n][reds][0][0] = abs(reds - blacks); + } + + for (int i = n - 1; i >= 0; i--) { + for (int reds = 0; reds < n; reds++) { + for (int modA = 0; modA < A; modA++) { + for (int modB = 0; modB < B; modB++) { + int opt1 = dp[i + 1][reds + 1][((modA * 10) + digits[i]) % A][modB]; + int opt2 = dp[i + 1][reds][modA][((modB * 10) + digits[i]) % B]; + dp[i][reds][modA][modB] = min(opt1, opt2); + } + } + } + } + + string ans; + int best = dp[0][0][0][0]; + if (best == inf) { + cout << -1 << endl; + return; + } + + int reds = 0; + int modA = 0; + int modB = 0; + for (int i = 0; i < n; i++) { + int opt1 = dp[i + 1][reds + 1][((modA * 10) + digits[i]) % A][modB]; + int opt2 = dp[i + 1][reds][modA][((modB * 10) + digits[i]) % B]; + if (opt1 == best) { + ans.push_back('R'); + reds++; + modA = ((modA * 10) + digits[i]) % A; + } else { + assert(opt2 == best); + ans.push_back('B'); + modB = ((modB * 10) + digits[i]) % B; + } + } + + cout << ans << endl; +} + +int main() { + int tc; + cin >> tc; + while (tc--) { + solve(); + } + return 0; +} \ No newline at end of file diff --git a/solved/Codeforces/Codeforces Round #748 (Div. 3)/sol b/solved/Codeforces/Codeforces Round #748 (Div. 3)/sol new file mode 100755 index 00000000..c21facc6 Binary files /dev/null and b/solved/Codeforces/Codeforces Round #748 (Div. 3)/sol differ