From 4094b4cd7b12ebaa2f1c6ecb8e2c71b2da32d9e2 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Mon, 21 Apr 2025 14:37:25 -0400 Subject: [PATCH] feat(codeforces/970): a-f --- codeforces/886/io/c.out | 2 +- codeforces/970/.clang-format | 9 ++ codeforces/970/.clangd | 34 +++++++ codeforces/970/a.cc | 87 +++++++++++++++++ codeforces/970/b.cc | 130 ++++++++++++++++++++++++ codeforces/970/c.cc | 91 +++++++++++++++++ codeforces/970/compile_flags.txt | 31 ++++++ codeforces/970/d.cc | 163 +++++++++++++++++++++++++++++++ codeforces/970/debug_flags.txt | 12 +++ codeforces/970/e.cc | 122 +++++++++++++++++++++++ codeforces/970/f.cc | 58 +++++++++++ codeforces/970/io/a.in | 6 ++ codeforces/970/io/a.out | 8 ++ codeforces/970/io/b.in | 11 +++ codeforces/970/io/b.out | 8 ++ codeforces/970/io/c.in | 6 ++ codeforces/970/io/c.out | 8 ++ codeforces/970/io/d.in | 16 +++ codeforces/970/io/d.out | 8 ++ codeforces/970/io/e.in | 21 ++++ codeforces/970/io/e.out | 13 +++ codeforces/970/io/f.in | 7 ++ codeforces/970/io/f.out | 6 ++ codeforces/970/makefile | 28 ++++++ codeforces/970/new | 66 +++++++++++++ codeforces/970/scripts/debug.sh | 29 ++++++ codeforces/970/scripts/run.sh | 29 ++++++ codeforces/970/scripts/utils.sh | 53 ++++++++++ 28 files changed, 1061 insertions(+), 1 deletion(-) create mode 100644 codeforces/970/.clang-format create mode 100644 codeforces/970/.clangd create mode 100644 codeforces/970/a.cc create mode 100644 codeforces/970/b.cc create mode 100644 codeforces/970/c.cc create mode 100644 codeforces/970/compile_flags.txt create mode 100644 codeforces/970/d.cc create mode 100644 codeforces/970/debug_flags.txt create mode 100644 codeforces/970/e.cc create mode 100644 codeforces/970/f.cc create mode 100644 codeforces/970/io/a.in create mode 100644 codeforces/970/io/a.out create mode 100644 codeforces/970/io/b.in create mode 100644 codeforces/970/io/b.out create mode 100644 codeforces/970/io/c.in create mode 100644 codeforces/970/io/c.out create mode 100644 codeforces/970/io/d.in create mode 100644 codeforces/970/io/d.out create mode 100644 codeforces/970/io/e.in create mode 100644 codeforces/970/io/e.out create mode 100644 codeforces/970/io/f.in create mode 100644 codeforces/970/io/f.out create mode 100644 codeforces/970/makefile create mode 100644 codeforces/970/new create mode 100644 codeforces/970/scripts/debug.sh create mode 100644 codeforces/970/scripts/run.sh create mode 100644 codeforces/970/scripts/utils.sh diff --git a/codeforces/886/io/c.out b/codeforces/886/io/c.out index d48b986..d67c96f 100644 --- a/codeforces/886/io/c.out +++ b/codeforces/886/io/c.out @@ -5,4 +5,4 @@ game aaaaaaaa [code]: 0 -[time]: 13.4587 ms \ No newline at end of file +[time]: 13.4587 ms diff --git a/codeforces/970/.clang-format b/codeforces/970/.clang-format new file mode 100644 index 0000000..e7350c4 --- /dev/null +++ b/codeforces/970/.clang-format @@ -0,0 +1,9 @@ +BasedOnStyle: Google +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: false +AllowShortEnumsOnASingleLine: false +AllowShortFunctionsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: false +AllowShortLambdasOnASingleLine: false +AllowShortLoopsOnASingleLine: false diff --git a/codeforces/970/.clangd b/codeforces/970/.clangd new file mode 100644 index 0000000..5b59a31 --- /dev/null +++ b/codeforces/970/.clangd @@ -0,0 +1,34 @@ +CompileFlags: + Add: + -O2 + -Wall + -Wextra + -Wpedantic + -Wshadow + -Wformat=2 + -Wfloat-equal + -Wlogical-op + -Wshift-overflow=2 + -Wnon-virtual-dtor + -Wold-style-cast + -Wcast-qual + -Wuseless-cast + -Wno-sign-promotion + -Wcast-align + -Wunused + -Woverloaded-virtual + -Wconversion + -Wsign-conversion + -Wmisleading-indentation + -Wduplicated-cond + -Wduplicated-branches + -Wlogical-op + -Wnull-dereference + -Wformat=2 + -Wformat-overflow + -Wformat-truncation + -Wdouble-promotion + -Wundef + -DLOCAL + -std=c++20 + -Wno-unknown-pragmas diff --git a/codeforces/970/a.cc b/codeforces/970/a.cc new file mode 100644 index 0000000..9a4c8fe --- /dev/null +++ b/codeforces/970/a.cc @@ -0,0 +1,87 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 ones, twos; + cin >> ones >> twos; + + for (u32 c1 = 0; c1 <= ones; ++c1) { + for (u32 c2 = 0; c2 <= twos; ++c2) { + if (c1 + c2 * 2 == (ones - c1) + (twos - c2) * 2) { + YES(); + return; + } + } + } + NO(); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/970/b.cc b/codeforces/970/b.cc new file mode 100644 index 0000000..f2645b1 --- /dev/null +++ b/codeforces/970/b.cc @@ -0,0 +1,130 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "No\n"; +} + +static void YES() { + std::cout << "Yes\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n; + string s; + cin >> n >> s; + + i64 cols = -1; + for (u32 i = 0; i < n; ++i) { + if (s[i] == '0') { + cols = i - 1; + break; + } + } + + if (cols == -1) { + if (n == 4) + YES(); + else + NO(); + return; + } + + // NOTE: didn't check it was square, holy shit throw + if (cols == 0 || n % cols != 0 || cols * cols != n) { + NO(); + return; + } + + vec> board(n / cols, vec(cols)); + for (u32 i = 0; i < board.size(); ++i) { + for (u32 j = 0; j < board[i].size(); ++j) { + board[i][j] = s[i * cols + j]; + } + } + + u32 zeroes = 0; + for (auto c : s) + if (c == '0') + ++zeroes; + for (u32 i = 0; i < cols; ++i) { + if (s[i] == '0' || s[n - cols + i] == '0') { + NO(); + return; + } + } + for (u32 i = 0; i < n; i += cols) { + if (s[i] == '0' || s[i + cols - 1] == '0') { + NO(); + return; + } + } + + if (n - 2 * cols - 2 * (n / cols) + 4 != zeroes) { + NO(); + } else { + YES(); + } +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/970/c.cc b/codeforces/970/c.cc new file mode 100644 index 0000000..6d1c5ec --- /dev/null +++ b/codeforces/970/c.cc @@ -0,0 +1,91 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + i64 l, r; + cin >> l >> r; + + i64 lo = 0, hi = r - l + 1; + + while (lo <= hi) { + i64 mid = lo + (hi - lo) / 2; + if (l + mid * (mid + 1) / 2 <= r) { + lo = mid + 1; + } else { + hi = mid - 1; + } + } + + // NOTE: hi is # elems ADDING; so also include l implciitly + cout << hi + 1 << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/970/compile_flags.txt b/codeforces/970/compile_flags.txt new file mode 100644 index 0000000..5725a8c --- /dev/null +++ b/codeforces/970/compile_flags.txt @@ -0,0 +1,31 @@ +-O2 +-Wall +-Wextra +-Wpedantic +-Wshadow +-Wformat=2 +-Wfloat-equal +-Wlogical-op +-Wshift-overflow=2 +-Wnon-virtual-dtor +-Wold-style-cast +-Wcast-qual +-Wuseless-cast +-Wno-sign-promotion +-Wcast-align +-Wunused +-Woverloaded-virtual +-Wconversion +-Wsign-conversion +-Wmisleading-indentation +-Wduplicated-cond +-Wduplicated-branches +-Wlogical-op +-Wnull-dereference +-Wformat=2 +-Wformat-overflow +-Wformat-truncation +-Wdouble-promotion +-Wundef +-DLOCAL +-std=c++23 diff --git a/codeforces/970/d.cc b/codeforces/970/d.cc new file mode 100644 index 0000000..550dfb5 --- /dev/null +++ b/codeforces/970/d.cc @@ -0,0 +1,163 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +bitset<2 * 100000 + 1> black; + +void solve() { + black.reset(); + + u32 n; + cin >> n; + vec a(n); + for (auto& e : a) { + cin >> e; + --e; + } + + char c; + for (u32 i = 0; i < n; ++i) { + cin >> c; + if (c == '1') + black[i] = true; + } + + vec cycle_id(n, -1); + vec cycle_pos(n, -1); + vec visited(n, false); + vec black_count(n, 0); + + for (u32 start = 0; start < n; ++start) { + if (visited[start]) + continue; + + u32 slow = start; + u32 fast = start; + + do { + slow = a[slow]; + fast = a[a[fast]]; + } while (slow != fast); + + u32 cycle_start = slow; + int cycle_length = 1; + slow = a[slow]; + + while (slow != cycle_start) { + cycle_length++; + slow = a[slow]; + } + + slow = cycle_start; + int cycle_black = 0; + for (int i = 0; i < cycle_length; ++i) { + cycle_id[slow] = start; + cycle_pos[slow] = i; + visited[slow] = true; + + if (black[slow]) + cycle_black++; + + slow = a[slow]; + } + + black_count[start] = cycle_black; + } + + vec answer(n, 0); + + for (u32 i = 0; i < n; ++i) { + if (cycle_id[i] != -1) { + answer[i] = black_count[cycle_id[i]]; + } else { + vec path; + u32 curr = i; + + while (cycle_id[curr] == -1) { + path.push_back(curr); + curr = a[curr]; + } + + for (u32 node : path) { + if (black[node]) + answer[i]++; + } + + answer[i] += black_count[cycle_id[curr]]; + } + } + + for (u32 i = 0; i < n; ++i) { + cout << answer[i] << ' '; + } + cout << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/970/debug_flags.txt b/codeforces/970/debug_flags.txt new file mode 100644 index 0000000..03cba1b --- /dev/null +++ b/codeforces/970/debug_flags.txt @@ -0,0 +1,12 @@ +-g3 +-fsanitize=address,undefined +-fsanitize=float-divide-by-zero +-fsanitize=float-cast-overflow +-fno-sanitize-recover=all +-fstack-protector-all +-fstack-usage +-fno-omit-frame-pointer +-fno-inline +-ffunction-sections +-D_GLIBCXX_DEBUG +-D_GLIBCXX_DEBUG_PEDANTIC diff --git a/codeforces/970/e.cc b/codeforces/970/e.cc new file mode 100644 index 0000000..f6ed8a0 --- /dev/null +++ b/codeforces/970/e.cc @@ -0,0 +1,122 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +#include +using namespace std; + +void solve() { + u32 n; + cin >> n; + string s; + cin >> s; + int ans = s.size(); + + if (n & 1) { + vector pref[2] = {vector(26), vector(26)}; + vector suf[2] = {vector(26), vector(26)}; + + for (int i = n - 1; i >= 0; i--) { + suf[i % 2][s[i] - 'a']++; + } + + for (int i = 0; i < n; i++) { + suf[i % 2][s[i] - 'a']--; + int odd = n; + + for (int k = 0; k < 2; k++) { + int best = 0; + for (int j = 0; j < 26; j++) { + best = max(best, suf[1 - k][j] + pref[k][j]); + } + odd -= best; + } + + ans = min(ans, odd); + pref[i % 2][s[i] - 'a']++; + } + } else { + vector v[2] = {vector(26), vector(26)}; + for (int i = 0; i < n; i++) { + v[i % 2][s[i] - 'a']++; + } + for (int i = 0; i < 2; i++) { + int best = 0; + for (int j = 0; j < 26; j++) { + best = max(best, v[i][j]); + } + ans -= best; + } + } + + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/970/f.cc b/codeforces/970/f.cc new file mode 100644 index 0000000..ec8d4ff --- /dev/null +++ b/codeforces/970/f.cc @@ -0,0 +1,58 @@ +#include +using namespace std; + +using i64 = long long; +constexpr i64 MOD = 1e9 + 7; + +i64 modinv(i64 a, i64 m) { + i64 m0 = m, y = 0, x = 1; + if (m == 1) + return 0; + while (a > 1) { + i64 q = a / m; + i64 t = m; + m = a % m, a = t; + t = y; + y = x - q * y; + x = t; + } + if (x < 0) + x += m0; + return x; +} + +void solve() { + int n; + cin >> n; + vector a(n); + for (auto &x : a) + cin >> x; + + i64 total_sum = 0, sum_sq = 0; + for (auto x : a) { + total_sum = (total_sum + x) % MOD; + sum_sq = (sum_sq + x * x) % MOD; + } + + i64 sum_pairs = (total_sum * total_sum % MOD - sum_sq + MOD) % MOD; + sum_pairs = sum_pairs * modinv(2, MOD) % MOD; + + i64 total_pairs = (i64)n * (n - 1) / 2 % MOD; + i64 inv_total_pairs = modinv(total_pairs, MOD); + i64 ans = sum_pairs * inv_total_pairs % MOD; + + cout << ans << '\n'; +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + solve(); + } + + return 0; +} diff --git a/codeforces/970/io/a.in b/codeforces/970/io/a.in new file mode 100644 index 0000000..a92e4d7 --- /dev/null +++ b/codeforces/970/io/a.in @@ -0,0 +1,6 @@ +5 +0 1 +0 3 +2 0 +2 3 +3 1 diff --git a/codeforces/970/io/a.out b/codeforces/970/io/a.out new file mode 100644 index 0000000..aee7d5d --- /dev/null +++ b/codeforces/970/io/a.out @@ -0,0 +1,8 @@ +NO +NO +YES +YES +NO + +[code]: 0 +[time]: 10.9363 ms \ No newline at end of file diff --git a/codeforces/970/io/b.in b/codeforces/970/io/b.in new file mode 100644 index 0000000..bad4b7b --- /dev/null +++ b/codeforces/970/io/b.in @@ -0,0 +1,11 @@ +5 +2 +11 +4 +1111 +9 +111101111 +9 +111111111 +12 +111110011111 diff --git a/codeforces/970/io/b.out b/codeforces/970/io/b.out new file mode 100644 index 0000000..d7b9f37 --- /dev/null +++ b/codeforces/970/io/b.out @@ -0,0 +1,8 @@ +No +Yes +Yes +No +No + +[code]: 0 +[time]: 4.70805 ms \ No newline at end of file diff --git a/codeforces/970/io/c.in b/codeforces/970/io/c.in new file mode 100644 index 0000000..e9293d6 --- /dev/null +++ b/codeforces/970/io/c.in @@ -0,0 +1,6 @@ +5 +1 2 +1 5 +2 2 +10 20 +1 1000000000 diff --git a/codeforces/970/io/c.out b/codeforces/970/io/c.out new file mode 100644 index 0000000..994290d --- /dev/null +++ b/codeforces/970/io/c.out @@ -0,0 +1,8 @@ +2 +3 +1 +5 +44721 + +[code]: 0 +[time]: 3.74532 ms \ No newline at end of file diff --git a/codeforces/970/io/d.in b/codeforces/970/io/d.in new file mode 100644 index 0000000..d35ec4b --- /dev/null +++ b/codeforces/970/io/d.in @@ -0,0 +1,16 @@ +5 +1 +1 +0 +5 +1 2 4 5 3 +10101 +5 +5 4 1 3 2 +10011 +6 +3 5 6 1 2 4 +010000 +6 +1 2 3 4 5 6 +100110 diff --git a/codeforces/970/io/d.out b/codeforces/970/io/d.out new file mode 100644 index 0000000..9bad16b --- /dev/null +++ b/codeforces/970/io/d.out @@ -0,0 +1,8 @@ +0 +1 0 2 2 2 +3 3 3 3 3 +0 1 0 0 1 0 +1 0 0 1 1 0 + +[code]: 0 +[time]: 3.73864 ms \ No newline at end of file diff --git a/codeforces/970/io/e.in b/codeforces/970/io/e.in new file mode 100644 index 0000000..c9ea2b9 --- /dev/null +++ b/codeforces/970/io/e.in @@ -0,0 +1,21 @@ +10 +1 +a +2 +ca +3 +aab +5 +ababa +6 +acdada +9 +ejibmyyju +6 +bbccbc +6 +abacba +5 +bcbca +5 +dcbdb diff --git a/codeforces/970/io/e.out b/codeforces/970/io/e.out new file mode 100644 index 0000000..2fa121e --- /dev/null +++ b/codeforces/970/io/e.out @@ -0,0 +1,13 @@ +1 +0 +1 +1 +2 +6 +2 +3 +1 +1 + +[code]: 0 +[time]: 4.22215 ms \ No newline at end of file diff --git a/codeforces/970/io/f.in b/codeforces/970/io/f.in new file mode 100644 index 0000000..3bb70f4 --- /dev/null +++ b/codeforces/970/io/f.in @@ -0,0 +1,7 @@ +3 +3 +3 2 3 +4 +2 2 2 4 +5 +1 2 3 4 5 diff --git a/codeforces/970/io/f.out b/codeforces/970/io/f.out new file mode 100644 index 0000000..0adc382 --- /dev/null +++ b/codeforces/970/io/f.out @@ -0,0 +1,6 @@ +7 +6 +500000012 + +[code]: 0 +[time]: 4.27008 ms \ No newline at end of file diff --git a/codeforces/970/makefile b/codeforces/970/makefile new file mode 100644 index 0000000..9c5450b --- /dev/null +++ b/codeforces/970/makefile @@ -0,0 +1,28 @@ +.PHONY: run debug clean setup init + +SRC = $(word 2,$(MAKECMDGOALS)) + +.SILENT: + +run: + sh scripts/run.sh $(SRC) + +debug: + sh scripts/debug.sh $(SRC) + +clean: + rm -rf build/* + +setup: + test -d build || mkdir -p build + test -d io || mkdir -p io + test -d scripts || mkdir -p scripts + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . + test -f .clang-format || cp $(HOME)/.config/cp-template/.clang-format . + +init: + make setup + +%: + @: diff --git a/codeforces/970/new b/codeforces/970/new new file mode 100644 index 0000000..34d6367 --- /dev/null +++ b/codeforces/970/new @@ -0,0 +1,66 @@ +void solve() { + u32 n; + string s; + cin >> n >> s; + + // Function to calculate minimum ops for alternating even-length string + auto calc_min_ops = [](const vec& even_count, const vec& odd_count, + u32 even_positions, u32 odd_positions) -> u32 { + // Find most frequent characters in each position type + pai max_even = {0, 0}; // (char, count) + pai second_max_even = {0, 0}; + pai max_odd = {0, 0}; + pai second_max_odd = {0, 0}; + + for (u32 c = 0; c < 26; ++c) { + if (even_count[c] > max_even.second) { + second_max_even = max_even; + max_even = {c, even_count[c]}; + } else if (even_count[c] > second_max_even.second) { + second_max_even = {c, even_count[c]}; + } + + if (odd_count[c] > max_odd.second) { + second_max_odd = max_odd; + max_odd = {c, odd_count[c]}; + } else if (odd_count[c] > second_max_odd.second) { + second_max_odd = {c, odd_count[c]}; + } + } + + u32 option1 = + (even_positions - max_even.second) + (odd_positions - max_odd.second); + u32 option2 = MAX; + if (max_even.first != max_odd.first) { + option2 = (even_positions - even_count[max_odd.first]) + + (odd_positions - odd_count[max_even.first]); + } else { + // Try second-best for one position type + u32 sub_option1 = (even_positions - second_max_even.second) + + (odd_positions - max_odd.second); + u32 sub_option2 = (even_positions - max_even.second) + + (odd_positions - second_max_odd.second); + option2 = min(sub_option1, sub_option2); + } + + return min(option1, option2); + }; + + if (n % 2 == 0) { + // Even length case + vec even_count(26, 0); + vec odd_count(26, 0); + + for (u32 i = 0; i < n; ++i) { + if (i % 2 == 0) { + even_count[s[i] - 'a']++; + } else { + odd_count[s[i] - 'a']++; + } + } + + cout << calc_min_ops(even_count, odd_count, n / 2, n / 2) << '\n'; + } else { + // O(n) - use suffix? + } +} diff --git a/codeforces/970/scripts/debug.sh b/codeforces/970/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/codeforces/970/scripts/debug.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +DBG_BIN="${BASE}.debug" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +DBG_BIN="build/$DBG_BIN" + +compile_source "$SRC" "$DBG_BIN" "$OUTPUT" @debug_flags.txt +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$DBG_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/codeforces/970/scripts/run.sh b/codeforces/970/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/codeforces/970/scripts/run.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +RUN_BIN="${BASE}.run" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +RUN_BIN="build/$RUN_BIN" + +compile_source "$SRC" "$RUN_BIN" "$OUTPUT" "" +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$RUN_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/codeforces/970/scripts/utils.sh b/codeforces/970/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/codeforces/970/scripts/utils.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +execute_binary() { + binary="$1" + input="$2" + output="$3" + + start=$(date '+%s.%N') + timeout 2s ./"$binary" <"$input" >"$output" 2>&1 + CODE=$? + end=$(date '+%s.%N') + truncate -s "$(head -n 1000 "$output" | wc -c)" "$output" + + if [ $CODE -ge 124 ]; then + MSG='' + case $CODE in + 124) MSG='TIMEOUT' ;; + 128) MSG='SIGILL' ;; + 130) MSG='SIGABRT' ;; + 131) MSG='SIGBUS' ;; + 136) MSG='SIGFPE' ;; + 135) MSG='SIGSEGV' ;; + 137) MSG='SIGPIPE' ;; + 139) MSG='SIGTERM' ;; + esac + [ $CODE -ne 124 ] && sed -i '$d' "$output" + test -n "$MSG" && printf '\n[code]: %s (%s)' "$CODE" "$MSG" >>"$output" + else + printf '\n[code]: %s' "$CODE" >>"$output" + fi + + printf '\n[time]: %s ms' "$(awk "BEGIN {print ($end - $start) * 1000}")" >>$output + return $CODE +} + +compile_source() { + src="$1" + bin="$2" + output="$3" + flags="$4" + + test -f "$bin" && rm "$bin" || true + g++ @compile_flags.txt $flags "$src" -o "$bin" 2>"$output" + CODE=$? + + if [ $CODE -gt 0 ]; then + printf '\n[code]: %s' "$CODE" >>"$output" + return $CODE + else + echo '' >"$output" + return 0 + fi +}