diff --git a/src/bin/a.cc b/src/bin/a.cc index 28bc6a6..5b566eb 100644 --- a/src/bin/a.cc +++ b/src/bin/a.cc @@ -233,7 +233,7 @@ int period(string s) { // find the length of shortest recurring period } ///////////////////////////////////////////////////////// -#define SINGLE_TEST_CASE +// #define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 512 void dump() {} @@ -241,16 +241,18 @@ void dump() {} void prep() {} void solve() { - read(string, s); - int open = 1; - for (auto&& x : s) { - if (x == '|') { - open ^= 1; - } else if (open) { - cout << x; - } + read(int, n, m, k); + readvec(int, a, n); + readvec(int, b, m); + sort(a.begin(), a.end()); + sort(b.begin(), b.end()); + int res = 0; + for (int i = 0; i < n; ++i) { + int j = -1; + while (j + 1 < m && b[j + 1] + a[i] <= k) ++j; + res += j + 1; } - cout << endl; + cout << res << endl; } int main() { diff --git a/src/bin/a.out b/src/bin/a.out index bbc53da..de49ea5 100755 Binary files a/src/bin/a.out and b/src/bin/a.out differ diff --git a/src/bin/b.cc b/src/bin/b.cc index 0119ff1..8860206 100644 --- a/src/bin/b.cc +++ b/src/bin/b.cc @@ -233,7 +233,7 @@ int period(string s) { // find the length of shortest recurring period } ///////////////////////////////////////////////////////// -#define SINGLE_TEST_CASE +// #define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 512 void dump() {} @@ -241,14 +241,21 @@ void dump() {} void prep() {} void solve() { - vector res; - loop { - read(int, x); - res.push_back(x); - if (x == 0) break; + read(int, n); + readvec(ll, a, n); + for (int i = 1; i < n - 1; ++i) { + if (a[i - 1] < 0) { + cout << "NO\n"; + return; + } + a[i] -= 2 * a[i - 1]; + a[i + 1] -= a[i - 1]; + } + if (a[n - 1] != 0 || a[n - 2] != 0) { + cout << "NO\n"; + } else { + cout << "YES\n"; } - reverse(res.begin(), res.end()); - for (auto&& x : res) cout << x << endl; } int main() { diff --git a/src/bin/c.cc b/src/bin/c.cc index 86e4f80..4281a73 100644 --- a/src/bin/c.cc +++ b/src/bin/c.cc @@ -233,7 +233,7 @@ int period(string s) { // find the length of shortest recurring period } ///////////////////////////////////////////////////////// -#define SINGLE_TEST_CASE +// #define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 512 void dump() {} @@ -242,28 +242,13 @@ void prep() {} void solve() { read(int, n); - readvec(ll, a, n); - read(int, m); - readvec(ll, b, m); - read(int, l); - readvec(ll, c, l); - unordered_set able; - for (auto&& x : a) { - for (auto&& y : b) { - for (auto&& z : c) { - able.insert(x + y + z); - } - } - } - read(int, q); - while (q--) { - read(int, x); - if (able.count(x)) { - cout << "Yes\n"; - } else { - cout << "No\n"; - } + read(string, a); + int res = 0; + for (int i = 2; i < n; ++i) { + if (a[i - 2] == 'p' && a[i - 1] == 'i' && a[i] == 'e') res += 1; + if (a[i - 2] == 'm' && a[i - 1] == 'a' && a[i] == 'p') res += 1, a[i] = 'x'; } + cout << res << endl; } int main() { diff --git a/src/bin/cf-1244c.cc b/src/bin/cf-1244c.cc new file mode 100644 index 0000000..6826d79 --- /dev/null +++ b/src/bin/cf-1244c.cc @@ -0,0 +1,377 @@ +#pragma GCC optimize("Ofast") +///////////////////////////////////////////////////////// +/** + * Useful Macros + * by subcrip + * (requires C++17) + */ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) decltype(container)::value_type + +/* type aliases */ +using ll = int64_t; +using ull = uint64_t; +using pii = pair; +using pil = pair; +using pli = pair; +using pll = pair; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; + +/* random */ + +mt19937 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!(var == val)) var = val; else +#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;) +#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +/* build data structures */ +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) +#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(type, a, n) __AS_PROCEDURE(vector a(n); for (int i = 0; i < (n); ++i) cin >> a[i];) +#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;) +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} + +/* pops */ +#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();) +#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();) +#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();) + +/* math */ +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(string s, string t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(string s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} +///////////////////////////////////////////////////////// + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 512 + +void dump() {} + +void prep() {} + +namespace Exgcd { + template T abs(T x) { return x < 0 ? -x : x; } + + template + struct exgcd_solution_t { + T x, y, gcd; + }; + + template + struct diophantine_solution_t { + exgcd_solution_t x_min, y_min; + T range; + }; + + // solve `ax + by = gcd(a, b)` + template + optional> exgcd(T a, T b) { + if (a < 0 || b < 0 || a == 0 && b == 0) return nullopt; + T x, y, g; + function __exgcd = [&__exgcd, &x, &y, &g] (T a, T b) -> void { + if (b == 0) { + g = a, x = 1, y = 0; + } else { + __exgcd(b, a % b); + swap(x, y); + y -= a / b * x; + } + }; + __exgcd(a, b); + return {{ x, y, g }}; + }; + + template + optional inverse(T a, T b) { + auto raw = exgcd(a, b); + if (raw == nullopt || raw.value().gcd != 1) { + return nullopt; + } else { + return mod(raw.value().x, b); + } + } + + // solve { x = a_i (mod n_i) } if n_i's are coprime + template + optional crt(const vector>& equations) { + T prod = 1; + for (auto&& [a, n] : equations) { + prod *= n; + } + T res = 0; + for (auto&& [a, n] : equations) { + T m = prod / n; + auto m_rev = inverse(m, n); + if (m_rev == nullopt) return nullopt; + res = mod(res + a * mod(m * m_rev.value(), prod), prod); + } + return res; + } + + // find minimal non-negative integral solutions of `ax + by = c`. It's not guaranteed that the other variable is non-negative. + template + optional> diophantine(T a, T b, T c, bool force_positive = false) { + if (a < 0 || b < 0 || a == 0 && b == 0) return nullopt; + auto raw = exgcd(a, b).value(); + if (c % raw.gcd) { + return nullopt; + } else { + T x = raw.x * c / raw.gcd, y = raw.y * c / raw.gcd; + T kx = force_positive ? (x <= 0 ? (-x) * raw.gcd / b + 1 : 1 - (x + b / raw.gcd - 1) * raw.gcd / b) : (x <= 0 ? ((-x) + b / raw.gcd - 1) * raw.gcd / b : (- x * raw.gcd / b)); + T ky = force_positive ? (y <= 0 ? (- 1 - (-y) * raw.gcd / a) : (y + a / raw.gcd - 1) * raw.gcd / a - 1) : (y <= 0 ? (- ((-y) + a / raw.gcd - 1) * raw.gcd / a) : y * raw.gcd / a); + return {{ { x + b * kx / raw.gcd , y - a * kx / raw.gcd , raw.gcd }, { x + b * ky / raw.gcd , y - a * ky / raw.gcd, raw.gcd }, abs(kx - ky) + 1 }}; + } + } + + // find the minimal non-negative integral solution of `ax = b (mod n)` + template + optional congruential(T a, T b, T n) { + if (a == 0) { + if (b != 0) return nullopt; + return 0; + } + if (a < 0 && a != LLONG_MIN && b != LLONG_MIN) a = -a, b = -b; + auto sol = diophantine(a, n, b); + if (sol == nullopt) { + return nullopt; + } else { + return sol.value().x_min.x; + } + } +} + +void solve() { + read(ll, n, p, w, d); + auto sol = Exgcd::diophantine<__int128_t>(w, d, p); + if (sol == nullopt) { + cout << -1 << endl; + } else { + ll x = sol->x_min.x, y = sol->x_min.y; + if (x + y <= n) { + if (y < 0) cout << -1 << endl; + else cout << x << ' ' << y << ' ' << n - x - y << endl; + } else { + ll g = sol->x_min.gcd; + ll tm = (w - d) / g; + ll k = (x + y - n + tm - 1) / tm; + if (k * w / g > y) { + cout << -1 << endl; + } else { + ll new_x = x + k * d / g, new_y = y - k * w / g; + cout << new_x << ' ' << new_y << ' ' << n - new_x - new_y << endl; + } + } + } +} + +int main() { + untie, cout.tie(NULL); + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + solve(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/d.cc b/src/bin/d.cc index d2baa0f..b509a74 100644 --- a/src/bin/d.cc +++ b/src/bin/d.cc @@ -233,7 +233,7 @@ int period(string s) { // find the length of shortest recurring period } ///////////////////////////////////////////////////////// -#define SINGLE_TEST_CASE +// #define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 512 void dump() {} @@ -241,32 +241,27 @@ void dump() {} void prep() {} void solve() { - read(string, t); - int n = t.length(); - read(int, m); - vector> dp(m + 1, vector(n + 1, INF)); - dp[0][0] = 0; - for (int y = 1; y <= m; ++y) { - read(int, k); - readvec(string, s, k); - for (int i = 0; i <= n; ++i) dp[y][i] = dp[y - 1][i]; - for (auto&& x : s) { - int l = x.size(); - for (int i = 0; i + l <= n; ++i) { - int able = 1; - for (int j = 0; j < l; ++j) { - if (t[i + j] != x[j]) { - able = 0; - break; - } - } - if (able) { - dp[y][i + l] = min(dp[y][i + l], dp[y - 1][i] + 1); - } + read(int, n, m, x); + vector> dp(m + 1, vector(n)); + dp[0][x - 1] = 1; + for (int i = 1; i <= m; ++i) { + read(int, r), read(char, op); + if (op != '1') { + for (int j = 0; j < n; ++j) { + if (dp[i - 1][j]) dp[i][mod(j + r, n)] = 1; + } + } + if (op != '0') { + for (int j = 0; j < n; ++j) { + if (dp[i - 1][j]) dp[i][mod(j - r, n)] = 1; } } } - cout << (dp[m][n] == INF ? -1 : dp[m][n]) << endl; + cout << count(dp[m].begin(), dp[m].end(), 1) << "\n"; + for (int i = 0; i < n; ++i) { + if (dp[m][i]) cout << i + 1 << ' '; + } + cout << "\n"; } int main() { diff --git a/src/bin/e.cc b/src/bin/e.cc index caa2fc9..56533da 100644 --- a/src/bin/e.cc +++ b/src/bin/e.cc @@ -233,64 +233,38 @@ int period(string s) { // find the length of shortest recurring period } ///////////////////////////////////////////////////////// -#define SINGLE_TEST_CASE +// #define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 512 void dump() {} void prep() {} -struct ListNode { - int val; - ListNode* prev; - ListNode* next; -}; - void solve() { - read(int, n); - ListNode head {0, nullptr, nullptr}, tail {0, nullptr, nullptr}; - head.next = &tail; - tail.prev = &head; - ListNode* curr = &head; - unordered_map mp; + read(int, n, m, k, d); + vector cost(n); for (int i = 0; i < n; ++i) { - read(int, x); - curr->next = new ListNode{x, curr, &tail}; - curr = curr->next; - mp[x] = curr; - } - read(int, q); - while (q--) { - read(int, op); - if (op == 1) { - read(int, x, y); - ListNode* pre = mp[x]; - ListNode* nxt = pre->next; - ListNode* nw = new ListNode {y, pre, nxt}; - pre->next = nw; - nxt->prev = nw; - mp[y] = nw; - } else { - read(int, x); - ListNode* nw = mp[x]; - ListNode* pre = nw->prev; - ListNode* nxt = nw->next; - pre->next = nxt; - nxt->prev = pre; - mp.erase(x); - delete nw; + readvec(ll, a, m); + priority_queue, greater<>> pq; + ll curr = 0; + for (int j = 0; j < m; ++j) { + while (pq.size() && pq.top().second < j - d - 1) pq.pop(); + if (j > d + 1) { + curr = pq.top().first + a[j] + 1; + } else { + curr = 1 + a[j] + 1; + } + pq.emplace(curr, j);; } + cost[i] = curr; } - curr = &head; - loop { - curr = curr->next; - if (curr->prev != &head) delete curr->prev; - if (curr == &tail) { - break; - } - cout << curr->val << ' '; + ll sum = 0, res = INFLL; + for (int i = 0; i < n; ++i) { + sum += cost[i]; + if (i - k >= 0) sum -= cost[i - k]; + if (i + 1 >= k) res = min(res, sum); } - cout << endl; + cout << res << endl; } int main() { diff --git a/src/bin/f.cc b/src/bin/f.cc index 5d49194..568de5f 100644 --- a/src/bin/f.cc +++ b/src/bin/f.cc @@ -233,7 +233,7 @@ int period(string s) { // find the length of shortest recurring period } ///////////////////////////////////////////////////////// -#define SINGLE_TEST_CASE +// #define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 512 void dump() {} @@ -241,66 +241,53 @@ void dump() {} void prep() {} void solve() { - read(int, n); - vector> cost(n, vector(n)); - for (int i = 0; i < n; ++i) { - for (int j = 0; j < n; ++j) { - cin >> cost[i][j]; - } + read(int, n, m, k); + readvec(ll, a, n); + int x = 1; + for (int i = 1; i < n; ++i) { + if (a[i] - a[i - 1] > a[x] - a[x - 1]) x = i; } - vector> r(n, vector(n - 1)); - vector> d(n - 1, vector(n)); - for (int i = 0; i < n; ++i) { - for (int j = 0; j < n - 1; ++j) { - cin >> r[i][j]; - } + ll other = 0; + for (int i = 1; i < n; ++i) { + if (i != x) other = max(other, a[i] - a[i - 1]); } - for (int i = 0; i < n - 1; ++i) { - for (int j = 0; j < n; ++j) { - cin >> d[i][j]; - } - } - vector> dp1(n, vector(n, INFLL)); - dp1[n - 1][n - 1] = 0; - for (int i = n - 1; ~i; --i) { - for (int j = n - 1; ~j; --j) { - if (j + 1 != n) dp1[i][j] = min(dp1[i][j], dp1[i][j+1] + r[i][j]); - if (i + 1 != n) dp1[i][j] = min(dp1[i][j], dp1[i+1][j] + d[i][j]); - } - } - vector> dp2(n, vector(n, {INFLL, 0})); - dp2[0][0] = {0, 0}; - ll res = INFLL; - for (int i = 0; i < n; ++i) { - for (int j = 0; j < n; ++j) { - if (i) { - auto [co, sv] = dp2[i-1][j]; - ll new_co = (max(ll(0), d[i-1][j] - sv) + cost[i-1][j] - 1) / cost[i-1][j]; - ll new_sv = sv + new_co * cost[i-1][j] - d[i-1][j]; - dp2[i][j] = min(dp2[i][j], {co + new_co + 1, new_sv}); + ll u = a[x - 1], v = a[x]; + ll target = (v + u) / 2; + readvec(ll, b, m); + readvec(ll, c, k); + sort(b.begin(), b.end()); + sort(c.begin(), c.end()); + ll res = v - u; + for (int i = 0; i < m; ++i) { + { + int l = 0, r = k - 1; + while (l < r) { + int mid = l + r + 1 >> 1; + if (c[mid] + b[i] <= target) { + l = mid; + } else { + r = mid - 1; + } } - if (j) { - auto [co, sv] = dp2[i][j-1]; - ll new_co = (max(ll(0), r[i][j-1] - sv) + cost[i][j-1] - 1) / cost[i][j-1]; - ll new_sv = sv + new_co * cost[i][j-1] - r[i][j-1]; - dp2[i][j] = min(dp2[i][j], {co + new_co + 1, new_sv}); + if (r >= 0 && c[r] + b[i] <= v && c[r] + b[i] >= u) { + res = min(res, max(other, max(c[r] + b[i] - u, v - c[r] - b[i]))); + } + } + { + int l = 0, r = k - 1; + while (l < r) { + int mid = l + r >> 1; + if (c[mid] + b[i] > target) { + r = mid; + } else { + l = mid + 1; + } + } + if (l < k && c[l] + b[i] <= v && c[l] + b[i] >= u) { + res = min(res, max(other, max(c[l] + b[i] - u, v - c[l] - b[i]))); } - ll wait = (max(ll(0), dp1[i][j] - dp2[i][j].second) + cost[i][j] - 1) / cost[i][j]; - res = min(res, dp2[i][j].first + wait + 2 * n - 2 - i - j); } } - // for (int i = 0; i < n; ++i) { - // for (int j = 0; j < n; ++j) { - // cout << dp1[i][j] << ' '; - // } - // cout << endl; - // } - // for (int i = 0; i < n; ++i) { - // for (int j = 0; j < n; ++j) { - // cout << dp2[i][j] << ' '; - // } - // cout << endl; - // } cout << res << endl; } diff --git a/src/bin/g.cc b/src/bin/g.cc new file mode 100644 index 0000000..44d418a --- /dev/null +++ b/src/bin/g.cc @@ -0,0 +1,277 @@ +#pragma GCC optimize("Ofast") +///////////////////////////////////////////////////////// +/** + * Useful Macros + * by subcrip + * (requires C++17) + */ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) decltype(container)::value_type + +/* type aliases */ +using ll = int64_t; +using ull = uint64_t; +using pii = pair; +using pil = pair; +using pli = pair; +using pll = pair; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; + +/* random */ + +mt19937 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!(var == val)) var = val; else +#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;) +#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +/* build data structures */ +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) +#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(type, a, n) __AS_PROCEDURE(vector a(n); for (int i = 0; i < (n); ++i) cin >> a[i];) +#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;) +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} + +/* pops */ +#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();) +#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();) +#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();) + +/* math */ +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(string s, string t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(string s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} +///////////////////////////////////////////////////////// + +// #define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 512 + +void dump() {} + +void prep() {} + +void solve() { + read(int, n, m); + unordered_map, safe_hash> cl; + vector> ch(n + 1); + for (int i = 0; i < m; ++i) { + read(int, u, v, c); + ch[u].emplace_back(v, c); + ch[v].emplace_back(u, c); + } + read(int, b, e); + unordered_set b_open, e_open; + + assert(false); +} + +int main() { + untie, cout.tie(NULL); + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + solve(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/std.in b/src/bin/std.in index 1f831fd..1181b87 100644 --- a/src/bin/std.in +++ b/src/bin/std.in @@ -1,5 +1,29 @@ 3 -2 3 4 -1 2 -2 3 +7 9 +2 4 1 +3 6 1 +2 3 5 +1 7 1 +4 7 1 +2 5 4 +5 4 4 +3 4 1 +3 7 1 +5 3 +6 5 +6 5 83691 +4 1 83691 +5 4 83691 +3 2 83691 +4 3 83691 +5 1 +6 7 +6 1 83691 +6 2 83691 +2 5 83691 +5 6 83691 +2 3 83691 +5 4 83574 +3 5 83691 +1 4 diff --git a/src/bin/test.cc b/src/bin/test.cc index cc44add..a4a39f0 100644 --- a/src/bin/test.cc +++ b/src/bin/test.cc @@ -233,15 +233,29 @@ int period(string s) { // find the length of shortest recurring period } ///////////////////////////////////////////////////////// -// #define SINGLE_TEST_CASE +#define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 512 void dump() {} void prep() {} -void solve() { +int Exgcd(int a, int b, int &x, int &y) { + if (!b) { + x = 1; + y = 0; + return a; + } + int d = Exgcd(b, a % b, x, y); + int t = x; + x = y; + y = t - (a / b) * y; + return d; +} +void solve() { + int x, y; + cout << Exgcd(-6, 4, x, y) << ' ' << x << ' ' << y << endl; } int main() {