feat(codeforces/1020): a-e

This commit is contained in:
Barrett Ruth 2025-04-24 19:30:13 -04:00
parent 4094b4cd7b
commit 7795f9e52f
25 changed files with 973 additions and 1 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/1020/.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

94
codeforces/1020/a.cc Normal file
View file

@ -0,0 +1,94 @@
#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;
string s;
cin >> s;
u32 ans = 0;
for (auto c : s)
ans += c == '1';
ans *= n;
for (u32 i = 0; i < n; ++i) {
if (s[i] == '0')
++ans;
else
--ans;
}
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;
}
// }}}

92
codeforces/1020/b.cc Normal file
View file

@ -0,0 +1,92 @@
#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, x;
cin >> n >> x;
if (n == 1) {
cout << "0\n";
return;
}
u32 count = n;
for (u32 i = 0; i < x && count; ++i, --count)
cout << i << ' ';
for (u32 i = n - 1; i > x && count; --i, --count)
cout << i << ' ';
if (count)
cout << x << '\n';
else
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;
}
// }}}

119
codeforces/1020/c.cc Normal file
View file

@ -0,0 +1,119 @@
#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;
i64 k;
cin >> n >> k;
vec<i64> a(n), b(n);
for (auto& e : a)
cin >> e;
i64 x;
bool x_found = false;
u32 neg_count = 0;
bool bad = false;
for (u32 i = 0; i < n; ++i) {
cin >> b[i];
if (b[i] == -1) {
++neg_count;
} else {
auto tx = a[i] + b[i];
if (x_found && tx != x) {
bad = true;
continue;
}
x_found = true;
x = tx;
}
}
if (bad) {
cout << "0\n";
return;
}
if (neg_count == n) {
cout << *min_element(all(a)) + k - *max_element(all(a)) + 1 << '\n';
} else {
for (u32 i = 0; i < n; ++i) {
if (!(0 <= x - a[i] && x - a[i] <= k)) {
cout << "0\n";
return;
}
}
cout << "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,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

121
codeforces/1020/d.cc Normal file
View file

@ -0,0 +1,121 @@
#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, m;
cin >> n >> m;
vector<i64> a(n), b(m);
for (auto& e : a)
cin >> e;
for (auto& e : b)
cin >> e;
a.emplace_back(MIN<i64>);
i64 ans = MAX<i32>;
vec<i64> post(m + 1, -1);
post[m] = n;
i64 i = n - 1;
for (i64 j = m - 1; j >= 0 && i >= 0; --j) {
while (i >= 0 && a[i] < b[j]) {
--i;
}
if (post[j] == -1)
post[j] = i;
--i;
}
i64 j = 0;
i64 matched = 0;
for (i = 0; i <= n && j < m; ++i) {
if (post[j + 1] >= i) {
ans = min(ans, b[j]);
}
if (a[i] >= b[j]) {
if (++matched == m) {
ans = 0;
break;
}
++j;
}
}
if (ans == MAX<i32>)
cout << "-1\n";
else
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;
}
// }}}

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

128
codeforces/1020/e.cc Normal file
View file

@ -0,0 +1,128 @@
#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, q;
cin >> n >> q;
vec<u32> a(n);
vec<u32> m(n + 1);
for (u32 i = 0; i < n; ++i) {
cin >> a[i];
m[a[i]] = i;
}
while (q--) {
i64 l, r;
i64 k;
cin >> l >> r >> k;
--l;
--r;
u32 index = m[k];
if (!(l <= index && index <= r)) {
cout << "-1 ";
} else {
u32 L = l, R = r;
u32 lt_need = 0, gt_need = 0, lt_keep = 0, gt_keep = 0;
while (L <= R) {
u32 M = L + (R - L) / 2;
if (M == index) {
if (gt_need + k + gt_keep > n || lt_need + lt_keep >= k) {
cout << "-1 ";
} else {
cout << max(lt_need, gt_need) * 2 << ' ';
}
break;
} else if (M < index) {
if (a[M] > k) {
++lt_need;
} else {
++lt_keep;
}
L = M + 1;
} else {
if (a[M] < k) {
++gt_need;
} else {
++gt_keep;
}
R = M - 1;
}
}
}
}
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;
}
// }}}

