제출 #673628

#제출 시각아이디문제언어결과실행 시간메모리
673628peijarCopy and Paste 3 (JOI22_copypaste3)C++17
30 / 100
3072 ms35244 KiB
#include <bits/stdc++.h> #define int long long using namespace std; template <const int32_t MOD> struct ModInt { int32_t x; ModInt() : x(0) {} ModInt(long long u) : x(u % MOD) { if (x < 0) x += MOD; } friend bool operator==(const ModInt &a, const ModInt &b) { return a.x == b.x; } friend bool operator!=(const ModInt &a, const ModInt &b) { return a.x != b.x; } friend bool operator<(const ModInt &a, const ModInt &b) { return a.x < b.x; } friend bool operator>(const ModInt &a, const ModInt &b) { return a.x > b.x; } friend bool operator<=(const ModInt &a, const ModInt &b) { return a.x <= b.x; } friend bool operator>=(const ModInt &a, const ModInt &b) { return a.x >= b.x; } static ModInt sign(long long k) { return ((k & 1) ? ModInt(MOD - 1) : ModInt(1)); } ModInt &operator+=(const ModInt &m) { x += m.x; if (x >= MOD) x -= MOD; return *this; } ModInt &operator-=(const ModInt &m) { x -= m.x; if (x < 0LL) x += MOD; return *this; } ModInt &operator*=(const ModInt &m) { x = (1LL * x * m.x) % MOD; return *this; } friend ModInt operator-(const ModInt &a) { ModInt res(a); if (res.x) res.x = MOD - res.x; return res; } friend ModInt operator+(const ModInt &a, const ModInt &b) { return ModInt(a) += ModInt(b); } friend ModInt operator-(const ModInt &a, const ModInt &b) { return ModInt(a) -= ModInt(b); } friend ModInt operator*(const ModInt &a, const ModInt &b) { return ModInt(a) *= ModInt(b); } static long long fp(long long u, long long k) { long long res = 1LL; while (k > 0LL) { if (k & 1LL) res = (res * u) % MOD; u = (u * u) % MOD; k /= 2LL; } return res; } static constexpr int mod() { return MOD; } ModInt fastpow(long long k) { return ModInt(fp(x, k)); } ModInt inv() { assert(x); return ModInt(fp(x, MOD - 2)); } ModInt &operator/=(const ModInt &m) { return *this *= ModInt(m).inv(); } friend ModInt operator/(const ModInt &a, const ModInt &b) { return ModInt(a) *= ModInt(b).inv(); } friend ostream &operator<<(ostream &out, const ModInt &a) { return out << a.x; } friend istream &operator>>(istream &in, ModInt &a) { return in >> a.x; } }; const int MOD = 1e9 + 9; using Mint = ModInt<MOD>; const int BASE = 330; const int MAXN = 2501; const int INF = 1e18; Mint powBase[MAXN], invPow[MAXN]; int N, costAdd, costCut, costPaste; string s; vector<Mint> prefPow; map<Mint, vector<int>> occs; map<Mint, int> cache; int solve(int l, int r) { if (l == r) return 0; if (l + 1 == r) return costAdd; Mint curHsh = (prefPow[r] - prefPow[l]) * invPow[l]; if (cache.count(curHsh)) return cache[curHsh]; int &ret = cache[curHsh]; ret = INF; ret = min(ret, solve(l + 1, r) + costAdd); ret = min(ret, (r - l) * costAdd); for (int len = 1; len < r - l; ++len) { Mint x = (prefPow[l + len] - prefPow[l]) * invPow[l]; vector<int> &pos = occs[x]; int nbOcc = 0; int cur = l; while (cur + len <= r) { nbOcc++; int id = lower_bound(pos.begin(), pos.end(), cur + len) - pos.begin(); if (id == (int)pos.size()) break; cur = pos[id]; } ret = min(ret, solve(l, l + len) + costCut + costPaste * nbOcc + (r - l - len * nbOcc) * costAdd); } return ret; } signed main(void) { ios_base::sync_with_stdio(false); cin.tie(0); powBase[0] = 1; for (int i = 1; i < MAXN; ++i) powBase[i] = powBase[i - 1] * BASE; invPow[MAXN - 1] = powBase[MAXN - 1].inv(); for (int i = MAXN - 1; i > 0; --i) invPow[i - 1] = invPow[i] * BASE; cin >> N >> s >> costAdd >> costCut >> costPaste; prefPow.resize(N + 1); for (int i = 0; i < N; ++i) prefPow[i + 1] = prefPow[i] + powBase[i] * s[i]; for (int deb = 0; deb < N; ++deb) for (int fin = deb + 1; fin <= N; ++fin) occs[(prefPow[fin] - prefPow[deb]) * invPow[deb]].push_back(deb); cout << solve(0, N) << endl; return 0; vector<vector<int>> dp(N + 1, vector<int>(N + 1, INF)); for (int deb = N; deb >= 0; --deb) { vector<vector<pair<int, int>>> updates(N + 1); for (int len = 1; len < N - deb; ++len) { int nbOcc = 0; Mint x = (prefPow[deb + len] - prefPow[deb]) * invPow[deb]; int curPos = deb; auto &vec = occs[x]; while (true) { nbOcc++; updates[curPos + len].emplace_back(len, nbOcc); int id = lower_bound(vec.begin(), vec.end(), curPos + len) - vec.begin(); if (id == (int)vec.size()) break; curPos = vec[id]; } } for (int fin = deb; fin <= N; ++fin) { int &cur = dp[deb][fin]; if (deb == fin) { cur = 0; continue; } cur = min(cur, dp[deb + 1][fin] + costAdd); cur = min(cur, dp[deb][fin - 1] + costAdd); for (auto [len, nbOcc] : updates[fin]) cur = min(cur, nbOcc * costPaste + costCut + dp[deb][deb + len] + (fin - deb - len * nbOcc) * costAdd); } } cout << dp[0][N] << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...