codeforces: 1003

This commit is contained in:
Barrett Ruth 2025-02-09 15:59:43 -05:00
parent 7440bc2937
commit d1b961e1c4
60 changed files with 2235 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

8
codeforces/996/.clangd Normal file
View file

@ -0,0 +1,8 @@
CompileFlags:
Add:
- -std=c++23
- -Wall
- -Wextra
- -Wpedantic
- -Wshadow
- -Wno-unknown-pragmas

84
codeforces/996/a.cc Normal file
View file

@ -0,0 +1,84 @@
#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;
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);
}
[[nodiscard]] static int sz(auto &&x) {
return static_cast<int>(x.size());
}
template <typename... Args>
void pr(std::format_string<Args...> fmt, Args &&...args) {
std::print(fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void prln(std::format_string<Args...> fmt, Args &&...args) {
std::println(fmt, std::forward<Args>(args)...);
}
using ll = long long;
using ld = long double;
template <typename T>
using vec = std::vector<T>;
#define ff first
#define ss second
#define eb emplace_back
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend() // }}}
#define FORI(a, b, c) for (int a = (b); a < (c); ++a)
#define ROFI(a, b, c) for (int a = (b); a > (c); --a)
#define FORLL(a, b, c) for (ll a = (b); a < (c); ++a)
#define ROFLL(a, b, c) for (ll a = (b); a > (c); --a)
void solve() {
string a;
cin >> a;
if (!(a.size() > 2)) {
std::println("NO");
return;
}
if (!(a[0] == '1' && a[1] == '0')) {
std::println("NO");
return;
}
int x = sz(a);
if (stoi(a.substr(2, a.size())) < 2 || a[2] == '0') {
std::println("NO");
} else {
std::println("YES");
}
}
int main() { // {{{
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
} //}}}

8
codeforces/996/a.in Normal file
View file

@ -0,0 +1,8 @@
7
100
1010
101
105
2033
1019
1002

10
codeforces/996/a.out Normal file
View file

@ -0,0 +1,10 @@
NO
YES
NO
YES
NO
YES
NO
[code]: 0
[time]: 12.1806 ms

96
codeforces/996/b.cc Normal file
View file

@ -0,0 +1,96 @@
#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;
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());
}
template <typename... Args>
void pr(std::format_string<Args...> fmt, Args&&... args) {
std::print(fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void pr(std::format_string<Args...> fmt) {
std::print(fmt);
}
template <typename... Args>
void prln(std::format_string<Args...> fmt, Args&&... args) {
std::println(fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void prln(std::format_string<Args...> fmt) {
std::println(fmt);
}
using ll = long long;
using ld = long double;
template <typename T>
using vec = std::vector<T>;
#define ff first
#define ss second
#define eb emplace_back
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define FORI(a, b, c) for (int a = (b); a < (c); ++a)
#define ROFI(a, b, c) for (int a = (b); a > (c); --a)
#define FORLL(a, b, c) for (ll a = (b); a < (c); ++a)
#define ROFLL(a, b, c) for (ll a = (b); a > (c); --a) // }}}
void solve() {
int n;
cin >> n;
int a;
cin >> a;
int l = a, r = a;
bool done = false;
while (--n) {
cin >> a;
if (!(a == l - 1 || a == r + 1)) {
done = true;
}
l = min(l, a);
r = max(r, a);
}
if (done)
prln("NO");
else
prln("YES");
}
int main() { // {{{
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
} //}}}

9
codeforces/996/b.in Normal file
View file

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

11
codeforces/996/b.out Normal file
View file

@ -0,0 +1,11 @@
n=5
NO
n=3
YES
n=4
YES
n=5
NO
[code]: 0
[time]: 11.2283 ms

210
codeforces/996/c.cc Normal file
View file

@ -0,0 +1,210 @@
#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;
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());
}
template <typename... Args>
void pr(std::format_string<Args...> fmt, Args &&...args) {
std::print(fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void pr(std::format_string<Args...> fmt) {
std::print(fmt);
}
template <typename... Args>
void prln(std::format_string<Args...> fmt, Args &&...args) {
std::println(fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void prln(std::format_string<Args...> fmt) {
std::println(fmt);
}
using ll = long long;
using ld = long double;
template <typename T>
using vec = std::vector<T>;
#define ff first
#define ss second
#define eb emplace_back
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define FORI(a, b, c) for (int a = (b); a < (c); ++a)
#define ROFI(a, b, c) for (int a = (b); a > (c); --a)
#define FORLL(a, b, c) for (ll a = (b); a < (c); ++a)
#define ROFLL(a, b, c) for (ll a = (b); a > (c); --a) // }}}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// https://mirror.codeforces.com/blog/entry/124683
namespace hashing {
using i64 = std::int64_t;
using u64 = std::uint64_t;
static const u64 FIXED_RANDOM =
std::chrono::steady_clock::now().time_since_epoch().count();
#if USE_AES
std::mt19937 rd(FIXED_RANDOM);
const __m128i KEY1{(i64)rd(), (i64)rd()};
const __m128i KEY2{(i64)rd(), (i64)rd()};
#endif
template <class T, class D = void>
struct custom_hash {};
template <class T>
inline void hash_combine(u64 &seed, T const &v) {
custom_hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b97f4a7c15 + (seed << 12) + (seed >> 4);
};
template <class T>
struct custom_hash<T,
typename std::enable_if<std::is_integral<T>::value>::type> {
u64 operator()(T _x) const {
u64 x = _x;
#if USE_AES
__m128i m{i64(u64(x) * 0xbf58476d1ce4e5b9u64), (i64)FIXED_RANDOM};
__m128i y = _mm_aesenc_si128(m, KEY1);
__m128i z = _mm_aesenc_si128(y, KEY2);
return z[0];
#else
x += 0x9e3779b97f4a7c15 + FIXED_RANDOM;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
#endif
}
};
template <class T>
struct custom_hash<T, std::void_t<decltype(std::begin(std::declval<T>()))>> {
u64 operator()(T const &a) const {
u64 value = FIXED_RANDOM;
for (auto &x : a)
hash_combine(value, x);
return value;
}
};
template <class... T>
struct custom_hash<std::tuple<T...>> {
u64 operator()(const std::tuple<T...> &a) const {
u64 value = FIXED_RANDOM;
std::apply(
[&value](T const &...args) {
(hash_combine(value, args), ...);
},
a);
return value;
}
};
template <class T, class U>
struct custom_hash<std::pair<T, U>> {
u64 operator()(std::pair<T, U> const &a) const {
u64 value = FIXED_RANDOM;
hash_combine(value, a.first);
hash_combine(value, a.second);
return value;
}
};
}; // namespace hashing
#ifdef PB_DS_ASSOC_CNTNR_HPP
template <class Key, class Value = null_type>
using hashtable = gp_hash_table<
Key, Value, hashing::custom_hash<Key>, std::equal_to<Key>,
direct_mask_range_hashing<>, linear_probe_fn<>,
hash_standard_resize_policy<hash_exponential_size_policy<>,
hash_load_check_resize_trigger<>, true>>;
#endif
#ifdef PB_DS_TREE_POLICY_HPP
template <typename T>
using multiset = tree<T, null_type, std::less_equal<T>, rb_tree_tag,
tree_order_statistics_node_update>;
template <class Key, class Value = null_type>
using rbtree = tree<Key, Value, std::less<Key>, rb_tree_tag,
tree_order_statistics_node_update>;
#endif
void solve() {
int n;
cin >> n;
vec<int> a(n);
for (auto &e : a)
cin >> e;
int m;
cin >> m;
hashtable<int, int> a2s, s2a;
while (m--) {
string s;
cin >> s;
if (s.size() != a.size()) {
prln("NO");
continue;
}
bool done = false;
FORI(i, 0, sz<int>(a)) {
if (a2s.find(a[i]) == a2s.end() && s2a.find(s[i]) == s2a.end()) {
a2s[a[i]] = s[i];
s2a[s[i]] = a[i];
} else if (a2s[a[i]] != s[i] || s2a[s[i]] != a[i]) {
done = true;
break;
}
}
if (done)
prln("NO");
else
prln("YES");
a2s.clear();
s2a.clear();
}
}
int main() { // {{{
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
} //}}}

19
codeforces/996/c.in Normal file
View file

@ -0,0 +1,19 @@
3
5
3 5 2 1 3
2
abfda
afbfa
2
1 2
3
ab
abc
aa
4
5 -3 5 -3
4
aaaa
bcbc
aba
cbcb

12
codeforces/996/c.out Normal file
View file

@ -0,0 +1,12 @@
YES
NO
YES
NO
NO
NO
YES
NO
YES
[code]: 0
[time]: 4.55689 ms

View file

@ -0,0 +1,5 @@
-std=c++23
-Wall
-Wextra
-Wpedantic
-Wshadow

103
codeforces/996/d.cc Normal file
View file

@ -0,0 +1,103 @@
#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;
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());
}
template <typename... Args>
void pr(std::format_string<Args...> fmt, Args&&... args) {
std::print(fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void pr(std::format_string<Args...> fmt) {
std::print(fmt);
}
template <typename... Args>
void prln(std::format_string<Args...> fmt, Args&&... args) {
std::println(fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void prln(std::format_string<Args...> fmt) {
std::println(fmt);
}
using ll = long long;
using ld = long double;
template <typename T>
using vec = std::vector<T>;
#define ff first
#define ss second
#define eb emplace_back
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define FORI(a, b, c) for (int a = (b); a < (c); ++a)
#define ROFI(a, b, c) for (int a = (b); a > (c); --a)
#define FORLL(a, b, c) for (ll a = (b); a < (c); ++a)
#define ROFLL(a, b, c) for (ll a = (b); a > (c); --a) // }}}
void solve() {
int n;
cin >> n;
vec<ll> a(n);
vec<ll> prefix(n + 1, 0);
FORI(i, 0, n) {
cin >> a[i];
prefix[i + 1] = prefix[i] + a[i];
}
string s;
cin >> s;
int l = 0, r = n - 1;
ll ans = 0;
while (l < r) {
if (s[l] == 'L' && s[r] == 'R') {
ans += prefix[r + 1] - prefix[l];
++l;
--r;
} else if (s[r] == 'L') {
--r;
} else {
++l;
}
}
prln("{}", ans);
}
int main() { // {{{
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
} //}}}

16
codeforces/996/d.in Normal file
View file

@ -0,0 +1,16 @@
5
3
-1 -1 -1
LLR
6
3 5 1 4 3 2
LRLLLR
2
2 8
LR
2
3 9
RL
5
1 2 3 4 5
LRLRR

8
codeforces/996/d.out Normal file
View file

@ -0,0 +1,8 @@
0
18
10
0
22
[code]: 0
[time]: 10.4334 ms

145
codeforces/996/e.cc Normal file
View file

@ -0,0 +1,145 @@
#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;
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());
}
template <typename... Args>
void pr(std::format_string<Args...> fmt, Args&&... args) {
std::print(fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void pr(std::format_string<Args...> fmt) {
std::print(fmt);
}
template <typename... Args>
void prln(std::format_string<Args...> fmt, Args&&... args) {
std::println(fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void prln(std::format_string<Args...> fmt) {
std::println(fmt);
}
using ll = long long;
using ld = long double;
template <typename T>
using vec = std::vector<T>;
#define ff first
#define ss second
#define eb emplace_back
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define FORI(a, b, c) for (int a = (b); a < (c); ++a)
#define ROFI(a, b, c) for (int a = (b); a > (c); --a)
#define FORLL(a, b, c) for (ll a = (b); a < (c); ++a)
#define ROFLL(a, b, c) for (ll a = (b); a > (c); --a) // }}}
void fill(vec<vec<ll>>& matrix, vec<int>& a, int k) {
int n = matrix.size();
int m = matrix[0].size();
vec<vec<int>> extra(n, vec<int>(m));
FORI(i, 0, n) {
FORI(j, 0, m) {
int top = max(0, i - (k - 1));
int left = max(0, j - (k - 1));
int bottom = min(n - k, i);
int right = min(m - k, j);
extra[i][j] = (bottom - top + 1) * (right - left + 1);
}
}
vec<pair<int, pair<int, int>>> cells;
FORI(i, 0, n) {
FORI(j, 0, m) {
cells.push_back({extra[i][j], {i, j}});
}
}
sort(rall(cells));
FORI(i, 0, min(n * m, sz<int>(a))) {
auto [x, y] = cells[i].ss;
matrix[x][y] = a[i];
}
}
long long sums(vector<vector<ll>>& matrix, int k) {
int n = matrix.size(), m = matrix[0].size();
vec<vec<ll>> prefix(n + 1, vec<ll>(m + 1, 0));
FORI(i, 1, n + 1) {
FORI(j, 1, m + 1) {
prefix[i][j] = matrix[i - 1][j - 1] + prefix[i - 1][j] +
prefix[i][j - 1] - prefix[i - 1][j - 1];
}
}
auto get = [&](int r1, int c1, int r2, int c2) -> ll {
return prefix[r2 + 1][c2 + 1] - prefix[r2 + 1][c1] - prefix[r1][c2 + 1] +
prefix[r1][c1];
};
ll ans = 0;
FORI(i, 0, n - k + 1) {
FORI(j, 0, m - k + 1) {
ans += get(i, j, i + k - 1, j + k - 1);
}
}
return ans;
}
void solve() {
int n, m, k;
cin >> n >> m >> k;
int w;
cin >> w;
vec<int> a(w);
for (auto& e : a)
cin >> e;
sort(rall(a));
vector<vector<ll>> matrix(n, vector<ll>(m, 0));
fill(matrix, a, k);
prln("{}", sums(matrix, k));
}
int main() { // {{{
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
} //}}}

16
codeforces/996/e.in Normal file
View file

@ -0,0 +1,16 @@
5
3 4 2
9
1 1 1 1 1 1 1 1 1
2 1 1
2
5 7
20 15 7
9
4 1 4 5 6 1 1000000000 898 777
1984 1 1
4
5 4 1499 2004
9 5 5
6
6 7 14 16 16 6

8
codeforces/996/e.out Normal file
View file

@ -0,0 +1,8 @@
21
12
49000083104
3512
319
[code]: 0
[time]: 13.2093 ms

106
codeforces/996/f.cc Normal file
View file

@ -0,0 +1,106 @@
#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;
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());
}
template <typename... Args>
void pr(std::format_string<Args...> fmt, Args&&... args) {
std::print(fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void pr(std::format_string<Args...> fmt) {
std::print(fmt);
}
template <typename... Args>
void prln(std::format_string<Args...> fmt, Args&&... args) {
std::println(fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void prln(std::format_string<Args...> fmt) {
std::println(fmt);
}
using ll = long long;
using ld = long double;
template <typename T>
using vec = std::vector<T>;
#define ff first
#define ss second
#define eb emplace_back
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define FORI(a, b, c) for (int a = (b); a < (c); ++a)
#define ROFI(a, b, c) for (int a = (b); a > (c); --a)
#define FORLL(a, b, c) for (ll a = (b); a < (c); ++a)
#define ROFLL(a, b, c) for (ll a = (b); a > (c); --a) // }}}
void solve() {
int n, k;
cin >> n >> k;
vec<pair<ll, ll>> recs;
FORI(i, 0, n) {
cin >> recs[i].ff >> recs[i].ss;
}
vec<vec<ll>> dp(n + 1, vec<ll>(k + 1, MAX<ll>));
FORI(i, 0, n + 1) {
dp[i][0] = 0;
}
FORLL(i, 1, n + 1) {
FORLL(j, 1, k + 1) {
int x = recs[i - 1].ff, y = recs[i - 1].ss;
int cost = 0;
FORLL(take, 0, min(j, recs[i - 1].ff + recs[i - 1].ss) + 1) {
if (dp[i - 1][j - take] != MAX<ll>)
dp[i][j] = min(dp[i][j], dp[i - 1][j - take] + cost);
if (x > y)
swap(x, y);
cost += x;
y--;
}
}
}
cout << (dp[n][k] == MAX<ll> ? -1 : dp[n][k]) << '\n';
}
int main() { // {{{
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
} //}}}

27
codeforces/996/f.in Normal file
View file

@ -0,0 +1,27 @@
7
1 4
6 3
1 5
4 4
5 10
1 1
1 1
1 1
1 1
1 1
2 100
1 2
5 6
3 11
2 2
3 3
4 4
3 25
9 2
4 3
8 10
4 18
5 4
8 5
8 3
6 2

3
codeforces/996/f.out Normal file
View file

@ -0,0 +1,3 @@
[code]: 15 (SIGTERM)
[time]: 98.3179 ms