From 372c5d20a51ba2f01d8526fe239c2d17db675034 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Fri, 7 Mar 2025 12:52:53 -0500 Subject: [PATCH] feat(cf): 894 --- codeforces/891/.clang-format | 9 ++ codeforces/891/.clangd | 8 ++ codeforces/891/a.cc | 101 ++++++++++++++ codeforces/891/a.in | 15 ++ codeforces/891/a.out | 10 ++ codeforces/891/b.cc | 108 ++++++++++++++ codeforces/891/b.in | 11 ++ codeforces/891/b.out | 13 ++ codeforces/891/c.cc | 215 ++++++++++++++++++++++++++++ codeforces/891/c.in | 3 + codeforces/891/c.out | 4 + codeforces/891/compile_flags.txt | 6 + codeforces/891/d.cc | 113 +++++++++++++++ codeforces/891/d.in | 16 +++ codeforces/891/d.out | 13 ++ codeforces/891/e.cc | 155 +++++++++++++++++++++ codeforces/891/e.in | 7 + codeforces/891/e.out | 6 + codeforces/891/f.cc | 223 +++++++++++++++++++++++++++++ codeforces/891/f.in | 18 +++ codeforces/891/f.out | 6 + codeforces/894/.clang-format | 9 ++ codeforces/894/.clangd | 8 ++ codeforces/894/a.cc | 103 ++++++++++++++ codeforces/894/a.in | 3 + codeforces/894/a.out | 4 + codeforces/894/b.cc | 98 +++++++++++++ codeforces/894/b.in | 13 ++ codeforces/894/b.out | 15 ++ codeforces/894/c.cc | 131 +++++++++++++++++ codeforces/894/c.in | 15 ++ codeforces/894/c.out | 10 ++ codeforces/894/compile_flags.txt | 6 + codeforces/894/d.cc | 94 +++++++++++++ codeforces/894/d.in | 6 + codeforces/894/d.out | 8 ++ codeforces/894/e.cc | 103 ++++++++++++++ codeforces/894/e.in | 13 ++ codeforces/894/e.out | 9 ++ codeforces/894/f.cc | 117 ++++++++++++++++ codeforces/894/f.in | 13 ++ codeforces/894/f.out | 7 + codeforces/894/g.cc | 232 +++++++++++++++++++++++++++++++ codeforces/894/g.in | 34 +++++ codeforces/894/g.out | 86 ++++++++++++ codeforces/974/e.cc | 124 +++++++++++++++++ codeforces/974/e.in | 5 + codeforces/974/e.out | 4 + 48 files changed, 2330 insertions(+) create mode 100644 codeforces/891/.clang-format create mode 100644 codeforces/891/.clangd create mode 100644 codeforces/891/a.cc create mode 100644 codeforces/891/a.in create mode 100644 codeforces/891/a.out create mode 100644 codeforces/891/b.cc create mode 100644 codeforces/891/b.in create mode 100644 codeforces/891/b.out create mode 100644 codeforces/891/c.cc create mode 100644 codeforces/891/c.in create mode 100644 codeforces/891/c.out create mode 100644 codeforces/891/compile_flags.txt create mode 100644 codeforces/891/d.cc create mode 100644 codeforces/891/d.in create mode 100644 codeforces/891/d.out create mode 100644 codeforces/891/e.cc create mode 100644 codeforces/891/e.in create mode 100644 codeforces/891/e.out create mode 100644 codeforces/891/f.cc create mode 100644 codeforces/891/f.in create mode 100644 codeforces/891/f.out create mode 100644 codeforces/894/.clang-format create mode 100644 codeforces/894/.clangd create mode 100644 codeforces/894/a.cc create mode 100644 codeforces/894/a.in create mode 100644 codeforces/894/a.out create mode 100644 codeforces/894/b.cc create mode 100644 codeforces/894/b.in create mode 100644 codeforces/894/b.out create mode 100644 codeforces/894/c.cc create mode 100644 codeforces/894/c.in create mode 100644 codeforces/894/c.out create mode 100644 codeforces/894/compile_flags.txt create mode 100644 codeforces/894/d.cc create mode 100644 codeforces/894/d.in create mode 100644 codeforces/894/d.out create mode 100644 codeforces/894/e.cc create mode 100644 codeforces/894/e.in create mode 100644 codeforces/894/e.out create mode 100644 codeforces/894/f.cc create mode 100644 codeforces/894/f.in create mode 100644 codeforces/894/f.out create mode 100644 codeforces/894/g.cc create mode 100644 codeforces/894/g.in create mode 100644 codeforces/894/g.out create mode 100644 codeforces/974/e.cc create mode 100644 codeforces/974/e.in create mode 100644 codeforces/974/e.out diff --git a/codeforces/891/.clang-format b/codeforces/891/.clang-format new file mode 100644 index 0000000..e7350c4 --- /dev/null +++ b/codeforces/891/.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/891/.clangd b/codeforces/891/.clangd new file mode 100644 index 0000000..4f8ead9 --- /dev/null +++ b/codeforces/891/.clangd @@ -0,0 +1,8 @@ +CompileFlags: + Add: + - -Wall + - -Wextra + - -Wpedantic + - -Wshadow + - -DLOCAL + - -Wno-unknown-pragmas \ No newline at end of file diff --git a/codeforces/891/a.cc b/codeforces/891/a.cc new file mode 100644 index 0000000..4c83917 --- /dev/null +++ b/codeforces/891/a.cc @@ -0,0 +1,101 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +[[nodiscard]] static T MIN() { + return std::numeric_limits::min(); +} + +template +[[nodiscard]] static T MAX() { + return 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()); +} + +#define prln(...) std::println(__VA_ARGS__) +#define pr(...) std::print(__VA_ARGS__) + +#ifdef LOCAL +#define dbgln(...) std::println(__VA_ARGS__) +#define dbg(...) std::print(__VA_ARGS__) +#endif + +inline static void NO() { + prln("NO"); +} + +inline static void YES() { + prln("YES"); +} + +using ll = long long; +using ld = long double; +template +using ve = std::vector; +template +using ar = std::array; +template +using pa = std::pair; +template +using tu = std::tuple; +template +using dq = std::deque; +template +using qu = std::queue; +template +using pq = std::priority_queue; +template +using st = std::stack; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +void solve() { + int n; + cin >> n; + ve a(n); + for (auto &e : a) + cin >> e; + sort(all(a)); + + ll total = 0; + for (int i = 1; i < n; ++i) { + total += a[i]; + } + (a[0] & 1) == (total & 1) ? YES() : NO(); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/891/a.in b/codeforces/891/a.in new file mode 100644 index 0000000..d7b179d --- /dev/null +++ b/codeforces/891/a.in @@ -0,0 +1,15 @@ +7 +8 +1 2 4 3 2 3 5 4 +2 +4 7 +3 +3 9 8 +2 +1 7 +5 +5 4 3 2 1 +4 +4 3 4 5 +2 +50 48 diff --git a/codeforces/891/a.out b/codeforces/891/a.out new file mode 100644 index 0000000..5bd4ae1 --- /dev/null +++ b/codeforces/891/a.out @@ -0,0 +1,10 @@ +YES +NO +YES +YES +NO +YES +YES + +[code]: 0 +[time]: 12.9125 ms \ No newline at end of file diff --git a/codeforces/891/b.cc b/codeforces/891/b.cc new file mode 100644 index 0000000..9848e42 --- /dev/null +++ b/codeforces/891/b.cc @@ -0,0 +1,108 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +[[nodiscard]] static T MIN() { + return std::numeric_limits::min(); +} + +template +[[nodiscard]] static T MAX() { + return 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()); +} + +#define prln(...) std::println(__VA_ARGS__) +#define pr(...) std::print(__VA_ARGS__) + +#ifdef LOCAL +#define dbgln(...) std::println(__VA_ARGS__) +#define dbg(...) std::print(__VA_ARGS__) +#endif + +inline static void NO() { + prln("NO"); +} + +inline static void YES() { + prln("YES"); +} + +using ll = long long; +using ld = long double; +template +using ve = std::vector; +template +using ar = std::array; +template +using pa = std::pair; +template +using tu = std::tuple; +template +using dq = std::deque; +template +using qu = std::queue; +template +using pq = std::priority_queue; +template +using st = std::stack; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +void solve() { + string s; + cin >> s; + + bool carry = false; + int leftmost = sz(s); + + for (int i = sz(s) - 1; i >= 0; --i) { + int d = s[i] - '0' + int(carry); + if (d < 5) + s[i] += carry; + if (carry = d >= 5) + leftmost = i; + } + + if (carry) + pr("1"); + for (int i = 0; i < sz(s); ++i) + pr("{}", (i < leftmost) ? '0' : s[i]); + prln(); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/891/b.in b/codeforces/891/b.in new file mode 100644 index 0000000..0972773 --- /dev/null +++ b/codeforces/891/b.in @@ -0,0 +1,11 @@ +10 +1 +5 +99 +913 +1980 +20444 +20445 +60947 +419860 +40862016542130810467 diff --git a/codeforces/891/b.out b/codeforces/891/b.out new file mode 100644 index 0000000..496d50f --- /dev/null +++ b/codeforces/891/b.out @@ -0,0 +1,13 @@ +0 +15 +199 +1913 +0980 +00000 +00445 +161947 +009860 +00862026542131811467 + +[code]: 0 +[time]: 6.30426 ms \ No newline at end of file diff --git a/codeforces/891/c.cc b/codeforces/891/c.cc new file mode 100644 index 0000000..6436d03 --- /dev/null +++ b/codeforces/891/c.cc @@ -0,0 +1,215 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +[[nodiscard]] static T MIN() { + return std::numeric_limits::min(); +} + +template +[[nodiscard]] static T MAX() { + return 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()); +} + +#define prln(...) std::println(__VA_ARGS__) +#define pr(...) std::print(__VA_ARGS__) + +#ifdef LOCAL +#define dbgln(...) std::println(__VA_ARGS__) +#define dbg(...) std::print(__VA_ARGS__) +#endif + +inline static void NO() { + prln("NO"); +} + +inline static void YES() { + prln("YES"); +} + +using ll = long long; +using ld = long double; +template +using ve = std::vector; +template +using ar = std::array; +template +using pa = std::pair; +template +using tu = std::tuple; +template +using dq = std::deque; +template +using qu = std::queue; +template +using pq = std::priority_queue; +template +using st = std::stack; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +#include +#include + +using namespace __gnu_pbds; + +// https://mirror.codeforces.com/blog/entry/124683 + +namespace hashing { +using i64 = std::int64_t; +using u64 = std::uint64_t; +static const u64 FIXED_RANDOM = + std::chrono::steady_clock::now().time_since_epoch().count(); + +#if USE_AES +std::mt19937 rd(FIXED_RANDOM); +const __m128i KEY1{(i64)rd(), (i64)rd()}; +const __m128i KEY2{(i64)rd(), (i64)rd()}; +#endif + +template +struct custom_hash {}; + +template +inline void hash_combine(u64 &seed, T const &v) { + custom_hash hasher; + seed ^= hasher(v) + 0x9e3779b97f4a7c15 + (seed << 12) + (seed >> 4); +}; + +template +struct custom_hash::value>::type> { + u64 operator()(T _x) const { + u64 x = _x; +#if USE_AES + __m128i m{i64(u64(x) * 0xbf58476d1ce4e5b9u64), (i64)FIXED_RANDOM}; + __m128i y = _mm_aesenc_si128(m, KEY1); + __m128i z = _mm_aesenc_si128(y, KEY2); + return z[0]; +#else + x += 0x9e3779b97f4a7c15 + FIXED_RANDOM; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); +#endif + } +}; + +template +struct custom_hash()))>> { + u64 operator()(T const &a) const { + u64 value = FIXED_RANDOM; + for (auto &x : a) + hash_combine(value, x); + return value; + } +}; + +template +struct custom_hash> { + u64 operator()(const std::tuple &a) const { + u64 value = FIXED_RANDOM; + std::apply( + [&value](T const &...args) { + (hash_combine(value, args), ...); + }, + a); + return value; + } +}; + +template +struct custom_hash> { + u64 operator()(std::pair const &a) const { + u64 value = FIXED_RANDOM; + hash_combine(value, a.first); + hash_combine(value, a.second); + return value; + } +}; +}; // namespace hashing + +#ifdef PB_DS_ASSOC_CNTNR_HPP +template +using hashtable = gp_hash_table< + Key, Value, hashing::custom_hash, std::equal_to, + direct_mask_range_hashing<>, linear_probe_fn<>, + hash_standard_resize_policy, + hash_load_check_resize_trigger<>, true>>; + +#endif +#ifdef PB_DS_TREE_POLICY_HPP +template +using multitree = tree, rb_tree_tag, + tree_order_statistics_node_update>; +template +using rbtree = tree, rb_tree_tag, + tree_order_statistics_node_update>; +#endif + +void solve() { + ll n; + cin >> n; + ve b(n * (n - 1) / 2); + + hashtable m; + for (auto &e : b) { + cin >> e; + ++m[e]; + } + sort(all(b)); + b.erase(unique(all(b)), b.end()); + + ve ans(n); + + int write = n - 1; + int count = 0; + for (int i = sz(b) - 1; i >= 0; --i) { + while (m[b[i]]) { + ans[write--] = b[i]; + m[b[i]] -= count; + ++count; + } + } + + for (auto &e : ans) + pr("{} ", e); + prln(); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/891/c.in b/codeforces/891/c.in new file mode 100644 index 0000000..6e586a3 --- /dev/null +++ b/codeforces/891/c.in @@ -0,0 +1,3 @@ +1 +3 +1 3 1 diff --git a/codeforces/891/c.out b/codeforces/891/c.out new file mode 100644 index 0000000..b08d468 --- /dev/null +++ b/codeforces/891/c.out @@ -0,0 +1,4 @@ +1 3 3 + +[code]: 0 +[time]: 13.2291 ms \ No newline at end of file diff --git a/codeforces/891/compile_flags.txt b/codeforces/891/compile_flags.txt new file mode 100644 index 0000000..504aea8 --- /dev/null +++ b/codeforces/891/compile_flags.txt @@ -0,0 +1,6 @@ +-Wall +-Wextra +-Wpedantic +-Wshadow +-DLOCAL +-std=c++23 diff --git a/codeforces/891/d.cc b/codeforces/891/d.cc new file mode 100644 index 0000000..1b48d5b --- /dev/null +++ b/codeforces/891/d.cc @@ -0,0 +1,113 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +[[nodiscard]] static T MIN() { + return std::numeric_limits::min(); +} + +template +[[nodiscard]] static T MAX() { + return 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()); +} + +#define prln(...) std::println(__VA_ARGS__) +#define pr(...) std::print(__VA_ARGS__) + +#ifdef LOCAL +#define dbgln(...) std::println(__VA_ARGS__) +#define dbg(...) std::print(__VA_ARGS__) +#endif + +inline static void NO() { + prln("NO"); +} + +inline static void YES() { + prln("YES"); +} + +using ll = long long; +using ld = long double; +template +using ve = std::vector; +template +using ar = std::array; +template +using pa = std::pair; +template +using tu = std::tuple; +template +using dq = std::deque; +template +using qu = std::queue; +template +using pq = std::priority_queue; +template +using st = std::stack; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +void solve() { + int n; + cin >> n; + ve a(n), b(n); + for (auto& e : a) + cin >> e; + for (auto& e : b) + cin >> e; + + ll best_diff = MIN(); + ve ans; + for (int i = 0; i < n; ++i) { + auto diff = a[i] - b[i]; + if (diff > best_diff) { + best_diff = diff; + ans = {i}; + } else if (diff == best_diff) { + ans.eb(i); + } + } + + prln("{}", ans.size()); + for (auto& e : ans) + pr("{} ", e + 1); + prln(); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/891/d.in b/codeforces/891/d.in new file mode 100644 index 0000000..33359d1 --- /dev/null +++ b/codeforces/891/d.in @@ -0,0 +1,16 @@ +5 +4 +3 1 2 4 +4 3 2 1 +5 +1 2 4 1 2 +5 2 3 3 1 +2 +1 2 +2 1 +3 +0 2 1 +1 3 2 +3 +5 7 4 +-2 -3 -6 diff --git a/codeforces/891/d.out b/codeforces/891/d.out new file mode 100644 index 0000000..8ba5580 --- /dev/null +++ b/codeforces/891/d.out @@ -0,0 +1,13 @@ +1 +4 +2 +3 5 +1 +2 +3 +1 2 3 +2 +2 3 + +[code]: 0 +[time]: 14.0336 ms diff --git a/codeforces/891/e.cc b/codeforces/891/e.cc new file mode 100644 index 0000000..1661038 --- /dev/null +++ b/codeforces/891/e.cc @@ -0,0 +1,155 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +[[nodiscard]] static T MIN() { + return std::numeric_limits::min(); +} + +template +[[nodiscard]] static T MAX() { + return 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()); +} + +#define prln(...) std::println(__VA_ARGS__) +#define pr(...) std::print(__VA_ARGS__) + +#ifdef LOCAL +#define dbgln(...) std::println(__VA_ARGS__) +#define dbg(...) std::print(__VA_ARGS__) +#endif + +inline static void NO() { + prln("NO"); +} + +inline static void YES() { + prln("YES"); +} + +using ll = long long; +using ld = long double; +template +using ve = std::vector; +template +using ar = std::array; +template +using pa = std::pair; +template +using tu = std::tuple; +template +using dq = std::deque; +template +using qu = std::queue; +template +using pq = std::priority_queue; +template +using st = std::stack; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +void solve() { + int n; + cin >> n; + ve> a(n); + for (int i = 0; i < n; ++i) { + cin >> a[i].ff; + a[i].ss = i; + } + sort(all(a)); + ve prefix(n + 1, 0), postfix(n + 2, 0); + for (int i = 0; i < n; ++i) { + prefix[i + 1] = prefix[i] + a[i].ff; + } + for (int i = n - 1; i >= 0; --i) { + postfix[i + 1] = postfix[i + 2] + a[i].ff; + } + + ve ans(n); + for (int i = 0; i < n; ++i) { + ll x = n + a[i].ff * (i + 1) - prefix[i + 1]; + x += postfix[i + 2] - a[i].ff * (n - i - 1); + ans[a[i].ss] = x; + } + + for (auto& e : ans) + pr("{} ", e); + prln(); + /* + 1 3 4 + + 3 + 0 + + + to query @ i, want to get info related to + a[i] - a[0], a[i] - a[1], .... + + prefix differences + postfix differences for each index + + n + a[i] - a[0] + a[i] - a[1] + ... + a[i] - a[i] + a[j] - a[i] + a[j + 1] + - a[i] + + = n + a[i] * (i + 1) - prefix[i] (inclusive) + postfix[i] (exclusive) - + a[i] * `` + + n * a[i] - sum of prefix before i + sum of postfix after i + + sum a[i] - a[j] + 1 for j in range(i + 1) + count how manny points before us? * a[i] + remove prefix sum up to j + - and postfix! <- note this + + *-** + - + --- + ---- + + --- + - + -- + + ---- + -- + - + + really, we're just adding the inclusive distance between ALL pairs + + + */ +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/891/e.in b/codeforces/891/e.in new file mode 100644 index 0000000..d0772b8 --- /dev/null +++ b/codeforces/891/e.in @@ -0,0 +1,7 @@ +3 +3 +1 4 3 +5 +1 2 5 7 1 +4 +1 10 100 1000 diff --git a/codeforces/891/e.out b/codeforces/891/e.out new file mode 100644 index 0000000..787708e --- /dev/null +++ b/codeforces/891/e.out @@ -0,0 +1,6 @@ +8 7 6 +16 15 18 24 16 +1111 1093 1093 2893 + +[code]: 0 +[time]: 13.531 ms \ No newline at end of file diff --git a/codeforces/891/f.cc b/codeforces/891/f.cc new file mode 100644 index 0000000..14cf244 --- /dev/null +++ b/codeforces/891/f.cc @@ -0,0 +1,223 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +[[nodiscard]] static T MIN() { + return std::numeric_limits::min(); +} + +template +[[nodiscard]] static T MAX() { + return 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()); +} + +#define prln(...) std::println(__VA_ARGS__) +#define pr(...) std::print(__VA_ARGS__) + +#ifdef LOCAL +#define dbgln(...) std::println(__VA_ARGS__) +#define dbg(...) std::print(__VA_ARGS__) +#endif + +inline static void NO() { + prln("NO"); +} + +inline static void YES() { + prln("YES"); +} + +using ll = long long; +using ld = long double; +template +using ve = std::vector; +template +using ar = std::array; +template +using pa = std::pair; +template +using tu = std::tuple; +template +using dq = std::deque; +template +using qu = std::queue; +template +using pq = std::priority_queue; +template +using st = std::stack; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +#include +#include + +using namespace __gnu_pbds; + +// https://mirror.codeforces.com/blog/entry/124683 + +namespace hashing { +using i64 = std::int64_t; +using u64 = std::uint64_t; +static const u64 FIXED_RANDOM = + std::chrono::steady_clock::now().time_since_epoch().count(); + +#if USE_AES +std::mt19937 rd(FIXED_RANDOM); +const __m128i KEY1{(i64)rd(), (i64)rd()}; +const __m128i KEY2{(i64)rd(), (i64)rd()}; +#endif + +template +struct custom_hash {}; + +template +inline void hash_combine(u64 &seed, T const &v) { + custom_hash hasher; + seed ^= hasher(v) + 0x9e3779b97f4a7c15 + (seed << 12) + (seed >> 4); +}; + +template +struct custom_hash::value>::type> { + u64 operator()(T _x) const { + u64 x = _x; +#if USE_AES + __m128i m{i64(u64(x) * 0xbf58476d1ce4e5b9u64), (i64)FIXED_RANDOM}; + __m128i y = _mm_aesenc_si128(m, KEY1); + __m128i z = _mm_aesenc_si128(y, KEY2); + return z[0]; +#else + x += 0x9e3779b97f4a7c15 + FIXED_RANDOM; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); +#endif + } +}; + +template +struct custom_hash()))>> { + u64 operator()(T const &a) const { + u64 value = FIXED_RANDOM; + for (auto &x : a) + hash_combine(value, x); + return value; + } +}; + +template +struct custom_hash> { + u64 operator()(const std::tuple &a) const { + u64 value = FIXED_RANDOM; + std::apply( + [&value](T const &...args) { + (hash_combine(value, args), ...); + }, + a); + return value; + } +}; + +template +struct custom_hash> { + u64 operator()(std::pair const &a) const { + u64 value = FIXED_RANDOM; + hash_combine(value, a.first); + hash_combine(value, a.second); + return value; + } +}; +}; // namespace hashing + +#ifdef PB_DS_ASSOC_CNTNR_HPP +template +using hashtable = gp_hash_table< + Key, Value, hashing::custom_hash, std::equal_to, + direct_mask_range_hashing<>, linear_probe_fn<>, + hash_standard_resize_policy, + hash_load_check_resize_trigger<>, true>>; + +#endif +#ifdef PB_DS_TREE_POLICY_HPP +template +using multitree = tree, rb_tree_tag, + tree_order_statistics_node_update>; +template +using rbtree = tree, rb_tree_tag, + tree_order_statistics_node_update>; +#endif + +void solve() { + int n; + cin >> n; + ve a(n); + hashtable freq; + for (auto &e : a) { + cin >> e; + ++freq[e]; + } + int q; + cin >> q; + while (q--) { + ll x, y; + cin >> x >> y; + + if (x * x < 4 * y) { + pr("0 "); + continue; + } + + ll root = sqrtl(powl(x, 2) - 4 * y); + + ll r0 = (x + root) / 2; + ll r1 = (x - root) / 2; + + if (r0 + r1 != x || r0 * r1 != y) { + pr("0 "); + continue; + } + + if (r0 == r1) { + pr("{} ", (freq[r0] - 1) * freq[r0] / 2); + } else + pr("{} ", freq[r0] * freq[r1]); + } + prln(); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/891/f.in b/codeforces/891/f.in new file mode 100644 index 0000000..624bf53 --- /dev/null +++ b/codeforces/891/f.in @@ -0,0 +1,18 @@ +3 +3 +1 3 2 +4 +3 2 +5 6 +3 1 +5 5 +4 +1 1 1 1 +1 +2 1 +6 +1 4 -2 3 3 3 +3 +2 -8 +-1 -2 +7 12 diff --git a/codeforces/891/f.out b/codeforces/891/f.out new file mode 100644 index 0000000..547989b --- /dev/null +++ b/codeforces/891/f.out @@ -0,0 +1,6 @@ +1 1 0 0 +6 +1 1 3 + +[code]: 0 +[time]: 12.9466 ms \ No newline at end of file diff --git a/codeforces/894/.clang-format b/codeforces/894/.clang-format new file mode 100644 index 0000000..e7350c4 --- /dev/null +++ b/codeforces/894/.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/894/.clangd b/codeforces/894/.clangd new file mode 100644 index 0000000..4f8ead9 --- /dev/null +++ b/codeforces/894/.clangd @@ -0,0 +1,8 @@ +CompileFlags: + Add: + - -Wall + - -Wextra + - -Wpedantic + - -Wshadow + - -DLOCAL + - -Wno-unknown-pragmas \ No newline at end of file diff --git a/codeforces/894/a.cc b/codeforces/894/a.cc new file mode 100644 index 0000000..03c06f9 --- /dev/null +++ b/codeforces/894/a.cc @@ -0,0 +1,103 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +[[nodiscard]] static T MIN() { + return std::numeric_limits::min(); +} + +template +[[nodiscard]] static T MAX() { + return 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()); +} + +#define prln(...) std::println(__VA_ARGS__) +#define pr(...) std::print(__VA_ARGS__) + +#ifdef LOCAL +#define dbgln(...) std::println(__VA_ARGS__) +#define dbg(...) std::print(__VA_ARGS__) +#endif + +using ll = long long; +using ld = long double; +template +using ve = std::vector; +template +using ar = std::array; +template +using pa = std::pair; +template +using tu = std::tuple; +template +using dq = std::deque; +template +using qu = std::queue; +template +using pq = std::priority_queue; +template +using st = std::stack; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +void solve() { + int n, m; + cin >> n >> m; + + vector grid(n); + for (auto &s : grid) + cin >> s; + string letters = "vika"; + int i = 0; + + for (int col = 0; col < m; ++col) { + for (int row = 0; row < n; ++row) { + if (grid[row][col] == letters[i]) { + if (++i == 4) { + cout << "YES\n"; + return; + } + break; + } + } + } + + cout << "NO" << endl; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/894/a.in b/codeforces/894/a.in new file mode 100644 index 0000000..63bc45f --- /dev/null +++ b/codeforces/894/a.in @@ -0,0 +1,3 @@ +1 +1 1 +v diff --git a/codeforces/894/a.out b/codeforces/894/a.out new file mode 100644 index 0000000..c8c8345 --- /dev/null +++ b/codeforces/894/a.out @@ -0,0 +1,4 @@ +NO + +[code]: 0 +[time]: 14.0724 ms \ No newline at end of file diff --git a/codeforces/894/b.cc b/codeforces/894/b.cc new file mode 100644 index 0000000..c69b9ca --- /dev/null +++ b/codeforces/894/b.cc @@ -0,0 +1,98 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +[[nodiscard]] static T MIN() { + return std::numeric_limits::min(); +} + +template +[[nodiscard]] static T MAX() { + return 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()); +} + +#define prln(...) std::println(__VA_ARGS__) +#define pr(...) std::print(__VA_ARGS__) + +#ifdef LOCAL +#define dbgln(...) std::println(__VA_ARGS__) +#define dbg(...) std::print(__VA_ARGS__) +#endif + +using ll = long long; +using ld = long double; +template +using ve = std::vector; +template +using ar = std::array; +template +using pa = std::pair; +template +using tu = std::tuple; +template +using dq = std::deque; +template +using qu = std::queue; +template +using pq = std::priority_queue; +template +using st = std::stack; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +void solve() { + int n; + cin >> n; + ve b(n); + for (auto& e : b) + cin >> e; + + ve ans{b[0]}; + for (int i = 1; i < n; ++i) { + if (ans.back() > b[i]) { + ans.eb(b[i]); + } + ans.eb(b[i]); + } + cout << ans.size() << endl; + for (auto& e : ans) + cout << e << ' '; + cout << endl; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/894/b.in b/codeforces/894/b.in new file mode 100644 index 0000000..4811984 --- /dev/null +++ b/codeforces/894/b.in @@ -0,0 +1,13 @@ +6 +3 +4 6 3 +3 +1 2 3 +5 +1 7 9 5 7 +1 +144 +2 +1 1 +5 +1 2 2 1 1 diff --git a/codeforces/894/b.out b/codeforces/894/b.out new file mode 100644 index 0000000..ec9f69f --- /dev/null +++ b/codeforces/894/b.out @@ -0,0 +1,15 @@ +4 +4 6 3 3 +3 +1 2 3 +6 +1 7 9 5 5 7 +1 +144 +2 +1 1 +6 +1 2 2 1 1 1 + +[code]: 0 +[time]: 14.2317 ms diff --git a/codeforces/894/c.cc b/codeforces/894/c.cc new file mode 100644 index 0000000..de78d1f --- /dev/null +++ b/codeforces/894/c.cc @@ -0,0 +1,131 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +[[nodiscard]] static T MIN() { + return std::numeric_limits::min(); +} + +template +[[nodiscard]] static T MAX() { + return 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()); +} + +#define prln(...) std::println(__VA_ARGS__) +#define pr(...) std::print(__VA_ARGS__) + +#ifdef LOCAL +#define dbgln(...) std::println(__VA_ARGS__) +#define dbg(...) std::print(__VA_ARGS__) +#endif + +using ll = long long; +using ld = long double; +template +using ve = std::vector; +template +using ar = std::array; +template +using pa = std::pair; +template +using tu = std::tuple; +template +using dq = std::deque; +template +using qu = std::queue; +template +using pq = std::priority_queue; +template +using st = std::stack; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +inline static void NO() { + prln("NO"); +} +inline static void YES() { + prln("YES"); +} + +void solve() { + int n; + cin >> n; + ve a(n); + for (auto& e : a) + cin >> e; + + ve b(n, 0); + int write = n - 1; + + int y = 0; + int i = 0; + while (i < n) { + ++y; + while (i + 1 < n && a[i] == a[i + 1]) { + ++y; + ++i; + } + if ((b[write] = y) != a[write]) { + NO(); + return; + }; + --write; + if (i + 1 < n) { + for (int j = 0; j < a[i] - a[i + 1] - 1; ++j) { + if ((b[write] = y) != a[write]) { + NO(); + return; + } + --write; + } + } + ++i; + } + + while (write >= 0) { + if ((b[write] = y) != a[write]) { + NO(); + return; + } + --write; + } + + YES(); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/894/c.in b/codeforces/894/c.in new file mode 100644 index 0000000..91118e5 --- /dev/null +++ b/codeforces/894/c.in @@ -0,0 +1,15 @@ +7 +5 +5 4 3 2 1 +3 +3 1 1 +3 +4 2 1 +1 +2 +5 +5 3 3 1 1 +5 +5 5 5 3 3 +2 +6 1 diff --git a/codeforces/894/c.out b/codeforces/894/c.out new file mode 100644 index 0000000..d117228 --- /dev/null +++ b/codeforces/894/c.out @@ -0,0 +1,10 @@ +YES +YES +NO +NO +YES +YES +NO + +[code]: 0 +[time]: 12.7897 ms \ No newline at end of file diff --git a/codeforces/894/compile_flags.txt b/codeforces/894/compile_flags.txt new file mode 100644 index 0000000..504aea8 --- /dev/null +++ b/codeforces/894/compile_flags.txt @@ -0,0 +1,6 @@ +-Wall +-Wextra +-Wpedantic +-Wshadow +-DLOCAL +-std=c++23 diff --git a/codeforces/894/d.cc b/codeforces/894/d.cc new file mode 100644 index 0000000..36bb698 --- /dev/null +++ b/codeforces/894/d.cc @@ -0,0 +1,94 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +[[nodiscard]] static T MIN() { + return std::numeric_limits::min(); +} + +template +[[nodiscard]] static T MAX() { + return 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()); +} + +#define prln(...) std::println(__VA_ARGS__) +#define pr(...) std::print(__VA_ARGS__) + +#ifdef LOCAL +#define dbgln(...) std::println(__VA_ARGS__) +#define dbg(...) std::print(__VA_ARGS__) +#endif + +using ll = long long; +using ld = long double; +template +using ve = std::vector; +template +using ar = std::array; +template +using pa = std::pair; +template +using tu = std::tuple; +template +using dq = std::deque; +template +using qu = std::queue; +template +using pq = std::priority_queue; +template +using st = std::stack; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +void solve() { + ll n; + cin >> n; + ll l = 1, r = sqrt(2 * n) + 1; + + while (l <= r) { + ll m = l + (r - l) / 2; + if (m * (m - 1) / 2 <= n) { + l = m + 1; + } else + r = m - 1; + } + + cout << r + n - r * (r - 1) / 2 << endl; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/894/d.in b/codeforces/894/d.in new file mode 100644 index 0000000..f7b049a --- /dev/null +++ b/codeforces/894/d.in @@ -0,0 +1,6 @@ +5 +1 +3 +6 +179 +1000000000000000000 diff --git a/codeforces/894/d.out b/codeforces/894/d.out new file mode 100644 index 0000000..ff0a029 --- /dev/null +++ b/codeforces/894/d.out @@ -0,0 +1,8 @@ +2 +3 +4 +27 +2648956421 + +[code]: 0 +[time]: 12.2623 ms \ No newline at end of file diff --git a/codeforces/894/e.cc b/codeforces/894/e.cc new file mode 100644 index 0000000..2d97740 --- /dev/null +++ b/codeforces/894/e.cc @@ -0,0 +1,103 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +[[nodiscard]] static T MIN() { + return std::numeric_limits::min(); +} + +template +[[nodiscard]] static T MAX() { + return 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()); +} + +#define prln(...) std::println(__VA_ARGS__) +#define pr(...) std::print(__VA_ARGS__) + +#ifdef LOCAL +#define dbgln(...) std::println(__VA_ARGS__) +#define dbg(...) std::print(__VA_ARGS__) +#endif + +using ll = long long; +using ld = long double; +template +using ve = std::vector; +template +using ar = std::array; +template +using pa = std::pair; +template +using tu = std::tuple; +template +using dq = std::deque; +template +using qu = std::queue; +template +using pq = std::priority_queue; +template +using st = std::stack; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +ll n, m, d; +void solve() { + cin >> n >> m >> d; + + ll ans = 0; + ll sum = 0; + multiset tree; + ll x; + for (int i = 0; i < n; ++i) { + cin >> x; + if (x > 0) { + sum += x; + tree.insert(x); + } + if (sz(tree) > m) { + sum -= *tree.begin(); + tree.erase(tree.begin()); + } + + ans = max(ans, sum - d * (i + 1)); + } + + prln("{}", ans); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/894/e.in b/codeforces/894/e.in new file mode 100644 index 0000000..f56154d --- /dev/null +++ b/codeforces/894/e.in @@ -0,0 +1,13 @@ +6 +5 2 2 +3 2 5 4 6 +4 3 2 +1 1 1 1 +6 6 6 +-82 45 1 -77 39 11 +5 2 2 +3 2 5 4 8 +2 1 1 +-1 2 +6 3 2 +-8 8 -2 -1 9 0 diff --git a/codeforces/894/e.out b/codeforces/894/e.out new file mode 100644 index 0000000..51bbe06 --- /dev/null +++ b/codeforces/894/e.out @@ -0,0 +1,9 @@ +2 +0 +60 +3 +0 +7 + +[code]: 0 +[time]: 12.3913 ms \ No newline at end of file diff --git a/codeforces/894/f.cc b/codeforces/894/f.cc new file mode 100644 index 0000000..41c326e --- /dev/null +++ b/codeforces/894/f.cc @@ -0,0 +1,117 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +[[nodiscard]] static T MIN() { + return std::numeric_limits::min(); +} + +template +[[nodiscard]] static T MAX() { + return 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()); +} + +#define prln(...) std::println(__VA_ARGS__) +#define pr(...) std::print(__VA_ARGS__) + +#ifdef LOCAL +#define dbgln(...) std::println(__VA_ARGS__) +#define dbg(...) std::print(__VA_ARGS__) +#endif + +inline static void NO() { + prln("NO"); +} + +inline static void YES() { + prln("YES"); +} + +using ll = long long; +using ld = long double; +template +using ve = std::vector; +template +using ar = std::array; +template +using pa = std::pair; +template +using tu = std::tuple; +template +using dq = std::deque; +template +using qu = std::queue; +template +using pq = std::priority_queue; +template +using st = std::stack; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +ll w, f; +void solve() { + cin >> w >> f; + int n; + cin >> n; + ll total = 0; + + ve strengths(n); + for (int i = 0; i < n; ++i) { + cin >> strengths[i]; + total += strengths[i]; + } + + ve dp(total + 1, false); + dp[0] = true; + for (int i = 0; i < n; ++i) { + for (int j = total; j >= strengths[i]; --j) { + dp[j] = dp[j] || dp[j - strengths[i]]; + } + } + + ll ans = MAX(); + for (int i = 0; i <= total; ++i) { + if (dp[i]) { + ans = min(ans, max((i + w - 1) / w, (total - i + f - 1) / f)); + } + } + + cout << ans << endl; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/894/f.in b/codeforces/894/f.in new file mode 100644 index 0000000..a78e084 --- /dev/null +++ b/codeforces/894/f.in @@ -0,0 +1,13 @@ +4 +2 3 +3 +2 6 7 +37 58 +1 +93 +190 90 +2 +23 97 +13 4 +4 +10 10 2 45 diff --git a/codeforces/894/f.out b/codeforces/894/f.out new file mode 100644 index 0000000..7c015b4 --- /dev/null +++ b/codeforces/894/f.out @@ -0,0 +1,7 @@ +3 +2 +1 +5 + +[code]: 0 +[time]: 7.07388 ms diff --git a/codeforces/894/g.cc b/codeforces/894/g.cc new file mode 100644 index 0000000..b34907e --- /dev/null +++ b/codeforces/894/g.cc @@ -0,0 +1,232 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +[[nodiscard]] static T MIN() { + return std::numeric_limits::min(); +} + +template +[[nodiscard]] static T MAX() { + return 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()); +} + +#define prln(...) std::println(__VA_ARGS__) +#define pr(...) std::print(__VA_ARGS__) + +#ifdef LOCAL +#define dbgln(...) std::println(__VA_ARGS__) +#define dbg(...) std::print(__VA_ARGS__) +#endif + +inline static void NO() { + prln("NO"); +} + +inline static void YES() { + prln("YES"); +} + +using ll = long long; +using ld = long double; +template +using ve = std::vector; +template +using ar = std::array; +template +using pa = std::pair; +template +using tu = std::tuple; +template +using dq = std::deque; +template +using qu = std::queue; +template +using pq = std::priority_queue; +template +using st = std::stack; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +#include +#include + +using namespace __gnu_pbds; + +// https://mirror.codeforces.com/blog/entry/124683 + +namespace hashing { +using i64 = std::int64_t; +using u64 = std::uint64_t; +static const u64 FIXED_RANDOM = + std::chrono::steady_clock::now().time_since_epoch().count(); + +#if USE_AES +std::mt19937 rd(FIXED_RANDOM); +const __m128i KEY1{(i64)rd(), (i64)rd()}; +const __m128i KEY2{(i64)rd(), (i64)rd()}; +#endif + +template +struct custom_hash {}; + +template +inline void hash_combine(u64 &seed, T const &v) { + custom_hash hasher; + seed ^= hasher(v) + 0x9e3779b97f4a7c15 + (seed << 12) + (seed >> 4); +}; + +template +struct custom_hash::value>::type> { + u64 operator()(T _x) const { + u64 x = _x; +#if USE_AES + __m128i m{i64(u64(x) * 0xbf58476d1ce4e5b9u64), (i64)FIXED_RANDOM}; + __m128i y = _mm_aesenc_si128(m, KEY1); + __m128i z = _mm_aesenc_si128(y, KEY2); + return z[0]; +#else + x += 0x9e3779b97f4a7c15 + FIXED_RANDOM; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); +#endif + } +}; + +template +struct custom_hash()))>> { + u64 operator()(T const &a) const { + u64 value = FIXED_RANDOM; + for (auto &x : a) + hash_combine(value, x); + return value; + } +}; + +template +struct custom_hash> { + u64 operator()(const std::tuple &a) const { + u64 value = FIXED_RANDOM; + std::apply( + [&value](T const &...args) { + (hash_combine(value, args), ...); + }, + a); + return value; + } +}; + +template +struct custom_hash> { + u64 operator()(std::pair const &a) const { + u64 value = FIXED_RANDOM; + hash_combine(value, a.first); + hash_combine(value, a.second); + return value; + } +}; +}; // namespace hashing + +#ifdef PB_DS_ASSOC_CNTNR_HPP +template +using hashtable = gp_hash_table< + Key, Value, hashing::custom_hash, std::equal_to, + direct_mask_range_hashing<>, linear_probe_fn<>, + hash_standard_resize_policy, + hash_load_check_resize_trigger<>, true>>; + +#endif +#ifdef PB_DS_TREE_POLICY_HPP +template +using multitree = tree, rb_tree_tag, + tree_order_statistics_node_update>; +template +using rbtree = tree, rb_tree_tag, + tree_order_statistics_node_update>; +#endif + +int n, q; +void solve() { + cin >> n; + ve a(n); + multitree values; + + for (auto &e : a) { + cin >> e; + values.insert(e); + } + + multitree tree; + for (auto it = next(values.begin()); it != values.end(); ++it) { + tree.insert(*it - *prev(it)); + } + + int i, x; + cin >> q; + while (q--) { + cin >> i >> x; + --i; + // NOTE: this is not removing the exact right value among duplicates + dbgln("update: i={}, x={}", i, x); + if (i) { + auto it = values.find(a[i]); + dbgln("removing {}", *it - *prev(it)); + tree.erase(*it - *prev(it)); + dbgln("adding diff {}", abs(*prev(it) - x)); + tree.insert(abs(*prev(it) - x)); + } + if (i != n - 1) { + dbgln("removing {}", *it - *next(it)); + tree.erase(*it - *next(it)); + dbgln("adding {}", abs(*next(it) - x)); + tree.insert(abs(*next(it) - x)); + } + values.erase(a[i]); + a[i] = x; + + int ans = *--values.end(); + if (!tree.empty()) + ans += *--tree.end(); + pr("{} ", ans); + } + prln(); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/894/g.in b/codeforces/894/g.in new file mode 100644 index 0000000..00baaed --- /dev/null +++ b/codeforces/894/g.in @@ -0,0 +1,34 @@ +4 +3 +2 4 8 +3 +1 6 +2 10 +3 1 +5 +1 2 2 2 2 +1 +5 3 +2 +5 6 +7 +1 2 +1 7 +1 7 +2 5 +1 2 +2 7 +2 2 +5 +2 5 1 10 6 +10 +1 7 +4 8 +2 5 +1 4 +2 8 +3 4 +1 9 +3 7 +3 4 +3 1 diff --git a/codeforces/894/g.out b/codeforces/894/g.out new file mode 100644 index 0000000..d4ef7fc --- /dev/null +++ b/codeforces/894/g.out @@ -0,0 +1,86 @@ +update: i=0, x=6 +removing -2 +adding 4 +12 update: i=1, x=10 +removing -8 +adding diff 2 +removing -2 +adding 8 +16 update: i=2, x=1 +removing -8 +adding diff 7 +16 +update: i=4, x=3 +removing -2 +adding diff 1 +3 +update: i=0, x=2 +removing -4 +adding 3 +9 update: i=0, x=7 +removing -4 +adding 2 +9 update: i=0, x=7 +removing -4 +adding 2 +9 update: i=1, x=5 +removing -5 +adding diff 1 +9 update: i=0, x=2 +removing -4 +adding 3 +9 update: i=1, x=7 +removing -5 +adding diff 1 +9 update: i=1, x=2 +removing -5 +adding diff 4 +10 +update: i=0, x=7 +removing 2 +adding 6 +16 update: i=3, x=8 +removing -7 +adding diff 2 +removing 2 +adding 7 +17 update: i=1, x=5 +removing -7 +adding diff 5 +removing 2 +adding 4 +17 update: i=0, x=4 +removing 2 +adding 3 +17 update: i=1, x=8 +removing -7 +adding diff 2 +removing 2 +adding 7 +17 update: i=2, x=4 +removing -7 +adding diff 6 +removing 2 +adding 3 +17 update: i=0, x=9 +removing 2 +adding 8 +18 update: i=2, x=7 +removing -7 +adding diff 3 +removing 2 +adding 6 +18 update: i=2, x=4 +removing -7 +adding diff 6 +removing 2 +adding 3 +18 update: i=2, x=1 +removing -7 +adding diff 9 +removing 2 +adding 0 +19 + +[code]: 0 +[time]: 14.2844 ms \ No newline at end of file diff --git a/codeforces/974/e.cc b/codeforces/974/e.cc new file mode 100644 index 0000000..7ee43ae --- /dev/null +++ b/codeforces/974/e.cc @@ -0,0 +1,124 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +[[nodiscard]] static T MIN() { + return std::numeric_limits::min(); +} + +template +[[nodiscard]] static T MAX() { + return 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()); +} + +#define prln(...) std::println(__VA_ARGS__) +#define pr(...) std::print(__VA_ARGS__) + +#ifdef LOCAL +#define dbgln(...) std::println(__VA_ARGS__) +#define dbg(...) std::print(__VA_ARGS__) +#endif + +using ll = long long; +using ld = long double; +template +using ve = std::vector; +template +using ar = std::array; +template +using pa = std::pair; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +void solve() { + int n, m, h; + cin >> n >> m >> h; + unordered_set horses; + + int horse; + for (int i = 0; i < h; ++i) { + cin >> horse; + horses.insert(horse); + } + + ve>> graph(n + 1); + for (int i = 0; i < m; ++i) { + int u, v; + ll w; + cin >> u >> v >> w; + graph[u].eb(v, w); + graph[v].eb(u, w); + } + + ve marian(n + 1, MAX()), robin(n + 1, MAX()); + + auto djikstra = [&](int start, ve& delta) { + priority_queue, ve>, greater<>> + pq; + delta[start] = 0; + pq.emplace(delta[start], start, horses.contains(start)); + unordered_set seen; + + while (!pq.empty()) { + auto [d0, u, took] = pq.top(); + pq.pop(); + if (d0 > delta[u] && !took) continue; + if (took) seen.insert(u); + for (auto [v, w] : graph[u]) { + ll d1 = d0 + (took ? w / 2 : w); + bool take = took || horses.contains(v); + if (d1 < delta[v]) { + delta[v] = d1; + pq.emplace(d1, v, take); + } + } + } + }; + + djikstra(1, marian); + djikstra(n, robin); + + ll ans = MAX(); + for (int node = 1; node <= n; ++node) { + ans = min(ans, max(marian[node], robin[node])); + } + + prln("{}", ans == MAX() ? -1 : ans); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/974/e.in b/codeforces/974/e.in new file mode 100644 index 0000000..378ff46 --- /dev/null +++ b/codeforces/974/e.in @@ -0,0 +1,5 @@ +1 +3 2 1 +2 +1 2 4 +1 3 16 diff --git a/codeforces/974/e.out b/codeforces/974/e.out new file mode 100644 index 0000000..5d9e69e --- /dev/null +++ b/codeforces/974/e.out @@ -0,0 +1,4 @@ +16 + +[code]: 0 +[time]: 14.7145 ms \ No newline at end of file