diff --git a/Codeforces Round 969 (Div. 1)/A. Iris and Game on the Tree.cpp b/Codeforces Round 969 (Div. 1)/A. Iris and Game on the Tree.cpp new file mode 100644 index 0000000..46c059c --- /dev/null +++ b/Codeforces Round 969 (Div. 1)/A. Iris and Game on the Tree.cpp @@ -0,0 +1,154 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2006/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vvi graph(n); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + graph[a].push_back(b); + graph[b].push_back(a); + } + + string a; + cin >> a; + + array c = {0, 0, 0}; + int ch = 0; + + function dfs = [&](int i, int p) { + if (i != p && graph[i].size() == 1) { + if (a[i] == '?') { + c[2]++; + return; + } + + c[a[i] - '0']++; + return; + } + + if (a[i] == '?') { + ch++; + } + + repv(j, graph[i]) { + if (j == p) { + continue; + } + dfs(j, i); + } + }; + + dfs(0, 0); + + if (a[0] == '?') { + cout << max(max(c[0], c[1]) + c[2] / 2, min(c[0], c[1]) + (c[2] + (~ch & 1)) / 2) << '\n'; + return; + } + + int t = a[0] - '0'; + cout << c[t^1] + (c[2] + 1) / 2 << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/European Championship 2025 - Online Mirror (Unrated, ICPC Rules, Teams Preferred)/F. Mascot Naming.cpp b/European Championship 2025 - Online Mirror (Unrated, ICPC Rules, Teams Preferred)/F. Mascot Naming.cpp new file mode 100644 index 0000000..0fd2371 --- /dev/null +++ b/European Championship 2025 - Online Mirror (Unrated, ICPC Rules, Teams Preferred)/F. Mascot Naming.cpp @@ -0,0 +1,175 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2068/F */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n; + cin >> n; + V a(n); + cin >> a; + + string s; + cin >> s; + + auto issub = [&](string &a, string &b) { + int i = 0; + rep(j, a.size()) { + if (a[j] == b[i]) { + i++; + if (i == b.size()) { + return true; + } + } + } + + return false; + }; + + vvi pos(26); + vi ind(n); + + rep(i, n) { + if (issub(a[i], s)) { + cout << "NO\n"; + return; + } + + pos[a[i][0] - 'a'].push_back(i); + } + + cout << "YES\n"; + + string ans; + + auto prall = [&](int j) { + vi tmp; + swap(tmp, pos[j]); + ans.push_back(j + 'a'); + + while (!tmp.empty()) { + int i = tmp.back(); + tmp.pop_back(); + ind[i]++; + if (ind[i] < a[i].size()) { + pos[a[i][ind[i]] - 'a'].push_back(i); + } + } + }; + + repv(now, s) { + int inv = now - 'a'; + rep(j, 26) { + if (j == inv || pos[j].empty()) { + continue; + } + + prall(j); + j = -1; + } + + if (!pos[inv].empty()) { + prall(inv); + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Good Bye 2024: 2025 is NEAR/C. Bewitching Stargazer.cpp b/Good Bye 2024: 2025 is NEAR/C. Bewitching Stargazer.cpp new file mode 100644 index 0000000..9157e90 --- /dev/null +++ b/Good Bye 2024: 2025 is NEAR/C. Bewitching Stargazer.cpp @@ -0,0 +1,124 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2053/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll n, k; + cin >> n >> k; + + function(ll, ll)> func = [&](ll l, ll r) -> pair { + ll sz = r - l + 1; + if (sz < k) { + return {0, 0}; + } + + ll mid = (l + r) >> 1; + if (sz & 1) { + auto [an, c] = func(l, mid - 1); + return {an * 2 + mid * (c + 1), c * 2 + 1}; + } + auto [an, c] = func(l, mid); + return {an * 2 + mid * c, c * 2}; + }; + + cout << func(1, n).first << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Polynomial Round 2022 (Div. 1 + Div. 2, Rated, Prizes!)/C. Ice and Fire.cpp b/Polynomial Round 2022 (Div. 1 + Div. 2, Rated, Prizes!)/C. Ice and Fire.cpp new file mode 100644 index 0000000..659d0ea --- /dev/null +++ b/Polynomial Round 2022 (Div. 1 + Div. 2, Rated, Prizes!)/C. Ice and Fire.cpp @@ -0,0 +1,120 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1774/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + string s; + cin >> s; + + int cur = 1; + cout << "1"; + nrep(i, 1, n - 1) { + if (s[i] != s[i - 1]) { + cur = (i + 1); + } + + cout << ' ' << cur; + } + cout << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +}