Submission #735195

# Submission time Handle Problem Language Result Execution time Memory
735195 2023-05-03T16:55:43 Z Sam_a17 Copy and Paste 3 (JOI22_copypaste3) C++17
100 / 100
1674 ms 367628 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;
 
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
 
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt(x) __builtin_popcount(x)
 
#define pb push_back
#define popf pop_front
#define popb pop_back
 
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
 
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
 
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
 
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();
 
  if (str != "") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  }
}
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2505;
const long long inf = 1e18;
long long a, b, c, dp[N][N], nxt[N];
int n;
string s;
 
const long long mod = 1e9 + 7;
long long h1[N], h2[N];
long long pw1[N], pw2[N];
 
long long compute1(int l, int r) {
  return (h1[l] - (pw1[r - l + 1] * h1[r + 1]) % mod + mod) % mod;
}
 
long long compute2(int l, int r) {
  return (h2[l] - (pw2[r - l + 1] * h2[r + 1]) % mod + mod) % mod;
}
 
pair<long long, long long> add(int l, int r) {
  l--, r--;
  pair<long long, long long> par;
  par.first = compute1(l, r);
  par.second = compute2(l, r);
  return par;
}
 
struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
 
        if (hash1 != hash2) {
            return hash1 ^ hash2;             
        }
         
        // If hash1 == hash2, their XOR is zero.
        return hash1;
    }
};
 
unordered_map<pair<long long, long long>, vector<int>, hash_pair> mp;
 
void solve_() {
  cin >> n >> s;
  cin >> a >> b >> c;
 
  for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= n; j++) {
      dp[i][j] = inf;
    }
  }
 
  mp.reserve((N * N) / 2);
 
  //
  for(int i = n - 1; i >= 0; i--) {
    h1[i] = ((h1[i + 1] * 31ll) % mod + s[i] - 'a' + 1) % mod; 
    h2[i] = ((h2[i + 1] * 13ll) % mod + s[i] - 'a' + 1) % mod; 
  }
 
  pw1[0] = pw2[0] = 1;
  for(int i = 1; i <= n; i++) {
    pw1[i] = (pw1[i - 1] * 31ll) % mod;
    pw2[i] = (pw2[i - 1] * 13ll) % mod; 
  }
 
  for(int len = 1; len <= n; len++) {
    for(int l = 1; l + len - 1 <= n; l++) {
      int r = l + len - 1;
      nxt[l] = n + 1;
      mp[add(l, r)].push_back(l);
 
      if(len == 1) {
        dp[l][r] = a;
      } else {
        dp[l][r] = min(dp[l][r], dp[l][r - 1] + a);
        dp[l][r] = min(dp[l][r], dp[l + 1][r] + a);
      }
    }
 
    for(int l = n - len + 1; l >= 1; l--) {
      int r = l + len - 1;
      auto pr = add(l, r);
      auto it = lower_bound(all(mp[pr]), r + 1);
      if(it != mp[pr].end()) {
        nxt[l] = *it;
      }
 
 
      int li = l;
      long long pp = dp[l][r], cnt = 1;
 
      while(r <= n) {
        dp[l][r] = min(dp[l][r], pp + cnt * c + b + (r - l + 1 - cnt * len) * a);
        li = nxt[li];
        r = li + len - 1;
        cnt++;  
      }
    }
 
    for(int l = 1; l + len - 1 <= n; l++) {
      int r = l + len - 1;
      mp[add(l, r)].clear();
    }
  }
 
  assert(dp[1][n] >= 0);
  cout << dp[1][n] << '\n';
}
 
int main() {
  setIO();
 
  auto solve = [&](int test_case)-> void {
    while(test_case--) {
      solve_();
    }
  };
 
  int test_cases = 1;
  // cin >> test_cases;
  solve(test_cases);
 
  return 0;
} 

Compilation message

