From 7b9271093dacbb5f60280f881b305589ec1ffa1c Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Mon, 3 Nov 2025 14:33:16 -0500 Subject: [PATCH] feat(codeforces): 1062 div 4 --- codeforces/1062/2167a.cc | 27 ++++++++++++ codeforces/1062/2167b.cc | 26 +++++++++++ codeforces/1062/2167c.cc | 33 ++++++++++++++ codeforces/1062/2167d.cc | 30 +++++++++++++ codeforces/1062/2167e.cc | 94 ++++++++++++++++++++++++++++++++++++++++ codeforces/1062/2167f.cc | 86 ++++++++++++++++++++++++++++++++++++ 6 files changed, 296 insertions(+) create mode 100644 codeforces/1062/2167a.cc create mode 100644 codeforces/1062/2167b.cc create mode 100644 codeforces/1062/2167c.cc create mode 100644 codeforces/1062/2167d.cc create mode 100644 codeforces/1062/2167e.cc create mode 100644 codeforces/1062/2167f.cc diff --git a/codeforces/1062/2167a.cc b/codeforces/1062/2167a.cc new file mode 100644 index 0000000..802db2e --- /dev/null +++ b/codeforces/1062/2167a.cc @@ -0,0 +1,27 @@ +#include +#include +#include +using namespace std; +vector a; +void solve() { + a.resize(4); + for (int i = 0; i < a.size(); ++i) { + cin >> a[i]; + } + + sort(begin(a), end(a)); + + if (a.front() == a.back()) { + cout << "YES"; + } else + cout << "NO"; + cout << '\n'; +} +int main() { + int t; + cin >> t; + for (int i = 0; i < t; ++i) { + solve(); + } +} + diff --git a/codeforces/1062/2167b.cc b/codeforces/1062/2167b.cc new file mode 100644 index 0000000..1b43fd4 --- /dev/null +++ b/codeforces/1062/2167b.cc @@ -0,0 +1,26 @@ +#include +#include +#include +using namespace std; +string s, t; +int n; +void solve() { + cin >> n; + cin >> s >> t; + sort(begin(s), end(s)); + sort(begin(t), end(t)); + if (s == t) { + cout << "YES"; + } else { + cout << "NO"; + } + cout << '\n'; +} +int main() { + int t; + cin >> t; + for (int i = 0; i < t; ++i) { + solve(); + } +} + diff --git a/codeforces/1062/2167c.cc b/codeforces/1062/2167c.cc new file mode 100644 index 0000000..a18f3ba --- /dev/null +++ b/codeforces/1062/2167c.cc @@ -0,0 +1,33 @@ +#include +#include +#include +using namespace std; +vector a; +int n; +void solve() { + cin >> n; + a.resize(n); + bool odd = false, even = false; + for (int i = 0; i < n; ++i) { + cin >> a[i]; + odd |= a[i] & 1; + even |= a[i] % 2 == 0; + } + + if (odd && even) { + sort(begin(a), end(a)); + } + + for (int i = 0; i < n; ++i) { + cout << a[i] << " \n"[i == n - 1]; + } +} +int main() { + int t; + cin >> t; + for (int i = 0; i < t; ++i) { + solve(); + } +} + + diff --git a/codeforces/1062/2167d.cc b/codeforces/1062/2167d.cc new file mode 100644 index 0000000..cbef84e --- /dev/null +++ b/codeforces/1062/2167d.cc @@ -0,0 +1,30 @@ +#include +#include +#include +#include +using namespace std; +using u64 = unsigned long long; +vector a; +int n; +void solve() { + cin >> n; + a.resize(n); + for (auto& e : a) cin >> e; + + for (u64 i = 2;; ++i) { + for (auto& e : a) { + if (gcd(e, i) == 1) { + cout << i << '\n'; + return; + } + } + } +} +int main() { + int t; + cin >> t; + for (int i = 0; i < t; ++i) { + solve(); + } +} + diff --git a/codeforces/1062/2167e.cc b/codeforces/1062/2167e.cc new file mode 100644 index 0000000..dbf6b9d --- /dev/null +++ b/codeforces/1062/2167e.cc @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include + +using namespace std; +using i64 = long long; + +struct Seg { + i64 L, R, PL, PR; + int type; // 0=interior, 1=left-edge, 2=right-edge + i64 pos, key; +}; + +static void eval(Seg& s) { + if (s.type == 1) { + s.pos = s.L; + s.key = s.PL - s.pos; + } else if (s.type == 2) { + s.pos = s.R; + s.key = s.pos - s.PR; + } else { + i64 m = (s.PL + s.PR) / 2; + if (m < s.L) m = s.L; + if (m > s.R) m = s.R; + s.pos = m; + s.key = min(s.pos - s.PL, s.PR - s.pos); + } + if (s.key < 0) s.key = 0; +} + +struct Cmp { + bool operator()(const Seg& a, const Seg& b) const { return a.key < b.key; } +}; + +void solve() { + int n; i64 k, X; + cin >> n >> k >> X; + vector A(n); + for (auto& e : A) cin >> e; + sort(A.begin(), A.end()); + + priority_queue, Cmp> pq; + + if (A[0] > 0) { + Seg s{0, A[0] - 1, A[0], 0, 1, 0, 0}; + eval(s); pq.push(s); + } + for (int i = 0; i + 1 < n; ++i) { + if (A[i] + 1 <= A[i + 1] - 1) { + Seg s{A[i] + 1, A[i + 1] - 1, A[i], A[i + 1], 0, 0, 0}; + eval(s); pq.push(s); + } + } + if (A.back() < X) { + Seg s{A.back() + 1, X, 0, A.back(), 2, 0, 0}; + eval(s); pq.push(s); + } + + i64 printed = 0; + while (printed < k && !pq.empty()) { + Seg s = pq.top(); pq.pop(); + cout << s.pos << (printed + 1 == k ? '\n' : ' '); + ++printed; + if (s.L <= s.pos - 1) { + Seg Ls{s.L, s.pos - 1, s.PL, s.PR, s.type, 0, 0}; + eval(Ls); pq.push(Ls); + } + if (s.pos + 1 <= s.R) { + Seg Rs{s.pos + 1, s.R, s.PL, s.PR, s.type, 0, 0}; + eval(Rs); pq.push(Rs); + } + } + + i64 last = LLONG_MIN; + for (int i = 0; printed < k && i < n; ++i) { + if (A[i] == last) continue; + last = A[i]; + cout << A[i] << (printed + 1 == k ? '\n' : ' '); + ++printed; + } +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + int t; + cin >> t; + while (t--) solve(); + return 0; +} + + diff --git a/codeforces/1062/2167f.cc b/codeforces/1062/2167f.cc new file mode 100644 index 0000000..56281df --- /dev/null +++ b/codeforces/1062/2167f.cc @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include + +using namespace std; +using u32 = uint32_t; +using i32 = int32_t; +using u64 = uint64_t; +using i64 = int64_t; + +void solve() { + u32 n; + i64 k; + cin >> n >> k; + vector> tree(n + 1); + for (u32 i = 0; i < n - 1; ++i) { + u32 u, v; + cin >> u >> v; + tree[u].emplace_back(v); + tree[v].emplace_back(u); + } + + u64 ans = 0; + + vector size(n + 1, 0); + auto dfs = [&](auto &&self, u32 u, u32 par) -> i64 { + i64 ss = 1; + for (auto &v : tree[u]) { + if (v == par) { + continue; + } + ss += self(self, v, u); + } + + return size[u] = ss; + }; + + dfs(dfs, 1, 0); + i64 count = + count_if(begin(size), end(size), [&k](auto const &e) { return e >= k; }); + + auto dfs2 = [&](auto &&self, u32 u, u32 par, i64 count) -> void { + ans += count; + + for (auto v : tree[u]) { + if (v == par) { + continue; + } + + auto su = size[u], sv = size[v]; + size[u] = su - sv; + size[v] = n; + + i64 child_count = count + (size[u] >= k) - (sv >= k); + self(self, v, u, child_count); + + size[u] = su; + size[v] = sv; + } + }; + + dfs2(dfs2, 1, 0, count); + + println("{}", ans); +} + +int main() { // {{{ + std::cin.exceptions(std::cin.failbit); +#ifdef LOCAL + std::cerr.rdbuf(std::cout.rdbuf()); + std::cout.setf(std::ios::unitbuf); + std::cerr.setf(std::ios::unitbuf); +#else + std::cin.tie(nullptr)->sync_with_stdio(false); +#endif + u32 tc = 1; + std::cin >> tc; + for (u32 t = 0; t < tc; ++t) { + solve(); + } + return 0; +} +// }}} +