11
codeforces/1020/io/a.in Normal file
View file

@ -0,0 +1,11 @@
5
3
101
1
1
5
00000
2
11
3
010

8
codeforces/1020/io/a.out Normal file
View file

@ -0,0 +1,8 @@
5
0
5
2
4
[code]: 0
[time]: 4.38595 ms

8
codeforces/1020/io/b.in Normal file
View file

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

10
codeforces/1020/io/b.out Normal file
View file

@ -0,0 +1,10 @@
0 1 3 2
3 2 1 0
4 3 2 1 0
0
0 1 2
0
0 1 2 3
[code]: 0
[time]: 4.17566 ms

22
codeforces/1020/io/c.in Normal file
View file

@ -0,0 +1,22 @@
7
3 10
1 3 2
-1 -1 1
5 1
0 1 0 0 1
-1 0 1 0 -1
5 1
0 1 0 0 1
-1 1 -1 1 -1
5 10
1 3 2 5 4
-1 -1 -1 -1 -1
5 4
1 3 2 1 3
1 -1 -1 1 -1
5 4
1 3 2 1 3
2 -1 -1 2 0
5 5
5 0 5 4 3
5 -1 -1 -1 -1

10
codeforces/1020/io/c.out Normal file
View file

@ -0,0 +1,10 @@
1
0
0
7
0
1
0
[code]: 0
[time]: 3.83496 ms

22
codeforces/1020/io/d.in Normal file
View file

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

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

@ -0,0 +1,10 @@
6
3
7
0
-1
-1
7
[code]: 0
[time]: 3.83306 ms

30
codeforces/1020/io/e.in Normal file
View file

@ -0,0 +1,30 @@
8
5 3
1 2 3 4 5
1 5 4
1 3 4
3 4 4
7 4
3 1 5 2 7 6 4
3 4 2
2 3 5
1 5 6
1 7 3
2 1
2 1
1 2 1
1 1
1
1 1 1
7 1
3 4 2 5 7 1 6
1 7 1
16 1
16 10 12 6 13 9 14 3 8 11 15 2 7 1 5 4
1 16 4
16 1
14 1 3 15 4 5 6 16 7 8 9 10 11 12 13 2
1 16 14
13 1
12 13 10 9 8 4 11 5 7 6 2 1 3
1 13 2

11
codeforces/1020/io/e.out Normal file
View file

@ -0,0 +1,11 @@
0 -1 0
2 0 -1 4
-1
0
-1
-1
-1
-1
[code]: 0
[time]: 4.73356 ms

28
codeforces/1020/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
%:
@:

51
codeforces/1020/new Normal file
View file

@ -0,0 +1,51 @@
12 13 10 9 8 4 11 5 7 6 2 1 3
l m r
index at 11
M = 7 < index, A[M] =11 > k=2, need one < 2
L = 8, R = 13 M = 10
12 13 10 9 8 4 11 5 7 6 2 1 3
l M r
M = 8 < index, A[M] = 6 > k = 2, need one < 2
L = 11, R = 13, M = 12,
M > index, a[M] < k -> need one > 2
L = 11, R = 11 -> good
overall, needed 2 lt 2, 1 gt 2
12 13 10 9 8 4 1 5 7 6 2 1 3
there aren't 2 lt 2 -> no
1 2 3 4 5 6 7 8 9 10 11 12 13
12 13 10 9 8 4 11 5 7 6 2 1 3
1 13 2
M = 7, 1 lt (11 > 2, Left)
M = 10, 2 lt (6 > 2, Right)
L = 11, R = 13, M = 12, 1 gt
done
7 4
3 1 5 2 7 6 4
3 4 2
2 3 5
1 5 6
1 7 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
14 1 3 15 4 5 6 16 7 8 9 10 11 12 13 2
1 16 14
L = 1, R = 16, M = 8 -> 16, go left (no cost)
L = 1, R = 7, M = 4 -> 15, left (no cost)
L = 1, R = 3, M = 2 -> go left (1 gt)

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
}

View file

@ -5,4 +5,4 @@
44721 44721
[code]: 0 [code]: 0
[time]: 3.74532 ms [time]: 4.08649 ms