copypaste3.cpp: In function 'void setIO(std::string)':
copypaste3.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
copypaste3.cpp:65:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 25172 KB Output is correct
2 Correct 16 ms 25140 KB Output is correct
3 Correct 14 ms 25136 KB Output is correct
4 Correct 13 ms 25172 KB Output is correct
5 Correct 13 ms 25236 KB Output is correct
6 Correct 14 ms 25140 KB Output is correct
7 Correct 14 ms 25156 KB Output is correct
8 Correct 14 ms 25172 KB Output is correct
9 Correct 13 ms 25172 KB Output is correct
10 Correct 14 ms 25172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 25300 KB Output is correct
2 Correct 13 ms 25172 KB Output is correct
3 Correct 307 ms 68100 KB Output is correct
4 Correct 364 ms 74220 KB Output is correct
5 Correct 424 ms 79500 KB Output is correct
6 Correct 483 ms 85040 KB Output is correct
7 Correct 580 ms 91288 KB Output is correct
8 Correct 593 ms 91188 KB Output is correct
9 Correct 592 ms 91160 KB Output is correct
10 Correct 12 ms 25172 KB Output is correct
11 Correct 13 ms 25140 KB Output is correct
12 Correct 13 ms 25172 KB Output is correct
13 Correct 14 ms 25172 KB Output is correct
14 Correct 12 ms 25272 KB Output is correct
15 Correct 13 ms 25188 KB Output is correct
16 Correct 15 ms 25300 KB Output is correct
17 Correct 13 ms 25172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 25172 KB Output is correct
2 Correct 16 ms 25140 KB Output is correct
3 Correct 14 ms 25136 KB Output is correct
4 Correct 13 ms 25172 KB Output is correct
5 Correct 13 ms 25236 KB Output is correct
6 Correct 14 ms 25140 KB Output is correct
7 Correct 14 ms 25156 KB Output is correct
8 Correct 14 ms 25172 KB Output is correct
9 Correct 13 ms 25172 KB Output is correct
10 Correct 14 ms 25172 KB Output is correct
11 Correct 12 ms 25172 KB Output is correct
12 Correct 12 ms 25196 KB Output is correct
13 Correct 13 ms 25248 KB Output is correct
14 Correct 14 ms 25276 KB Output is correct
15 Correct 13 ms 25300 KB Output is correct
16 Correct 13 ms 25300 KB Output is correct
17 Correct 14 ms 25172 KB Output is correct
18 Correct 14 ms 25172 KB Output is correct
19 Correct 14 ms 25300 KB Output is correct
20 Correct 13 ms 25172 KB Output is correct
21 Correct 12 ms 25328 KB Output is correct
22 Correct 13 ms 25332 KB Output is correct
23 Correct 13 ms 25324 KB Output is correct
24 Correct 13 ms 25320 KB Output is correct
25 Correct 13 ms 25300 KB Output is correct
26 Correct 13 ms 25416 KB Output is correct
27 Correct 13 ms 25364 KB Output is correct
28 Correct 15 ms 25300 KB Output is correct
29 Correct 13 ms 25300 KB Output is correct
30 Correct 13 ms 25300 KB Output is correct
31 Correct 13 ms 25392 KB Output is correct
32 Correct 12 ms 25300 KB Output is correct
33 Correct 13 ms 25300 KB Output is correct
34 Correct 13 ms 25156 KB Output is correct
35 Correct 13 ms 25172 KB Output is correct
36 Correct 13 ms 25172 KB Output is correct
37 Correct 13 ms 25212 KB Output is correct
38 Correct 15 ms 25288 KB Output is correct
39 Correct 14 ms 25316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 25172 KB Output is correct
2 Correct 16 ms 25140 KB Output is correct
3 Correct 14 ms 25136 KB Output is correct
4 Correct 13 ms 25172 KB Output is correct
5 Correct 13 ms 25236 KB Output is correct
6 Correct 14 ms 25140 KB Output is correct
7 Correct 14 ms 25156 KB Output is correct
8 Correct 14 ms 25172 KB Output is correct
9 Correct 13 ms 25172 KB Output is correct
10 Correct 14 ms 25172 KB Output is correct
11 Correct 12 ms 25172 KB Output is correct
12 Correct 12 ms 25196 KB Output is correct
13 Correct 13 ms 25248 KB Output is correct
14 Correct 14 ms 25276 KB Output is correct
15 Correct 13 ms 25300 KB Output is correct
16 Correct 13 ms 25300 KB Output is correct
17 Correct 14 ms 25172 KB Output is correct
18 Correct 14 ms 25172 KB Output is correct
19 Correct 14 ms 25300 KB Output is correct
20 Correct 13 ms 25172 KB Output is correct
21 Correct 12 ms 25328 KB Output is correct
22 Correct 13 ms 25332 KB Output is correct
23 Correct 13 ms 25324 KB Output is correct
24 Correct 13 ms 25320 KB Output is correct
25 Correct 13 ms 25300 KB Output is correct
26 Correct 13 ms 25416 KB Output is correct
27 Correct 13 ms 25364 KB Output is correct
28 Correct 15 ms 25300 KB Output is correct
29 Correct 13 ms 25300 KB Output is correct
30 Correct 13 ms 25300 KB Output is correct
31 Correct 13 ms 25392 KB Output is correct
32 Correct 12 ms 25300 KB Output is correct
33 Correct 13 ms 25300 KB Output is correct
34 Correct 13 ms 25156 KB Output is correct
35 Correct 13 ms 25172 KB Output is correct
36 Correct 13 ms 25172 KB Output is correct
37 Correct 13 ms 25212 KB Output is correct
38 Correct 15 ms 25288 KB Output is correct
39 Correct 14 ms 25316 KB Output is correct
40 Correct 14 ms 25860 KB Output is correct
41 Correct 15 ms 26452 KB Output is correct
42 Correct 19 ms 28060 KB Output is correct
43 Correct 18 ms 28160 KB Output is correct
44 Correct 20 ms 28116 KB Output is correct
45 Correct 20 ms 28116 KB Output is correct
46 Correct 20 ms 28208 KB Output is correct
47 Correct 17 ms 26836 KB Output is correct
48 Correct 19 ms 27348 KB Output is correct
49 Correct 19 ms 27628 KB Output is correct
50 Correct 19 ms 27732 KB Output is correct
51 Correct 18 ms 27324 KB Output is correct
52 Correct 18 ms 27348 KB Output is correct
53 Correct 20 ms 28116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 25172 KB Output is correct
2 Correct 16 ms 25140 KB Output is correct
3 Correct 14 ms 25136 KB Output is correct
4 Correct 13 ms 25172 KB Output is correct
5 Correct 13 ms 25236 KB Output is correct
6 Correct 14 ms 25140 KB Output is correct
7 Correct 14 ms 25156 KB Output is correct
8 Correct 14 ms 25172 KB Output is correct
9 Correct 13 ms 25172 KB Output is correct
10 Correct 14 ms 25172 KB Output is correct
11 Correct 12 ms 25172 KB Output is correct
12 Correct 12 ms 25196 KB Output is correct
13 Correct 13 ms 25248 KB Output is correct
14 Correct 14 ms 25276 KB Output is correct
15 Correct 13 ms 25300 KB Output is correct
16 Correct 13 ms 25300 KB Output is correct
17 Correct 14 ms 25172 KB Output is correct
18 Correct 14 ms 25172 KB Output is correct
19 Correct 14 ms 25300 KB Output is correct
20 Correct 13 ms 25172 KB Output is correct
21 Correct 12 ms 25328 KB Output is correct
22 Correct 13 ms 25332 KB Output is correct
23 Correct 13 ms 25324 KB Output is correct
24 Correct 13 ms 25320 KB Output is correct
25 Correct 13 ms 25300 KB Output is correct
26 Correct 13 ms 25416 KB Output is correct
27 Correct 13 ms 25364 KB Output is correct
28 Correct 15 ms 25300 KB Output is correct
29 Correct 13 ms 25300 KB Output is correct
30 Correct 13 ms 25300 KB Output is correct
31 Correct 13 ms 25392 KB Output is correct
32 Correct 12 ms 25300 KB Output is correct
33 Correct 13 ms 25300 KB Output is correct
34 Correct 13 ms 25156 KB Output is correct
35 Correct 13 ms 25172 KB Output is correct
36 Correct 13 ms 25172 KB Output is correct
37 Correct 13 ms 25212 KB Output is correct
38 Correct 15 ms 25288 KB Output is correct
39 Correct 14 ms 25316 KB Output is correct
40 Correct 14 ms 25860 KB Output is correct
41 Correct 15 ms 26452 KB Output is correct
42 Correct 19 ms 28060 KB Output is correct
43 Correct 18 ms 28160 KB Output is correct
44 Correct 20 ms 28116 KB Output is correct
45 Correct 20 ms 28116 KB Output is correct
46 Correct 20 ms 28208 KB Output is correct
47 Correct 17 ms 26836 KB Output is correct
48 Correct 19 ms 27348 KB Output is correct
49 Correct 19 ms 27628 KB Output is correct
50 Correct 19 ms 27732 KB Output is correct
51 Correct 18 ms 27324 KB Output is correct
52 Correct 18 ms 27348 KB Output is correct
53 Correct 20 ms 28116 KB Output is correct
54 Correct 45 ms 37580 KB Output is correct
55 Correct 88 ms 39708 KB Output is correct
56 Correct 188 ms 83260 KB Output is correct
57 Correct 184 ms 83536 KB Output is correct
58 Correct 184 ms 83924 KB Output is correct
59 Correct 183 ms 83828 KB Output is correct
60 Correct 189 ms 83980 KB Output is correct
61 Correct 118 ms 57060 KB Output is correct
62 Correct 84 ms 40216 KB Output is correct
63 Correct 163 ms 72264 KB Output is correct
64 Correct 163 ms 72708 KB Output is correct
65 Correct 136 ms 60392 KB Output is correct
66 Correct 132 ms 60360 KB Output is correct
67 Correct 186 ms 84048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 25172 KB Output is correct
2 Correct 16 ms 25140 KB Output is correct
3 Correct 14 ms 25136 KB Output is correct
4 Correct 13 ms 25172 KB Output is correct
5 Correct 13 ms 25236 KB Output is correct
6 Correct 14 ms 25140 KB Output is correct
7 Correct 14 ms 25156 KB Output is correct
8 Correct 14 ms 25172 KB Output is correct
9 Correct 13 ms 25172 KB Output is correct
10 Correct 14 ms 25172 KB Output is correct
11 Correct 14 ms 25300 KB Output is correct
12 Correct 13 ms 25172 KB Output is correct
13 Correct 307 ms 68100 KB Output is correct
14 Correct 364 ms 74220 KB Output is correct
15 Correct 424 ms 79500 KB Output is correct
16 Correct 483 ms 85040 KB Output is correct
17 Correct 580 ms 91288 KB Output is correct
18 Correct 593 ms 91188 KB Output is correct
19 Correct 592 ms 91160 KB Output is correct
20 Correct 12 ms 25172 KB Output is correct
21 Correct 13 ms 25140 KB Output is correct
22 Correct 13 ms 25172 KB Output is correct
23 Correct 14 ms 25172 KB Output is correct
24 Correct 12 ms 25272 KB Output is correct
25 Correct 13 ms 25188 KB Output is correct
26 Correct 15 ms 25300 KB Output is correct
27 Correct 13 ms 25172 KB Output is correct
28 Correct 12 ms 25172 KB Output is correct
29 Correct 12 ms 25196 KB Output is correct
30 Correct 13 ms 25248 KB Output is correct
31 Correct 14 ms 25276 KB Output is correct
32 Correct 13 ms 25300 KB Output is correct
33 Correct 13 ms 25300 KB Output is correct
34 Correct 14 ms 25172 KB Output is correct
35 Correct 14 ms 25172 KB Output is correct
36 Correct 14 ms 25300 KB Output is correct
37 Correct 13 ms 25172 KB Output is correct
38 Correct 12 ms 25328 KB Output is correct
39 Correct 13 ms 25332 KB Output is correct
40 Correct 13 ms 25324 KB Output is correct
41 Correct 13 ms 25320 KB Output is correct
42 Correct 13 ms 25300 KB Output is correct
43 Correct 13 ms 25416 KB Output is correct
44 Correct 13 ms 25364 KB Output is correct
45 Correct 15 ms 25300 KB Output is correct
46 Correct 13 ms 25300 KB Output is correct
47 Correct 13 ms 25300 KB Output is correct
48 Correct 13 ms 25392 KB Output is correct
49 Correct 12 ms 25300 KB Output is correct
50 Correct 13 ms 25300 KB Output is correct
51 Correct 13 ms 25156 KB Output is correct
52 Correct 13 ms 25172 KB Output is correct
53 Correct 13 ms 25172 KB Output is correct
54 Correct 13 ms 25212 KB Output is correct
55 Correct 15 ms 25288 KB Output is correct
56 Correct 14 ms 25316 KB Output is correct
57 Correct 14 ms 25860 KB Output is correct
58 Correct 15 ms 26452 KB Output is correct
59 Correct 19 ms 28060 KB Output is correct
60 Correct 18 ms 28160 KB Output is correct
61 Correct 20 ms 28116 KB Output is correct
62 Correct 20 ms 28116 KB Output is correct
63 Correct 20 ms 28208 KB Output is correct
64 Correct 17 ms 26836 KB Output is correct
65 Correct 19 ms 27348 KB Output is correct
66 Correct 19 ms 27628 KB Output is correct
67 Correct 19 ms 27732 KB Output is correct
68 Correct 18 ms 27324 KB Output is correct
69 Correct 18 ms 27348 KB Output is correct
70 Correct 20 ms 28116 KB Output is correct
71 Correct 45 ms 37580 KB Output is correct
72 Correct 88 ms 39708 KB Output is correct
73 Correct 188 ms 83260 KB Output is correct
74 Correct 184 ms 83536 KB Output is correct
75 Correct 184 ms 83924 KB Output is correct
76 Correct 183 ms 83828 KB Output is correct
77 Correct 189 ms 83980 KB Output is correct
78 Correct 118 ms 57060 KB Output is correct
79 Correct 84 ms 40216 KB Output is correct
80 Correct 163 ms 72264 KB Output is correct
81 Correct 163 ms 72708 KB Output is correct
82 Correct 136 ms 60392 KB Output is correct
83 Correct 132 ms 60360 KB Output is correct
84 Correct 186 ms 84048 KB Output is correct
85 Correct 546 ms 167264 KB Output is correct
86 Correct 1592 ms 365740 KB Output is correct
87 Correct 1674 ms 366564 KB Output is correct
88 Correct 1604 ms 367104 KB Output is correct
89 Correct 1570 ms 367628 KB Output is correct
90 Correct 1662 ms 367580 KB Output is correct
91 Correct 487 ms 104488 KB Output is correct
92 Correct 506 ms 94924 KB Output is correct
93 Correct 1201 ms 278488 KB Output is correct
94 Correct 1256 ms 297088 KB Output is correct
95 Correct 957 ms 219164 KB Output is correct
96 Correct 969 ms 219088 KB Output is correct
97 Correct 1575 ms 367516 KB Output is correct