Submission #547414

# Submission time Handle Problem Language Result Execution time Memory
547414 2022-04-10T16:12:32 Z skittles1412 Copy and Paste 3 (JOI22_copypaste3) C++17
100 / 100
917 ms 342816 KB
#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
	cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
	cerr << t << " | ";
	dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
	cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
		 << ": ";                                          \
	dbgh(__VA_ARGS__)
#else
#define cerr   \
	if (false) \
	cerr
#define dbg(...)
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())

const long mod = 1e9 + 7;

long bpow(long base, long exp) {
	long ans = 1;
	while (exp) {
		if (exp & 1) {
			ans = (ans * base) % mod;
		}
		base = (base * base) % mod;
		exp >>= 1;
	}
	return ans;
}

long inv(long x) {
	return bpow(x, mod - 2);
}

struct Hasher {
	int n;
	vector<long> psum, ipow;

	Hasher(const string& s, long base) : n(sz(s)), psum(n + 1), ipow(n + 1) {
		ipow[0] = 1;
		long ibase = inv(base), exp = 1;
		for (int i = 0; i < n; i++) {
			psum[i + 1] = (psum[i] + (s[i] - 'a' + 1) * exp) % mod;
			exp = (exp * base) % mod;
			ipow[i + 1] = (ipow[i] * ibase) % mod;
		}
	}

	long hash(int l, int r) const {
		return (psum[r] - psum[l] + mod) * ipow[l] % mod;
	}
};

struct H {
	Hasher h1, h2;

	H(const string& s) : h1(s, 31), h2(s, 29) {}

	long hash(int l, int r) const {
		return h1.hash(l, r) * mod + h2.hash(l, r);
	}
};

void solve1(int n, long wa, long wb, long wc) {
	long dp[n + 1];
	dp[0] = 0;
	for (int i = 1; i <= n; i++) {
		dp[i] = wa * i;
		for (int j = 1; j < i; j++) {
			int cnt = i / j;
			dp[i] = min(dp[i], wa * i + dp[j] + wb + cnt * (wc - wa * j));
		}
	}
	cout << dp[n] << endl;
}

void solve() {
	int n;
	cin >> n;
	string s;
	cin >> s;
	long wa, wb, wc;
	cin >> wa >> wb >> wc;
	{
		for (auto& a : s) {
			if (a != 'a') {
				goto loop;
			}
		}
		solve1(n, wa, wb, wc);
		return;
	loop:;
	}
	H h(s);
	pair<int, int> mp[n][n];
	for (int i = 1; i <= n; i++) {
		map<long, pair<int, int>> m;
		for (int j = n - i; j >= 0; j--) {
			mp[j][j + i - 1] =
				m.emplace(h.hash(j, j + i), pair<int, int> {j, j + i - 1}).first->second;
		}
	}
	long dp[n][n], mn[n];
	memset(mn, 0x3f, sizeof(mn));
	vector<pair<int, int>> occ[n][n];
	for (int l = n - 1; l >= 0; l--) {
		long copt = 0;
		for (int r = l; r < n; r++) {
			copt = min(copt + wa, wa * (r - l + 1) + mn[r]);
			dp[l][r] = copt;
			{
				auto [il, ir] = mp[l][r];
				int len = r - l + 1;
				int nbs =
					int(occ[il][ir].rend() - lower_bound(occ[il][ir].rbegin(), occ[il][ir].rend(),
														 pair<int, int> {l + len, -100})) -
					1;
				int ci = sz(occ[il][ir]), x = 0;
				occ[il][ir].emplace_back(l, nbs);
				while (ci != -1) {
					auto [pos, nxt] = occ[il][ir][ci];
					int np = pos + len - 1;
					x++;
					mn[np] = min(mn[np], dp[il][ir] + wb + x * (wc - wa * len));
					ci = nxt;
				}
			}
		}
	}
	cout << dp[0][n - 1] << endl;
}

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cin.exceptions(ios::failbit);
	solve();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 9 ms 328 KB Output is correct
