feat: usaco

This commit is contained in:
Barrett Ruth 2025-04-30 16:28:40 -04:00
parent 659cac6af3
commit 00d23dc313
106 changed files with 2569 additions and 52 deletions

View file

@ -0,0 +1,37 @@
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
-std=c++23
-std=c++23
-std=c++17
-std=c++17

View file

@ -0,0 +1,30 @@
-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++17

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 f64 = double;
using f128 = long double;
#if __cplusplus >= 202002L
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());
}
#endif
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()
#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;
cin >> n;
vector<u64> a(n);
for (auto &e : a)
cin >> e;
sort(all(a));
u64 ans = 0, charge = 1e9;
// for (u32 i = 0; i < n; ++i) {
// auto it = lower_bound(all(a), a[i]);
//
// u32 cnt = distance(it, a.end());
// if (cnt * a[i] > ans) {
// ans = cnt * a[i];
// charge = a[i];
// } else if (cnt * a[i] == ans) {
// charge = min(charge, a[i]);
// }
// }
for (u32 i = 0; i < n; ++i) {
if (ans < (n - i) * a[i]) {
ans = (n - i) * a[i];
charge = a[i];
} else if (ans == (n - i) * a[i]) {
charge = min(charge, a[i]);
}
}
cout << ans << ' ' << charge;
}
int main() { // {{{
cin.tie(nullptr)->sync_with_stdio(false);
cin.exceptions(cin.failbit);
#define PROBLEM_NAME "cow-college"
#ifdef LOCAL
freopen("io/" PROBLEM_NAME ".in", "r", stdin);
freopen("io/" PROBLEM_NAME ".out", "w", stdout);
#endif
solve();
return 0;
}
// }}}

View file

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

View file

@ -0,0 +1,80 @@
#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 f64 = double;
using f128 = long double;
#if __cplusplus >= 202002L
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());
}
#endif
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()
#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;
cin >> n;
vector<u32> a(n);
for (auto &e : a)
cin >> e;
sort(all(a));
u32 ans = 0;
for (u32 i = 0; i < n;) {
++ans;
while (i + 1 < n && a[i + 1] == a[i])
++i;
++i;
}
cout << ans << endl;
}
int main() { // {{{
cin.tie(nullptr)->sync_with_stdio(false);
cin.exceptions(cin.failbit);
u32 tc = 1;
// cin >> tc;
for (u32 t = 0; t < tc; ++t) {
solve();
}
return 0;
}
// }}}

View file

@ -0,0 +1,2 @@
4
1 6 4 6

View file

@ -0,0 +1,3 @@
12 4
[code]: 0
[time]: 4.01235 ms

View file

@ -0,0 +1,2 @@
10
5 9 5 5 10 9 3 1 8 8

View file

@ -0,0 +1,4 @@
6
[code]: 0
[time]: 4.40502 ms

View file

@ -0,0 +1,2 @@
4
1 2 3 4 5 6 100 1000

View file

@ -0,0 +1,3 @@
3
[code]: 0
[time]: 3.99446 ms

View file

@ -0,0 +1,8 @@
7
Bessie 1
Elsie 1
Daisy 2
Gertie 2
Annabelle 3
Maggie 4
Henrietta 4

View file

@ -0,0 +1,3 @@
Tie
[code]: 0
[time]: 3.75843 ms

View file

@ -0,0 +1,93 @@
#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 f64 = double;
using f128 = long double;
#if __cplusplus >= 202002L
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());
}
#endif
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()
#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
// }}}
long long findMinScore(std::vector<int> &a) {
int n = a.size() / 2;
long long S = 0;
for (int i = 0; i < 2 * n; i += 2) {
S += a[i + 1] - a[i];
}
long long min_score = S;
for (int i = 0; i < 2 * n - 1; i++) {
long long diff = a[i + 1] - a[i];
long long score = (i % 2 == 0) ? S - diff : S + diff;
min_score = std::min(min_score, score);
}
long long adjust = a[1] - a[0];
for (int k = 1; k < n; k++) {
adjust += a[2 * k + 1] - 2LL * a[2 * k] + a[2 * k - 1];
}
min_score = std::min(min_score, S - adjust);
return min_score;
}
void solve() {
u32 n;
cin >> n;
vector<i32> a(n * 2);
for (auto &e : a)
cin >> e;
sort(all(a));
cout << findMinScore(a);
}
int main() { // {{{
cin.tie(nullptr)->sync_with_stdio(false);
cin.exceptions(cin.failbit);
u32 tc = 1;
// cin >> tc;
for (u32 t = 0; t < tc; ++t) {
solve();
}
return 0;
}
// }}}

View file

@ -0,0 +1,29 @@
.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 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
%:
@:

View file

@ -0,0 +1,104 @@
#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 f64 = double;
using f128 = long double;
#if __cplusplus >= 202002L
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());
}
#endif
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()
#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
// }}}
vector<string> cows;
void solve() {
u32 n;
cin >> n;
unordered_map<string, u32> m;
u32 x;
string word;
for (u32 i = 0; i < n; ++i) {
cin >> word >> x;
m[word] += x;
}
u32 small = 1e9, ssmall = 1e9;
if (m.size() < cows.size()) {
small = 0;
} else
for (auto &[_, v] : m)
small = min(small, v);
string ans = "Tie";
for (auto it = m.begin(); it != m.end(); ++it) {
if (it->second > small && it->second < ssmall) {
ssmall = it->second;
ans = it->first;
} else if (it->second == ssmall) {
ans = "Tie";
}
}
cout << ans;
}
int main() { // {{{
cin.tie(nullptr)->sync_with_stdio(false);
cin.exceptions(cin.failbit);
cows = {"Bessie", "Elsie", "Daisy", "Gertie",
"Annabelle", "Maggie", "Henrietta"};
#define PROBLEM_NAME "notlast"
#ifdef LOCAL
freopen("io/" PROBLEM_NAME ".in", "r", stdin);
freopen("io/" PROBLEM_NAME ".out", "w", stdout);
#else
freopen(PROBLEM_NAME ".in", "r", stdin);
freopen(PROBLEM_NAME ".out", "w", stdout);
#endif
solve();
return 0;
}
// }}}

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
}