diff --git a/codeforces/935/.clang-format b/codeforces/935/.clang-format new file mode 100644 index 0000000..e7350c4 --- /dev/null +++ b/codeforces/935/.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/935/.clangd b/codeforces/935/.clangd new file mode 100644 index 0000000..7b2523e --- /dev/null +++ b/codeforces/935/.clangd @@ -0,0 +1,39 @@ +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 + -Wno-unknown-pragmas +-e -std=c++23 +-e -std=c++23 +-e -std=c++23 +-e -std=c++23 +-e -std=c++23 +-e -std=c++23 diff --git a/codeforces/935/a.cc b/codeforces/935/a.cc new file mode 100644 index 0000000..596c4fd --- /dev/null +++ b/codeforces/935/a.cc @@ -0,0 +1,110 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// 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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 a, b, c; + cin >> a >> b >> c; + + u32 ans = a + b / 3; + + b -= 3 * (b / 3); + + if (b > 0) { + if (b + c >= 3) { + c -= 3 - b; + ++ans; + } else { + cout << "-1\n"; + return; + } + } + + ans += ceil(c / 3.0); + + cout << ans << '\n'; +} + +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; +} +// }}} diff --git a/codeforces/935/b.cc b/codeforces/935/b.cc new file mode 100644 index 0000000..5a478a1 --- /dev/null +++ b/codeforces/935/b.cc @@ -0,0 +1,95 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// 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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u64 a, b, m; + cin >> a >> b >> m; + + u64 ans = (m + 1 + a - 1) / a + (m + 1 + b - 1) / b; + 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; +} +// }}} diff --git a/codeforces/935/c.cc b/codeforces/935/c.cc new file mode 100644 index 0000000..33725ff --- /dev/null +++ b/codeforces/935/c.cc @@ -0,0 +1,129 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// 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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + i32 n; + cin >> n; + string s; + cin >> s; + + u32 zero = 0; + u32 one = count_if(all(s), [](char c) { + return c == '1'; + }); + + i32 best_index = n; + f64 best_score = n; + for (i32 i = 0; i <= n; ++i) { + if (i > 0) { + zero += (s[i - 1] == '0'); + one -= (s[i - 1] == '1'); + } + auto left_count = i; + auto right_count = n - left_count; + bool valid = + zero >= (left_count + 2 - 1) / 2 && one >= (right_count + 2 - 1) / 2; + + if (!valid) { + continue; + } + + auto score = abs(n / 2.0 - i); + + if (score <= best_score) { + if (score == best_score) { + best_index = min(best_index, i); + } else { + best_index = i; + } + best_score = score; + } + } + + println("{}", best_index); +} + +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; +} +// }}} diff --git a/codeforces/935/compile_flags.txt b/codeforces/935/compile_flags.txt new file mode 100644 index 0000000..17821f8 --- /dev/null +++ b/codeforces/935/compile_flags.txt @@ -0,0 +1,31 @@ +-pedantic-errors +-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 +-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/935/d.cc b/codeforces/935/d.cc new file mode 100644 index 0000000..4d3b086 --- /dev/null +++ b/codeforces/935/d.cc @@ -0,0 +1,118 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// 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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n, m; + cin >> n >> m; + + // NOTE: missed, think big picture + // consider pos n - can either jump one and pay a[n], or skip and pay b[n] (static, must pay b[n]) + // so - just do the best, lol + // then, brute-force consider any choices from 1-m + + vec a(n), b(n); + for (auto& e : a) + cin >> e; + for (auto& e : b) + cin >> e; + + i32 pos = n; + u64 acc = 0; + for (i32 i = n - 1; i >= m; --i) { + acc += min(a[i], b[i]); + } + + u64 ans = MAX; + u64 prefix = 0; + for (i32 i = m - 1; i >= 0; --i) { + ans = min(ans, acc + prefix + a[i]); + prefix += min(a[i], b[i]); + } + + 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; +} +// }}} diff --git a/codeforces/935/debug_flags.txt b/codeforces/935/debug_flags.txt new file mode 100644 index 0000000..62a0135 --- /dev/null +++ b/codeforces/935/debug_flags.txt @@ -0,0 +1,14 @@ +-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 +-DLOCAL +-std=c++23 diff --git a/codeforces/935/e.cc b/codeforces/935/e.cc new file mode 100644 index 0000000..1433a84 --- /dev/null +++ b/codeforces/935/e.cc @@ -0,0 +1,109 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// 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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n, x, X; + cin >> n >> x; + vec a(n + 1); + for (u32 i = 1; i <= n; ++i) { + cin >> a[i]; + if (a[i] == x) + X = i; + } + + u32 l = 1, r = n + 1; + while (r - l > 1) { + auto m = (l + r) / 2; + if (a[m] <= x) + l = m; + else + r = m; + } + + println("1\n{} {}", X, l); +} + +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; +} +// }}} diff --git a/codeforces/935/f.cc b/codeforces/935/f.cc new file mode 100644 index 0000000..ee3a497 --- /dev/null +++ b/codeforces/935/f.cc @@ -0,0 +1,125 @@ +#include // {{{ + +#include +#ifdef __cpp_lib_ranges_enumerate +#include +namespace rv = std::views; +namespace rs = std::ranges; +#endif + +// 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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +#include +#include +using namespace __gnu_pbds; + +void solve() { + u32 n; + cin >> n; + + vec a(n); + for (auto& e : a) + cin >> e; + + vec permutation(n); + for (auto& e : permutation) + cin >> e; + + __gnu_pbds::tree, __gnu_pbds::null_type, less>, + __gnu_pbds::rb_tree_tag, + __gnu_pbds::tree_order_statistics_node_update> + tree; + for (u32 i = 0; i < n; ++i) + tree.insert({a[i], i}); + + u64 ans = 0, count = 0; + for (u32 i = 0; i < n; ++i) { + u32 k = min(i + 1, (u32)tree.size()); + u64 score = (u64)k * tree.find_by_order(tree.size() - k)->first; + if (score > ans) { + ans = score; + count = k; + } + u32 j = permutation[i] - 1; + tree.erase({a[j], j}); + } + + println("{} {}", ans, count); +} + +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; +} +// }}} diff --git a/codeforces/935/io/a.in b/codeforces/935/io/a.in new file mode 100644 index 0000000..42d7034 --- /dev/null +++ b/codeforces/935/io/a.in @@ -0,0 +1,11 @@ +10 +1 2 3 +1 4 1 +1 4 2 +1 1 1 +1 3 2 +19 7 18 +0 0 0 +7 0 0 +0 24 0 +1000000000 1000000000 1000000000 diff --git a/codeforces/935/io/a.out b/codeforces/935/io/a.out new file mode 100644 index 0000000..0727676 --- /dev/null +++ b/codeforces/935/io/a.out @@ -0,0 +1,14 @@ +3 +-1 +3 +-1 +3 +28 +0 +7 +8 +1666666667 + +[code]: 0 +[time]: 2.23827 ms +[debug]: false \ No newline at end of file diff --git a/codeforces/935/io/b.in b/codeforces/935/io/b.in new file mode 100644 index 0000000..69b526a --- /dev/null +++ b/codeforces/935/io/b.in @@ -0,0 +1,7 @@ +6 +6 7 4 +3 4 10 +7 8 56 +5 6 78123459896 +1 1 1 +1 1 1000000000000000000 diff --git a/codeforces/935/io/b.out b/codeforces/935/io/b.out new file mode 100644 index 0000000..c394bb0 --- /dev/null +++ b/codeforces/935/io/b.out @@ -0,0 +1,10 @@ +2 +7 +17 +28645268630 +4 +2000000000000000002 + +[code]: 0 +[time]: 2.33006 ms +[debug]: false \ No newline at end of file diff --git a/codeforces/935/io/c.in b/codeforces/935/io/c.in new file mode 100644 index 0000000..391525c --- /dev/null +++ b/codeforces/935/io/c.in @@ -0,0 +1,16 @@ +7 +3 +101 +6 +010111 +6 +011001 +3 +000 +3 +110 +3 +001 +4 +1100 + diff --git a/codeforces/935/io/c.out b/codeforces/935/io/c.out new file mode 100644 index 0000000..7c90dee --- /dev/null +++ b/codeforces/935/io/c.out @@ -0,0 +1,11 @@ +2 +3 +2 +3 +0 +1 +0 + +[code]: 0 +[time]: 2.31981 ms +[debug]: false \ No newline at end of file diff --git a/codeforces/935/io/d.in b/codeforces/935/io/d.in new file mode 100644 index 0000000..cd96368 --- /dev/null +++ b/codeforces/935/io/d.in @@ -0,0 +1,13 @@ +4 +4 2 +7 3 6 9 +4 3 8 5 +6 2 +6 9 7 1 8 3 +5 8 8 1 4 1 +7 7 +7 2 9 2 6 5 9 +9 1 10 7 1 4 9 +2 1 +2 3 +1 1 diff --git a/codeforces/935/io/d.out b/codeforces/935/io/d.out new file mode 100644 index 0000000..78ba30f --- /dev/null +++ b/codeforces/935/io/d.out @@ -0,0 +1,8 @@ +14 +22 +9 +3 + +[code]: 0 +[time]: 2.46668 ms +[debug]: false \ No newline at end of file diff --git a/codeforces/935/io/e.in b/codeforces/935/io/e.in new file mode 100644 index 0000000..37dd7b0 --- /dev/null +++ b/codeforces/935/io/e.in @@ -0,0 +1,11 @@ +5 +6 3 +1 2 3 4 5 6 +6 5 +3 1 6 5 2 4 +5 1 +3 5 4 2 1 +6 3 +4 3 1 5 2 6 +3 2 +3 2 1 diff --git a/codeforces/935/io/e.out b/codeforces/935/io/e.out new file mode 100644 index 0000000..7dc83de --- /dev/null +++ b/codeforces/935/io/e.out @@ -0,0 +1,14 @@ +1 +3 3 +1 +4 6 +1 +5 1 +1 +2 3 +1 +2 3 + +[code]: 0 +[time]: 2.33626 ms +[debug]: false \ No newline at end of file diff --git a/codeforces/935/io/f.in b/codeforces/935/io/f.in new file mode 100644 index 0000000..193b023 --- /dev/null +++ b/codeforces/935/io/f.in @@ -0,0 +1,19 @@ +6 +3 +9 8 14 +3 2 1 +5 +1 2 3 4 5 +1 2 3 4 5 +6 +1 2 3 4 5 6 +6 5 4 3 2 1 +5 +1 4 6 10 10 +2 1 4 5 3 +4 +2 2 5 5 +4 2 3 1 +5 +1 2 9 10 10 +1 4 2 3 5 diff --git a/codeforces/935/io/f.out b/codeforces/935/io/f.out new file mode 100644 index 0000000..8f5be2f --- /dev/null +++ b/codeforces/935/io/f.out @@ -0,0 +1,10 @@ +16 2 +9 3 +8 2 +20 2 +5 1 +20 2 + +[code]: 0 +[time]: 2.29096 ms +[debug]: false \ No newline at end of file diff --git a/codeforces/935/makefile b/codeforces/935/makefile new file mode 100644 index 0000000..3c50267 --- /dev/null +++ b/codeforces/935/makefile @@ -0,0 +1,30 @@ +.PHONY: run debug clean setup init + +VERSION ?= 20 + +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 .clang-format || cp $(HOME)/.config/cp-template/.clang-format . + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . && echo -std=c++$(VERSION) >>compile_flags.txt + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . && echo -e "\t\t-std=c++$(VERSION)" >>.clangd + +init: + make setup + +%: + @: diff --git a/codeforces/935/scripts/debug.sh b/codeforces/935/scripts/debug.sh new file mode 100644 index 0000000..1e63f37 --- /dev/null +++ b/codeforces/935/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" true +exit $? diff --git a/codeforces/935/scripts/run.sh b/codeforces/935/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/codeforces/935/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/935/scripts/utils.sh b/codeforces/935/scripts/utils.sh new file mode 100644 index 0000000..e4cf8f8 --- /dev/null +++ b/codeforces/935/scripts/utils.sh @@ -0,0 +1,61 @@ +#!/bin/sh + +execute_binary() { + binary="$1" + input="$2" + output="$3" + is_debug="$4" + + start=$(date '+%s.%N') + if [ -n "$is_debug" ]; then + asan="$(ldconfig -p | grep libasan.so | head -n1 | awk '{print $4}')" + LD_PRELOAD="$asan" timeout 2s ./"$binary" <"$input" >"$output" 2>&1 + else + timeout 2s ./"$binary" <"$input" >"$output" 2>&1 + fi + 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 + test -n "$is_debug" && is_debug_string=true || is_debug_string=false + printf '\n[debug]: %s' "$is_debug_string" >>$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 +}