feat(codeforces/995): a-e

This commit is contained in:
Barrett Ruth 2025-04-20 14:00:57 -04:00
parent e0a8333591
commit 970b302ab8
23 changed files with 821 additions and 0 deletions

View file

@ -0,0 +1,9 @@
BasedOnStyle: Google
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortCompoundRequirementOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLambdasOnASingleLine: false
AllowShortLoopsOnASingleLine: false

34
codeforces/995/.clangd Normal file
View file

@ -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

89
codeforces/995/a.cc Normal file
View file

@ -0,0 +1,89 @@
#include <bits/stdc++.h> // {{{
// 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 <typename T>
using vec = std::vector<T>;
template <typename T, size_t N>
using arr = std::array<T, N>;
template <typename T1, typename T2>
using pai = std::pair<T1, T2>;
template <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
template <typename T>
[[nodiscard]] static T sc(auto&& x) {
return static_cast<T>(x);
}
template <typename T>
[[nodiscard]] static T sz(auto&& x) {
return static_cast<T>(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;
cin >> n;
vec<u32> a(n), b(n);
for (auto& e : a)
cin >> e;
for (auto& e : b)
cin >> e;
b.emplace_back(0);
u64 ans = 0;
for (u32 i = 0; i < n; ++i) {
if (a[i] > b[i + 1])
ans += a[i] - b[i + 1];
}
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;
}
// }}}

98
codeforces/995/b.cc Normal file
View file

@ -0,0 +1,98 @@
#include <bits/stdc++.h> // {{{
// 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 <typename T>
using vec = std::vector<T>;
template <typename T, size_t N>
using arr = std::array<T, N>;
template <typename T1, typename T2>
using pai = std::pair<T1, T2>;
template <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
template <typename T>
[[nodiscard]] static T sc(auto&& x) {
return static_cast<T>(x);
}
template <typename T>
[[nodiscard]] static T sz(auto&& x) {
return static_cast<T>(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 n, a, b, c;
cin >> n >> a >> b >> c;
i64 ans = 3 * (n / (a + b + c));
i64 rest = n % (a + b + c);
if (rest == 0) {
cout << ans << '\n';
return;
}
if (rest <= a) {
cout << ans + 1 << '\n';
return;
}
rest -= a;
if (rest <= b) {
cout << ans + 2 << '\n';
return;
}
cout << ans + 3 << '\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;
}
// }}}

100
codeforces/995/c.cc Normal file
View file

@ -0,0 +1,100 @@
#include <bits/stdc++.h> // {{{
// 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 <typename T>
using vec = std::vector<T>;
template <typename T, size_t N>
using arr = std::array<T, N>;
template <typename T1, typename T2>
using pai = std::pair<T1, T2>;
template <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
template <typename T>
[[nodiscard]] static T sc(auto&& x) {
return static_cast<T>(x);
}
template <typename T>
[[nodiscard]] static T sz(auto&& x) {
return static_cast<T>(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<3 * 100000 + 1> know;
void solve() {
u64 n, m, k;
cin >> n >> m >> k;
know.reset();
vec<u64> M(m);
for (auto& e : M)
cin >> e;
u64 x;
for (u32 i = 0; i < k; ++i) {
cin >> x;
know[x] = 1;
}
for (auto e : M) {
if (know[e] && k - 1 == n - 1)
cout << '1';
else if (!know[e] && k == n - 1)
cout << '1';
else
cout << '0';
}
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;
}
// }}}

View file

@ -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

101
codeforces/995/d.cc Normal file
View file

@ -0,0 +1,101 @@
#include <bits/stdc++.h> // {{{
// 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 <typename T>
using vec = std::vector<T>;
template <typename T, size_t N>
using arr = std::array<T, N>;
template <typename T1, typename T2>
using pai = std::pair<T1, T2>;
template <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
template <typename T>
[[nodiscard]] static T sc(auto&& x) {
return static_cast<T>(x);
}
template <typename T>
[[nodiscard]] static T sz(auto&& x) {
return static_cast<T>(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() {
u64 n;
i64 x, y;
cin >> n >> x >> y;
vec<i64> a(n);
i64 total = 0;
for (auto& e : a) {
cin >> e;
total += e;
}
sort(all(a));
auto count = [&](i64 target) {
i64 ans = 0;
i32 j = n - 1;
for (i32 i = 0; i < n; ++i) {
while (j > i && a[i] + a[j] > target) {
--j;
}
if (j > i) {
ans += j - i;
}
}
return ans;
};
cout << count(total - x) - count(total - y - 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;
}
// }}}

View file

@ -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

107
codeforces/995/e.cc Normal file
View file

@ -0,0 +1,107 @@
#include <bits/stdc++.h> // {{{
// 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 <typename T>
using vec = std::vector<T>;
template <typename T, size_t N>
using arr = std::array<T, N>;
template <typename T1, typename T2>
using pai = std::pair<T1, T2>;
template <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
template <typename T>
[[nodiscard]] static T sc(auto&& x) {
return static_cast<T>(x);
}
template <typename T>
[[nodiscard]] static T sz(auto&& x) {
return static_cast<T>(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 n, k;
cin >> n >> k;
vec<i64> a(n), b(n);
for (auto& e : a)
cin >> e;
for (auto& e : b) {
cin >> e;
}
i64 ans = 0;
vec<i64> prices;
for (i64 i = 0; i < n; ++i) {
prices.push_back(a[i]);
prices.push_back(b[i]);
}
prices.push_back(0);
sort(all(prices));
prices.erase(unique(all(prices)), prices.end());
sort(all(a));
sort(all(b));
for (i64 price : prices) {
i64 A = n - distance(a.begin(), lower_bound(all(a), price));
i64 B = n - distance(b.begin(), lower_bound(all(b), price));
i64 profit = price * B;
if (B - A <= k) {
ans = max(ans, profit);
}
}
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;
}
// }}}

13
codeforces/995/io/a.in Normal file
View file

@ -0,0 +1,13 @@
4
2
3 2
2 1
1
5
8
3
1 1 1
2 2 2
6
8 2 5 6 2 6
8 2 7 4 3 4

7
codeforces/995/io/a.out Normal file
View file

@ -0,0 +1,7 @@
4
5
1
16
[code]: 0
[time]: 3.49522 ms

5
codeforces/995/io/b.in Normal file
View file

@ -0,0 +1,5 @@
4
12 1 5 3
6 6 7 4
16 3 4 1
1000000000 1 1 1

7
codeforces/995/io/b.out Normal file
View file

@ -0,0 +1,7 @@
5
1
6
1000000000
[code]: 0
[time]: 4.673 ms

13
codeforces/995/io/c.in Normal file
View file

@ -0,0 +1,13 @@
4
4 4 3
1 2 3 4
1 3 4
5 4 3
1 2 3 4
1 3 4
4 4 4
1 2 3 4
1 2 3 4
2 2 1
1 2
2

7
codeforces/995/io/c.out Normal file
View file

@ -0,0 +1,7 @@
0100
0000
1111
10
[code]: 0
[time]: 4.60768 ms

15
codeforces/995/io/d.in Normal file
View file

@ -0,0 +1,15 @@
7
4 8 10
4 6 3 6
6 22 27
4 9 6 3 4 5
3 8 10
3 2 1
3 1 1
2 3 4
3 3 6
3 2 1
4 4 12
3 3 2 1
6 8 8
1 1 2 2 2 3

10
codeforces/995/io/d.out Normal file
View file

@ -0,0 +1,10 @@
4
7
0
0
1
5
6
[code]: 0
[time]: 4.89306 ms

16
codeforces/995/io/e.in Normal file
View file

@ -0,0 +1,16 @@
5
2 0
2 1
3 4
1 1
2
5
3 3
1 5 2
3 6 4
4 3
2 3 2 8
3 7 3 9
3 1
2 9 5
12 14 9

8
codeforces/995/io/e.out Normal file
View file

@ -0,0 +1,8 @@
2
5
9
14
15
[code]: 0
[time]: 4.36282 ms

28
codeforces/995/makefile Normal file
View file

@ -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
%:
@:

View file

@ -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 $?

View file

@ -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 $?

View file

@ -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
}