Skip to content

Commit

Permalink
Add pending CF problems
Browse files Browse the repository at this point in the history
  • Loading branch information
pin3da committed Feb 17, 2023
1 parent b4426a4 commit 2cd13bb
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files.associations": {
"iostream": "cpp",
"numeric": "cpp",
"*.tcc": "cpp"
}
}
24 changes: 24 additions & 0 deletions solved/Codeforces/Codeforces Round #748 (Div. 3)/A.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <bits/stdc++.h>

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;
}
35 changes: 35 additions & 0 deletions solved/Codeforces/Codeforces Round #748 (Div. 3)/B.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <bits/stdc++.h>

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;
}
29 changes: 29 additions & 0 deletions solved/Codeforces/Codeforces Round #748 (Div. 3)/C.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>

using namespace std;

int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> 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;
}
30 changes: 30 additions & 0 deletions solved/Codeforces/Codeforces Round #748 (Div. 3)/D.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>

using namespace std;

bool all_equal(const vector<int> &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<int> 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;
}
61 changes: 61 additions & 0 deletions solved/Codeforces/Codeforces Round #748 (Div. 3)/D2.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <bits/stdc++.h>

using namespace std;

const int inf = 2000000;

int num_equal(const vector<int> &a, int target) {
int total = 0;
for (auto i : a)
if (i == target) total++;
return total;
}

set<int> divisors(int n) {
set<int> 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<int> 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<int, int> freq_divs;
for (auto i : a) {
if (i < start) continue;
set<int> 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;
}
91 changes: 91 additions & 0 deletions solved/Codeforces/Codeforces Round #748 (Div. 3)/E.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <bits/stdc++.h>

using namespace std;

const int INF = 1000000;

struct Node {
void AddTo(Node *to) {
adj.push_back(to);
num_edges++;
}

vector<Node *> UnprocessedNeighbors() {
vector<Node *> ans;
for (auto to : adj) {
if (!to->queued) ans.push_back(to);
}
return ans;
}

int id;
int timestamp;
bool queued;
int num_edges;
vector<Node *> 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<Node> nodes;
deque<Node *> 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;
}
79 changes: 79 additions & 0 deletions solved/Codeforces/Codeforces Round #748 (Div. 3)/F.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <bits/stdc++.h>

using namespace std;

const int inf = 1000;

void solve() {
int n, A, B;
cin >> n >> A >> B;
string number;
cin >> number;
vector<int> 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;
}
Binary file not shown.

0 comments on commit 2cd13bb

Please sign in to comment.