Submission #965539

#TimeUsernameProblemLanguageResultExecution timeMemory
965539Alkaser_IDCopy and Paste 3 (JOI22_copypaste3)C++17
20 / 100
3051 ms1272 KiB
#include <iostream> #include <algorithm> #include <vector> #include <map> #include <set> using namespace std; typedef long long ll; const ll mod = 1e9 + 7; #define ml(x,y) ((x*y)%mod) ll b1 = 53, p1[3003]; ll inv[3003]; ll a, b, c; ll hsh[3003]; map<ll, ll> dp; inline ll fsp(ll x, ll b) { if (b == 0) return 1; if (b == 1) return x; ll g = ml(x, x); g = fsp(g, b / 2); if (b % 2 == 0) return g; return (g * x) % mod; } inline ll small(ll l,ll r) { ll sz = r - l + 1; if (sz == 1) return a; if (sz == 2) { ll fr = (hsh[l] - (l == 0 ? 0 : hsh[l - 1])) % mod; ll sc = (hsh[l + 1] - hsh[l] + mod) % mod; sc = (sc * inv[1]) % mod; if (fr == sc) return min(2 * a, a + b + 2 * c); return 2 * a; } ll fr = (hsh[l] - (l == 0 ? 0 : hsh[l - 1])) % mod; ll sc = (hsh[l + 1] - hsh[l] + mod) % mod; ll th = (hsh[l + 2] - hsh[l + 1] + mod) % mod; sc = (sc * inv[1]) % mod; th = (th * inv[2]) % mod; if (sc != fr && fr != th && sc != th) return 3 * a; if (sc == fr && fr == th) return min(3 * a, a + b + 3 * c); return min(3 * a, 2 * a + b + 2 * c); } inline ll getmin(ll l, ll r) { ll th = hsh[r] - (l == 0 ? 0 : hsh[l - 1]); th = (th * inv[l]) % mod; if (dp.find(th) != dp.end()) return dp[th]; if (r - l + 1 <= 3) return small(l, r); ll dc = 0, tot = (r - l + 1) * a; for (ll ln = 1; ln <= (r - l + 1) / 2; ++ln) { map<ll, ll> m, d; map<ll, pair<ll, ll>> st; for (ll i = l; i + ln <= r + 1; ++i) { ll j = i + ln - 1; ll hs = (hsh[j] - (i > 0 ? hsh[i - 1] : 0) + mod) % mod; hs = (hs * inv[i]) % mod; if (m.find(hs) != m.end()) { if (i < d[hs]) continue; } else st[hs] = { i,j }; d[hs] = i + ln; ++m[hs]; } for (map<ll, ll>::iterator it = m.begin(); it != m.end(); ++it) { vector<ll> nh; pair<ll, ll> se = st[it->first]; ll bg = se.first; for (ll j = se.first; j <= se.second; ++j) { ll hvl = (hsh[j] - (bg == 0 ? 0 : hsh[bg - 1]) + mod) % mod; hvl = (hvl * inv[bg]) % mod; nh.push_back(hvl); } ll prp = getmin(se.first, se.second); ll tcost = it->second * ln * a - prp - it->second * c - b; if (tcost <= 0) continue; dc = max(dc, tcost); } } dp[th] = tot - dc; return tot - dc; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n; cin >> n; string s; cin >> s; cin >> a >> b >> c; p1[0] = 1; p1[1] = b1; for (ll i = 2; i <= s.size() + 3; ++i) { p1[i] = (p1[i - 1] * b1) % mod; } inv[0] = 1; for (ll i = 1; i <= s.size() + 3; ++i) { inv[i] = fsp(p1[i], mod - 2); } ll lst = 0; for (ll i = 0; i < s.size(); ++i) { hsh[i] = ((s[i] - 95) * p1[i]) % mod; hsh[i] = (hsh[i] + lst) % mod; lst = hsh[i]; } cout << getmin(0, n - 1); }

Compilation message (stderr)

copypaste3.cpp: In function 'int main()':
copypaste3.cpp:87:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |  for (ll i = 2; i <= s.size() + 3; ++i) {
      |                 ~~^~~~~~~~~~~~~~~
copypaste3.cpp:91:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |  for (ll i = 1; i <= s.size() + 3; ++i) {
      |                 ~~^~~~~~~~~~~~~~~
copypaste3.cpp:95:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |  for (ll i = 0; i < s.size(); ++i) {
      |                 ~~^~~~~~~~~~
#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...