This commit is contained in:
arielherself 2024-03-12 01:17:19 +08:00
parent a113ead758
commit 2c54d4f1b1
11 changed files with 813 additions and 171 deletions

View File

@ -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 // #define DUMP_TEST_CASE 512
void dump() {} void dump() {}
@ -241,16 +241,18 @@ void dump() {}
void prep() {} void prep() {}
void solve() { void solve() {
read(string, s); read(int, n, m, k);
int open = 1; readvec(int, a, n);
for (auto&& x : s) { readvec(int, b, m);
if (x == '|') { sort(a.begin(), a.end());
open ^= 1; sort(b.begin(), b.end());
} else if (open) { int res = 0;
cout << x; 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 << res << endl;
cout << endl;
} }
int main() { int main() {

Binary file not shown.

View File

@ -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 // #define DUMP_TEST_CASE 512
void dump() {} void dump() {}
@ -241,14 +241,21 @@ void dump() {}
void prep() {} void prep() {}
void solve() { void solve() {
vector<int> res; read(int, n);
loop { readvec(ll, a, n);
read(int, x); for (int i = 1; i < n - 1; ++i) {
res.push_back(x); if (a[i - 1] < 0) {
if (x == 0) break; 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() { int main() {

View File

@ -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 // #define DUMP_TEST_CASE 512
void dump() {} void dump() {}
@ -242,28 +242,13 @@ void prep() {}
void solve() { void solve() {
read(int, n); read(int, n);
readvec(ll, a, n); read(string, a);
read(int, m); int res = 0;
readvec(ll, b, m); for (int i = 2; i < n; ++i) {
read(int, l); if (a[i - 2] == 'p' && a[i - 1] == 'i' && a[i] == 'e') res += 1;
readvec(ll, c, l); if (a[i - 2] == 'm' && a[i - 1] == 'a' && a[i] == 'p') res += 1, a[i] = 'x';
unordered_set<int, safe_hash> 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";
}
} }
cout << res << endl;
} }
int main() { int main() {

377
src/bin/cf-1244c.cc Normal file
View File

@ -0,0 +1,377 @@
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* Useful Macros
* by subcrip
* (requires C++17)
*/
#include<bits/stdc++.h>
using namespace std;
/* macro helpers */
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::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<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
/* 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::milliseconds>(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<upair>
#define ugt std::greater<upair>
#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 <typename T, typename U>
size_t operator()(const pair<T, U>& 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<vector<int>> 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 <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> 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<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> 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<type> 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<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& 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<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> 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<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> 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<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> 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 <typename T> T abs(T x) { return x < 0 ? -x : x; }
template <typename T>
struct exgcd_solution_t {
T x, y, gcd;
};
template <typename T>
struct diophantine_solution_t {
exgcd_solution_t<T> x_min, y_min;
T range;
};
// solve `ax + by = gcd(a, b)`
template <typename T>
optional<exgcd_solution_t<T>> exgcd(T a, T b) {
if (a < 0 || b < 0 || a == 0 && b == 0) return nullopt;
T x, y, g;
function<void(T, T)> __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 <typename T>
optional<T> 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 <typename T>
optional<T> crt(const vector<pair<T, T>>& 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 <typename T>
optional<diophantine_solution_t<T>> 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 <typename T>
optional<T> 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
}

View File

@ -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 // #define DUMP_TEST_CASE 512
void dump() {} void dump() {}
@ -241,32 +241,27 @@ void dump() {}
void prep() {} void prep() {}
void solve() { void solve() {
read(string, t); read(int, n, m, x);
int n = t.length(); vector<vector<bool>> dp(m + 1, vector<bool>(n));
read(int, m); dp[0][x - 1] = 1;
vector<vector<int>> dp(m + 1, vector<int>(n + 1, INF)); for (int i = 1; i <= m; ++i) {
dp[0][0] = 0; read(int, r), read(char, op);
for (int y = 1; y <= m; ++y) { if (op != '1') {
read(int, k); for (int j = 0; j < n; ++j) {
readvec(string, s, k); if (dp[i - 1][j]) dp[i][mod(j + r, n)] = 1;
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) { if (op != '0') {
dp[y][i + l] = min(dp[y][i + l], dp[y - 1][i] + 1); for (int j = 0; j < n; ++j) {
if (dp[i - 1][j]) dp[i][mod(j - r, n)] = 1;
} }
} }
} }
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 << (dp[m][n] == INF ? -1 : dp[m][n]) << endl; cout << "\n";
} }
int main() { int main() {

View File

@ -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 // #define DUMP_TEST_CASE 512
void dump() {} void dump() {}
void prep() {} void prep() {}
struct ListNode {
int val;
ListNode* prev;
ListNode* next;
};
void solve() { void solve() {
read(int, n); read(int, n, m, k, d);
ListNode head {0, nullptr, nullptr}, tail {0, nullptr, nullptr}; vector<ll> cost(n);
head.next = &tail;
tail.prev = &head;
ListNode* curr = &head;
unordered_map<int, ListNode*, safe_hash> mp;
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
read(int, x); readvec(ll, a, m);
curr->next = new ListNode{x, curr, &tail}; priority_queue<pli, vector<pli>, greater<>> pq;
curr = curr->next; ll curr = 0;
mp[x] = curr; for (int j = 0; j < m; ++j) {
} while (pq.size() && pq.top().second < j - d - 1) pq.pop();
read(int, q); if (j > d + 1) {
while (q--) { curr = pq.top().first + a[j] + 1;
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 { } else {
read(int, x); curr = 1 + a[j] + 1;
ListNode* nw = mp[x];
ListNode* pre = nw->prev;
ListNode* nxt = nw->next;
pre->next = nxt;
nxt->prev = pre;
mp.erase(x);
delete nw;
} }
pq.emplace(curr, j);;
} }
curr = &head; cost[i] = curr;
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() { int main() {

View File

@ -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 // #define DUMP_TEST_CASE 512
void dump() {} void dump() {}
@ -241,66 +241,53 @@ void dump() {}
void prep() {} void prep() {}
void solve() { void solve() {
read(int, n); read(int, n, m, k);
vector<vector<ll>> cost(n, vector<ll>(n)); readvec(ll, a, n);
for (int i = 0; i < n; ++i) { int x = 1;
for (int j = 0; j < n; ++j) { for (int i = 1; i < n; ++i) {
cin >> cost[i][j]; if (a[i] - a[i - 1] > a[x] - a[x - 1]) x = i;
}
ll other = 0;
for (int i = 1; i < n; ++i) {
if (i != x) other = max(other, a[i] - a[i - 1]);
}
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;
} }
} }
vector<vector<ll>> r(n, vector<ll>(n - 1)); if (r >= 0 && c[r] + b[i] <= v && c[r] + b[i] >= u) {
vector<vector<ll>> d(n - 1, vector<ll>(n)); res = min(res, max(other, max(c[r] + b[i] - u, v - c[r] - b[i])));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n - 1; ++j) {
cin >> r[i][j];
} }
} }
for (int i = 0; i < n - 1; ++i) { {
for (int j = 0; j < n; ++j) { int l = 0, r = k - 1;
cin >> d[i][j]; while (l < r) {
int mid = l + r >> 1;
if (c[mid] + b[i] > target) {
r = mid;
} else {
l = mid + 1;
} }
} }
vector<vector<ll>> dp1(n, vector<ll>(n, INFLL)); if (l < k && c[l] + b[i] <= v && c[l] + b[i] >= u) {
dp1[n - 1][n - 1] = 0; res = min(res, max(other, max(c[l] + b[i] - u, v - c[l] - b[i])));
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<vector<pll>> dp2(n, vector<pll>(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});
} }
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});
}
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; cout << res << endl;
} }

277
src/bin/g.cc Normal file
View File

@ -0,0 +1,277 @@
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* Useful Macros
* by subcrip
* (requires C++17)
*/
#include<bits/stdc++.h>
using namespace std;
/* macro helpers */
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::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<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
/* 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::milliseconds>(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<upair>
#define ugt std::greater<upair>
#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 <typename T, typename U>
size_t operator()(const pair<T, U>& 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<vector<int>> 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 <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> 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<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> 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<type> 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<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& 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<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> 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<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> 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<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> 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<int, vector<pii>, safe_hash> cl;
vector<vector< pii >> 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<int, safe_hash> 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
}

View File

@ -1,5 +1,29 @@
3 3
2 3 4 7 9
1 2 2 4 1
2 3 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

View File

@ -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 // #define DUMP_TEST_CASE 512
void dump() {} void dump() {}
void prep() {} 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() { int main() {