4 Correct 11 ms 332 KB Output is correct
5 Correct 12 ms 340 KB Output is correct
6 Correct 14 ms 212 KB Output is correct
7 Correct 16 ms 340 KB Output is correct
8 Correct 16 ms 340 KB Output is correct
9 Correct 16 ms 336 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 596 KB Output is correct
41 Correct 4 ms 2004 KB Output is correct
42 Correct 6 ms 2388 KB Output is correct
43 Correct 5 ms 2388 KB Output is correct
44 Correct 5 ms 2388 KB Output is correct
45 Correct 5 ms 2388 KB Output is correct
46 Correct 5 ms 2388 KB Output is correct
47 Correct 4 ms 2132 KB Output is correct
48 Correct 6 ms 2260 KB Output is correct
49 Correct 4 ms 2304 KB Output is correct
50 Correct 4 ms 2260 KB Output is correct
51 Correct 4 ms 2260 KB Output is correct
52 Correct 4 ms 2260 KB Output is correct
53 Correct 5 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 596 KB Output is correct
41 Correct 4 ms 2004 KB Output is correct
42 Correct 6 ms 2388 KB Output is correct
43 Correct 5 ms 2388 KB Output is correct
44 Correct 5 ms 2388 KB Output is correct
45 Correct 5 ms 2388 KB Output is correct
46 Correct 5 ms 2388 KB Output is correct
47 Correct 4 ms 2132 KB Output is correct
48 Correct 6 ms 2260 KB Output is correct
49 Correct 4 ms 2304 KB Output is correct
50 Correct 4 ms 2260 KB Output is correct
51 Correct 4 ms 2260 KB Output is correct
52 Correct 4 ms 2260 KB Output is correct
53 Correct 5 ms 2388 KB Output is correct
54 Correct 23 ms 11200 KB Output is correct
55 Correct 105 ms 44636 KB Output is correct
56 Correct 124 ms 54932 KB Output is correct
57 Correct 123 ms 55040 KB Output is correct
58 Correct 115 ms 54988 KB Output is correct
59 Correct 111 ms 55076 KB Output is correct
60 Correct 116 ms 55068 KB Output is correct
61 Correct 99 ms 47556 KB Output is correct
62 Correct 96 ms 45452 KB Output is correct
63 Correct 108 ms 51708 KB Output is correct
64 Correct 109 ms 52276 KB Output is correct
65 Correct 114 ms 48748 KB Output is correct
66 Correct 109 ms 48864 KB Output is correct
67 Correct 136 ms 55104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 9 ms 328 KB Output is correct
14 Correct 11 ms 332 KB Output is correct
15 Correct 12 ms 340 KB Output is correct
16 Correct 14 ms 212 KB Output is correct
17 Correct 16 ms 340 KB Output is correct
18 Correct 16 ms 340 KB Output is correct
19 Correct 16 ms 336 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 0 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 0 ms 212 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 0 ms 212 KB Output is correct
56 Correct 0 ms 212 KB Output is correct
57 Correct 1 ms 596 KB Output is correct
58 Correct 4 ms 2004 KB Output is correct
59 Correct 6 ms 2388 KB Output is correct
60 Correct 5 ms 2388 KB Output is correct
61 Correct 5 ms 2388 KB Output is correct
62 Correct 5 ms 2388 KB Output is correct
63 Correct 5 ms 2388 KB Output is correct
64 Correct 4 ms 2132 KB Output is correct
65 Correct 6 ms 2260 KB Output is correct
66 Correct 4 ms 2304 KB Output is correct
67 Correct 4 ms 2260 KB Output is correct
68 Correct 4 ms 2260 KB Output is correct
69 Correct 4 ms 2260 KB Output is correct
70 Correct 5 ms 2388 KB Output is correct
71 Correct 23 ms 11200 KB Output is correct
72 Correct 105 ms 44636 KB Output is correct
73 Correct 124 ms 54932 KB Output is correct
74 Correct 123 ms 55040 KB Output is correct
75 Correct 115 ms 54988 KB Output is correct
76 Correct 111 ms 55076 KB Output is correct
77 Correct 116 ms 55068 KB Output is correct
78 Correct 99 ms 47556 KB Output is correct
79 Correct 96 ms 45452 KB Output is correct
80 Correct 108 ms 51708 KB Output is correct
81 Correct 109 ms 52276 KB Output is correct
82 Correct 114 ms 48748 KB Output is correct
83 Correct 109 ms 48864 KB Output is correct
84 Correct 136 ms 55104 KB Output is correct
85 Correct 303 ms 136972 KB Output is correct
86 Correct 837 ms 342288 KB Output is correct
87 Correct 870 ms 342656 KB Output is correct
88 Correct 846 ms 342712 KB Output is correct
89 Correct 844 ms 342668 KB Output is correct
90 Correct 832 ms 342816 KB Output is correct
91 Correct 560 ms 283444 KB Output is correct
92 Correct 862 ms 281628 KB Output is correct
93 Correct 758 ms 315972 KB Output is correct
94 Correct 788 ms 325480 KB Output is correct
95 Correct 768 ms 303164 KB Output is correct
96 Correct 750 ms 303260 KB Output is correct
97 Correct 917 ms 342732 KB Output is correct