feat(usaco/bronze): simulation

This commit is contained in:
Barrett Ruth 2025-04-26 16:38:04 -04:00
parent bf585a88f9
commit 659cac6af3
15 changed files with 449 additions and 1 deletions

75
usaco/bronze/cbarn.cc Normal file
View file

@ -0,0 +1,75 @@
#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;
template <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
#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> r(n);
for (auto& e : r)
cin >> e;
i64 ans = MAX<i64>;
for (u32 i = 0; i < n; ++i) {
i64 moves = 0;
i64 total = accumulate(all(r), 0LL);
for (i32 j = 0; j < n; ++j) {
total -= r[(i + j) % n];
moves += total;
}
ans = min(ans, moves);
}
cout << ans << '\n';
}
int main() { // {{{
cin.tie(nullptr)->sync_with_stdio(false);
cin.exceptions(cin.failbit);
#define PROBLEM_NAME "cbarn"
#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;
}
// }}}

6
usaco/bronze/io/cbarn.in Normal file
View file

@ -0,0 +1,6 @@
5
4
7
8
6
4

View file

@ -0,0 +1,4 @@
48
[code]: 0
[time]: 5.08571 ms

View file

@ -0,0 +1,5 @@
4
7 Mildred +3
4 Elsie -1
9 Mildred -1
1 Bessie +2

View file

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

View file

@ -3,4 +3,4 @@
2
[code]: 0
[time]: 4.47321 ms
[time]: 4.54783 ms

View file

@ -0,0 +1,7 @@
6
N 10
E 2
S 3
W 4
S 5
E 8

View file

@ -0,0 +1,4 @@
10
[code]: 0
[time]: 4.64821 ms

3
usaco/bronze/io/ttt.in Normal file
View file

@ -0,0 +1,3 @@
COW
XXO
ABC

13
usaco/bronze/io/ttt.out Normal file
View file

@ -0,0 +1,13 @@
2
x=C is false
x=C is false
x=X is false
x=O is true
x=X is false
x=A is false
x=W is false
x=C is false
x=W is false
[code]: 0
[time]: 4.11177 ms

3
usaco/bronze/io/tttt.in Normal file
View file

@ -0,0 +1,3 @@
COW
XCX
ABC

4
usaco/bronze/io/tttt.out Normal file
View file

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

112
usaco/bronze/measurement.cc Normal file
View file

@ -0,0 +1,112 @@
#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;
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()
#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;
map<u32, array<i32, 3>> deltas;
u32 day;
string name;
i32 delta;
for (u32 i = 0; i < n; ++i) {
cin >> day >> name >> delta;
if (name[0] == 'M') {
deltas[day][0] = delta;
} else if (name[0] == 'E') {
deltas[day][1] = delta;
} else {
deltas[day][2] = delta;
}
}
u32 last_winners = 0b111;
vector<i64> vals(3, 0);
i64 ans = 0;
for (auto it = deltas.begin(); it != deltas.end(); ++it) {
for (u32 j = 0; j < 3; ++j) {
vals[j] += it->second[j];
}
i64 large = *max_element(all(vals));
u32 winners = 0b000;
for (u32 j = 0; j < 3; ++j) {
winners |= u32(vals[j] == large) << j;
}
ans += last_winners != winners;
last_winners = winners;
}
cout << ans << '\n';
}
int main() { // {{{
cin.tie(nullptr)->sync_with_stdio(false);
cin.exceptions(cin.failbit);
#define PROBLEM_NAME "measurement"
#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;
}
// }}}

101
usaco/bronze/mowing.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 f64 = double;
using f128 = long double;
template <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
#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;
map<pair<i64, i64>, i64> m;
m[{0, 0}] = 0;
char dir;
i64 delta;
i64 x = 0, y = 0;
i64 ans = MAX<i64>, t = 0;
while (n--) {
cin >> dir >> delta;
i64 dx = 0, dy = 0;
switch (dir) {
case 'N':
dy = 1;
break;
case 'E':
dx = 1;
break;
case 'S':
dy = -1;
break;
case 'W':
dx = -1;
break;
}
for (i64 i = 0; i < abs(delta); ++i) {
++t;
x += dx;
y += dy;
if (m.find({x, y}) != m.end()) {
ans = min(ans, t - m[{x, y}]);
}
m[{x, y}] = t;
}
}
if (ans == MAX<i64>)
cout << "-1\n";
else
cout << ans << '\n';
}
int main() { // {{{
cin.tie(nullptr)->sync_with_stdio(false);
cin.exceptions(cin.failbit);
#define PROBLEM_NAME "mowing"
#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;
}
// }}}

107
usaco/bronze/tttt.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 f64 = double;
using f128 = long double;
template <typename T>
constexpr T MIN = std::numeric_limits<T>::min();
template <typename T>
constexpr T MAX = std::numeric_limits<T>::max();
#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() {
vector<string> grid(3);
for (auto& e : grid)
cin >> e;
u64 single = 0, two = 0;
for (char c = 'A'; c <= 'Z'; ++c) {
i32 ccountr = 0, dcountr = 0, ccountc = 0, dcountc = 0;
bool done = false;
for (u32 i = 0; i < 3 && !done; ++i) {
ccountr = (grid[i][0] == c) + (grid[i][1] == c) + (grid[i][2] == c);
ccountc = (grid[0][i] == c) + (grid[1][i] == c) + (grid[2][i] == c);
if (ccountr == 3 || ccountc == 3) {
++single;
done = true;
}
}
ccountr = (grid[0][0] == c) + (grid[1][1] == c) + (grid[2][2] == c);
ccountc = (grid[2][0] == c) + (grid[1][1] == c) + (grid[0][2] == c);
if (!done && (ccountr == 3 || ccountc == 3))
++single;
for (char d = 'A'; d < c; ++d) {
bool done = false;
for (u32 i = 0; i < 3 && !done; ++i) {
ccountr = (grid[i][0] == c) + (grid[i][1] == c) + (grid[i][2] == c);
dcountr = (grid[i][0] == d) + (grid[i][1] == d) + (grid[i][2] == d);
ccountc = (grid[0][i] == c) + (grid[1][i] == c) + (grid[2][i] == c);
dcountc = (grid[0][i] == d) + (grid[1][i] == d) + (grid[2][i] == d);
if (ccountr + dcountr == 3 && ccountr && dcountr ||
ccountc + dcountc == 3 && ccountc && dcountc) {
++(c == d ? single : two);
done = true;
}
}
ccountr = (grid[0][0] == c) + (grid[1][1] == c) + (grid[2][2] == c);
dcountr = (grid[0][0] == d) + (grid[1][1] == d) + (grid[2][2] == d);
ccountc = (grid[2][0] == c) + (grid[1][1] == c) + (grid[0][2] == c);
dcountc = (grid[2][0] == d) + (grid[1][1] == d) + (grid[0][2] == d);
if (!done && ccountr + dcountr == 3 && ccountr && dcountr) {
++(c == d ? single : two);
} else if (!done && ccountc + dcountc == 3 && ccountc && dcountc) {
++(c == d ? single : two);
}
}
}
cout << single << '\n' << two;
}
int main() { // {{{
cin.tie(nullptr)->sync_with_stdio(false);
cin.exceptions(cin.failbit);
#define PROBLEM_NAME "tttt"
#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;
}
// }}}