답안 #617435

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
617435 2022-08-01T11:19:43 Z patrikpavic2 Copy and Paste 3 (JOI22_copypaste3) C++17
100 / 100
602 ms 101084 KB
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <unordered_map>

#define PB push_back

using namespace std;

typedef long long ll;
typedef vector < int > vi;

const int N = 2550;
const int MOD1 = 1e9 + 7;
const int MOD2 = 1e9 + 9;
const int BASE1 = 1e6 + 3;
const int BASE2 = 31337;

inline int add1(int A, int B){
	if(A + B >= MOD1)
		return A + B - MOD1;
	return A + B;
}

inline int sub1(int A, int B){
	if(A - B < 0)
		return A - B + MOD1;
	return A - B;
}

inline int mul1(int A, int B){
	return (ll)A * B % MOD1;
}

inline int add2(int A, int B){
	if(A + B >= MOD2)
		return A + B - MOD2;
	return A + B;
}

inline int sub2(int A, int B){
	if(A - B < 0)
		return A - B + MOD2;
	return A - B;
}

inline int mul2(int A, int B){
	return (ll)A * B % MOD2;
}

ll dp[N][N], A, B, C;
vi divv[N];
int pot1[N], pot2[N], n;
ll HSH[N][N];
int nxt[N][N], hsh1[N], hsh2[N];
char s[N];
unordered_map < ll, int > bio;

int get_hsh1(int l, int r){
	return mul1(sub1(hsh1[r], (l ? hsh1[l - 1] : 0)), pot1[N - r - 1]);
}

int get_hsh2(int l, int r){
	return mul2(sub2(hsh2[r], (l ? hsh2[l - 1] : 0)), pot2[N - r - 1]);
}

void precompute(){
	pot1[0] = 1; pot2[0] = 1;
	for(int i = 1;i < N;i++)
		pot1[i] = mul1(pot1[i - 1], BASE1);
	for(int i = 1;i < N;i++)
		pot2[i] = mul2(pot2[i - 1], BASE2);
	for(int i = 0;i < n;i++){
		hsh1[i] = add1(mul1(s[i], pot1[i]), (i ? hsh1[i - 1] : 0));
		hsh2[i] = add2(mul2(s[i], pot2[i]), (i ? hsh2[i - 1] : 0));
	}
	for(int i = 0;i < n;i++)
		for(int j = i;j < n;j++)
			HSH[i][j] = get_hsh1(i, j) * MOD2 + get_hsh2(i, j);
	for(int ln = 1;ln <= n;ln++){
		bio.clear();
		for(int i = n - ln;i >= 0;i--){
			if(i + 2 * ln <= n)
				bio[HSH[i + ln][i + 2 * ln - 1]] = i + ln;
			if(bio[HSH[i][i + ln - 1]]){
				nxt[i][ln] = bio[HSH[i][i + ln - 1]];
			}
		}
	}
}


void dinamika(){
	for(int l = n - 1;l >= 0;l--){
		dp[l][l] = A;
		for(int r = l + 1;r <= n;r++)
			dp[l][r] = A + dp[l + 1][r];
		for(int k = 1;2 * k <= n - l;k++){
			if(k - 1) 
				dp[l][l + k - 1] = min(dp[l][l + k - 1], dp[l][l + k - 2] + A);
			ll cur = dp[l][l + k - 1] + B + C;
			int zad = l;
			while(nxt[zad][k]){
				cur += C + A * (nxt[zad][k] - (zad + k));
				zad = nxt[zad][k];
				dp[l][zad + k - 1] = min(dp[l][zad + k - 1], cur);
			}
		}
		for(int i = l + 1;i < n;i++)
			dp[l][i] = min(dp[l][i], dp[l][i - 1] + A);
	}
}

int main(){
	memset(dp, -1, sizeof(dp));
	scanf("%d%s%lld%lld%lld", &n, s, &A, &B, &C);
	precompute();
	dinamika();
	printf("%lld\n", dp[0][n - 1]);
	return 0;
}

Compilation message

copypaste3.cpp: In function 'int main()':
copypaste3.cpp:117:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |  scanf("%d%s%lld%lld%lld", &n, s, &A, &B, &C);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 51284 KB Output is correct
2 Correct 22 ms 51252 KB Output is correct
3 Correct 20 ms 51248 KB Output is correct
4 Correct 23 ms 51240 KB Output is correct
5 Correct 21 ms 51196 KB Output is correct
6 Correct 24 ms 51280 KB Output is correct
7 Correct 20 ms 51240 KB Output is correct
8 Correct 19 ms 51240 KB Output is correct
9 Correct 21 ms 51224 KB Output is correct
10 Correct 21 ms 51272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 51328 KB Output is correct
2 Correct 18 ms 51284 KB Output is correct
3 Correct 256 ms 82252 KB Output is correct
4 Correct 276 ms 86220 KB Output is correct
5 Correct 336 ms 90760 KB Output is correct
6 Correct 426 ms 95676 KB Output is correct
7 Correct 525 ms 101084 KB Output is correct
8 Correct 602 ms 101084 KB Output is correct
9 Correct 496 ms 100996 KB Output is correct
10 Correct 22 ms 51184 KB Output is correct
11 Correct 21 ms 51284 KB Output is correct
12 Correct 24 ms 51268 KB Output is correct
13 Correct 23 ms 51224 KB Output is correct
14 Correct 26 ms 51324 KB Output is correct
15 Correct 21 ms 51320 KB Output is correct
16 Correct 24 ms 51504 KB Output is correct
17 Correct 23 ms 51216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 51284 KB Output is correct
2 Correct 22 ms 51252 KB Output is correct
3 Correct 20 ms 51248 KB Output is correct
4 Correct 23 ms 51240 KB Output is correct
5 Correct 21 ms 51196 KB Output is correct
6 Correct 24 ms 51280 KB Output is correct
7 Correct 20 ms 51240 KB Output is correct
8 Correct 19 ms 51240 KB Output is correct
9 Correct 21 ms 51224 KB Output is correct
10 Correct 21 ms 51272 KB Output is correct
11 Correct 23 ms 51324 KB Output is correct
12 Correct 23 ms 51412 KB Output is correct
13 Correct 21 ms 51428 KB Output is correct
14 Correct 22 ms 51296 KB Output is correct
15 Correct 22 ms 51368 KB Output is correct
16 Correct 29 ms 51332 KB Output is correct
17 Correct 24 ms 51184 KB Output is correct
18 Correct 22 ms 51168 KB Output is correct
19 Correct 22 ms 51284 KB Output is correct
20 Correct 25 ms 51240 KB Output is correct
21 Correct 25 ms 51512 KB Output is correct
22 Correct 22 ms 51408 KB Output is correct
23 Correct 22 ms 51472 KB Output is correct
24 Correct 23 ms 51488 KB Output is correct
25 Correct 29 ms 51404 KB Output is correct
26 Correct 23 ms 51380 KB Output is correct
27 Correct 22 ms 51412 KB Output is correct
28 Correct 23 ms 51428 KB Output is correct
29 Correct 27 ms 51492 KB Output is correct
30 Correct 23 ms 51420 KB Output is correct
31 Correct 22 ms 51440 KB Output is correct
32 Correct 23 ms 51388 KB Output is correct
33 Correct 25 ms 51324 KB Output is correct
34 Correct 28 ms 51240 KB Output is correct
35 Correct 20 ms 51284 KB Output is correct
36 Correct 22 ms 51276 KB Output is correct
37 Correct 21 ms 51208 KB Output is correct
38 Correct 20 ms 51248 KB Output is correct
39 Correct 19 ms 51480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 51284 KB Output is correct
2 Correct 22 ms 51252 KB Output is correct
3 Correct 20 ms 51248 KB Output is correct
4 Correct 23 ms 51240 KB Output is correct
5 Correct 21 ms 51196 KB Output is correct
6 Correct 24 ms 51280 KB Output is correct
7 Correct 20 ms 51240 KB Output is correct
8 Correct 19 ms 51240 KB Output is correct
9 Correct 21 ms 51224 KB Output is correct
10 Correct 21 ms 51272 KB Output is correct
11 Correct 23 ms 51324 KB Output is correct
12 Correct 23 ms 51412 KB Output is correct
13 Correct 21 ms 51428 KB Output is correct
14 Correct 22 ms 51296 KB Output is correct
15 Correct 22 ms 51368 KB Output is correct
16 Correct 29 ms 51332 KB Output is correct
17 Correct 24 ms 51184 KB Output is correct
18 Correct 22 ms 51168 KB Output is correct
19 Correct 22 ms 51284 KB Output is correct
20 Correct 25 ms 51240 KB Output is correct
21 Correct 25 ms 51512 KB Output is correct
22 Correct 22 ms 51408 KB Output is correct
23 Correct 22 ms 51472 KB Output is correct
24 Correct 23 ms 51488 KB Output is correct
25 Correct 29 ms 51404 KB Output is correct
26 Correct 23 ms 51380 KB Output is correct
27 Correct 22 ms 51412 KB Output is correct
28 Correct 23 ms 51428 KB Output is correct
29 Correct 27 ms 51492 KB Output is correct
30 Correct 23 ms 51420 KB Output is correct
31 Correct 22 ms 51440 KB Output is correct
32 Correct 23 ms 51388 KB Output is correct
33 Correct 25 ms 51324 KB Output is correct
34 Correct 28 ms 51240 KB Output is correct
35 Correct 20 ms 51284 KB Output is correct
36 Correct 22 ms 51276 KB Output is correct
37 Correct 21 ms 51208 KB Output is correct
38 Correct 20 ms 51248 KB Output is correct
39 Correct 19 ms 51480 KB Output is correct
40 Correct 20 ms 51924 KB Output is correct
41 Correct 28 ms 53004 KB Output is correct
42 Correct 24 ms 53076 KB Output is correct
43 Correct 22 ms 52964 KB Output is correct
44 Correct 22 ms 53052 KB Output is correct
45 Correct 23 ms 52984 KB Output is correct
46 Correct 23 ms 52952 KB Output is correct
47 Correct 24 ms 53036 KB Output is correct
48 Correct 27 ms 52880 KB Output is correct
49 Correct 24 ms 52948 KB Output is correct
50 Correct 24 ms 52948 KB Output is correct
51 Correct 24 ms 53076 KB Output is correct
52 Correct 23 ms 53048 KB Output is correct
53 Correct 28 ms 52940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 51284 KB Output is correct
2 Correct 22 ms 51252 KB Output is correct
3 Correct 20 ms 51248 KB Output is correct
4 Correct 23 ms 51240 KB Output is correct
5 Correct 21 ms 51196 KB Output is correct
6 Correct 24 ms 51280 KB Output is correct
7 Correct 20 ms 51240 KB Output is correct
8 Correct 19 ms 51240 KB Output is correct
9 Correct 21 ms 51224 KB Output is correct
10 Correct 21 ms 51272 KB Output is correct
11 Correct 23 ms 51324 KB Output is correct
12 Correct 23 ms 51412 KB Output is correct
13 Correct 21 ms 51428 KB Output is correct
14 Correct 22 ms 51296 KB Output is correct
15 Correct 22 ms 51368 KB Output is correct
16 Correct 29 ms 51332 KB Output is correct
17 Correct 24 ms 51184 KB Output is correct
18 Correct 22 ms 51168 KB Output is correct
19 Correct 22 ms 51284 KB Output is correct
20 Correct 25 ms 51240 KB Output is correct
21 Correct 25 ms 51512 KB Output is correct
22 Correct 22 ms 51408 KB Output is correct
23 Correct 22 ms 51472 KB Output is correct
24 Correct 23 ms 51488 KB Output is correct
25 Correct 29 ms 51404 KB Output is correct
26 Correct 23 ms 51380 KB Output is correct
27 Correct 22 ms 51412 KB Output is correct
28 Correct 23 ms 51428 KB Output is correct
29 Correct 27 ms 51492 KB Output is correct
30 Correct 23 ms 51420 KB Output is correct
31 Correct 22 ms 51440 KB Output is correct
32 Correct 23 ms 51388 KB Output is correct
33 Correct 25 ms 51324 KB Output is correct
34 Correct 28 ms 51240 KB Output is correct
35 Correct 20 ms 51284 KB Output is correct
36 Correct 22 ms 51276 KB Output is correct
37 Correct 21 ms 51208 KB Output is correct
38 Correct 20 ms 51248 KB Output is correct
39 Correct 19 ms 51480 KB Output is correct
40 Correct 20 ms 51924 KB Output is correct
41 Correct 28 ms 53004 KB Output is correct
42 Correct 24 ms 53076 KB Output is correct
43 Correct 22 ms 52964 KB Output is correct
44 Correct 22 ms 53052 KB Output is correct
45 Correct 23 ms 52984 KB Output is correct
46 Correct 23 ms 52952 KB Output is correct
47 Correct 24 ms 53036 KB Output is correct
48 Correct 27 ms 52880 KB Output is correct
49 Correct 24 ms 52948 KB Output is correct
50 Correct 24 ms 52948 KB Output is correct
51 Correct 24 ms 53076 KB Output is correct
52 Correct 23 ms 53048 KB Output is correct
53 Correct 28 ms 52940 KB Output is correct
54 Correct 29 ms 55624 KB Output is correct
55 Correct 85 ms 64208 KB Output is correct
56 Correct 75 ms 63188 KB Output is correct
57 Correct 81 ms 63284 KB Output is correct
58 Correct 80 ms 63260 KB Output is correct
59 Correct 69 ms 63240 KB Output is correct
60 Correct 63 ms 63188 KB Output is correct
61 Correct 58 ms 63844 KB Output is correct
62 Correct 63 ms 64168 KB Output is correct
63 Correct 64 ms 63620 KB Output is correct
64 Correct 69 ms 63564 KB Output is correct
65 Correct 66 ms 64064 KB Output is correct
66 Correct 65 ms 63936 KB Output is correct
67 Correct 65 ms 63180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 51284 KB Output is correct
2 Correct 22 ms 51252 KB Output is correct
3 Correct 20 ms 51248 KB Output is correct
4 Correct 23 ms 51240 KB Output is correct
5 Correct 21 ms 51196 KB Output is correct
6 Correct 24 ms 51280 KB Output is correct
7 Correct 20 ms 51240 KB Output is correct
8 Correct 19 ms 51240 KB Output is correct
9 Correct 21 ms 51224 KB Output is correct
10 Correct 21 ms 51272 KB Output is correct
11 Correct 20 ms 51328 KB Output is correct
12 Correct 18 ms 51284 KB Output is correct
13 Correct 256 ms 82252 KB Output is correct
14 Correct 276 ms 86220 KB Output is correct
15 Correct 336 ms 90760 KB Output is correct
16 Correct 426 ms 95676 KB Output is correct
17 Correct 525 ms 101084 KB Output is correct
18 Correct 602 ms 101084 KB Output is correct
19 Correct 496 ms 100996 KB Output is correct
20 Correct 22 ms 51184 KB Output is correct
21 Correct 21 ms 51284 KB Output is correct
22 Correct 24 ms 51268 KB Output is correct
23 Correct 23 ms 51224 KB Output is correct
24 Correct 26 ms 51324 KB Output is correct
25 Correct 21 ms 51320 KB Output is correct
26 Correct 24 ms 51504 KB Output is correct
27 Correct 23 ms 51216 KB Output is correct
28 Correct 23 ms 51324 KB Output is correct
29 Correct 23 ms 51412 KB Output is correct
30 Correct 21 ms 51428 KB Output is correct
31 Correct 22 ms 51296 KB Output is correct
32 Correct 22 ms 51368 KB Output is correct
33 Correct 29 ms 51332 KB Output is correct
34 Correct 24 ms 51184 KB Output is correct
35 Correct 22 ms 51168 KB Output is correct
36 Correct 22 ms 51284 KB Output is correct
37 Correct 25 ms 51240 KB Output is correct
38 Correct 25 ms 51512 KB Output is correct
39 Correct 22 ms 51408 KB Output is correct
40 Correct 22 ms 51472 KB Output is correct
41 Correct 23 ms 51488 KB Output is correct
42 Correct 29 ms 51404 KB Output is correct
43 Correct 23 ms 51380 KB Output is correct
44 Correct 22 ms 51412 KB Output is correct
45 Correct 23 ms 51428 KB Output is correct
46 Correct 27 ms 51492 KB Output is correct
47 Correct 23 ms 51420 KB Output is correct
48 Correct 22 ms 51440 KB Output is correct
49 Correct 23 ms 51388 KB Output is correct
50 Correct 25 ms 51324 KB Output is correct
51 Correct 28 ms 51240 KB Output is correct
52 Correct 20 ms 51284 KB Output is correct
53 Correct 22 ms 51276 KB Output is correct
54 Correct 21 ms 51208 KB Output is correct
55 Correct 20 ms 51248 KB Output is correct
56 Correct 19 ms 51480 KB Output is correct
57 Correct 20 ms 51924 KB Output is correct
58 Correct 28 ms 53004 KB Output is correct
59 Correct 24 ms 53076 KB Output is correct
60 Correct 22 ms 52964 KB Output is correct
61 Correct 22 ms 53052 KB Output is correct
62 Correct 23 ms 52984 KB Output is correct
63 Correct 23 ms 52952 KB Output is correct
64 Correct 24 ms 53036 KB Output is correct
65 Correct 27 ms 52880 KB Output is correct
66 Correct 24 ms 52948 KB Output is correct
67 Correct 24 ms 52948 KB Output is correct
68 Correct 24 ms 53076 KB Output is correct
69 Correct 23 ms 53048 KB Output is correct
70 Correct 28 ms 52940 KB Output is correct
71 Correct 29 ms 55624 KB Output is correct
72 Correct 85 ms 64208 KB Output is correct
73 Correct 75 ms 63188 KB Output is correct
74 Correct 81 ms 63284 KB Output is correct
75 Correct 80 ms 63260 KB Output is correct
76 Correct 69 ms 63240 KB Output is correct
77 Correct 63 ms 63188 KB Output is correct
78 Correct 58 ms 63844 KB Output is correct
79 Correct 63 ms 64168 KB Output is correct
80 Correct 64 ms 63620 KB Output is correct
81 Correct 69 ms 63564 KB Output is correct
82 Correct 66 ms 64064 KB Output is correct
83 Correct 65 ms 63936 KB Output is correct
84 Correct 65 ms 63180 KB Output is correct
85 Correct 146 ms 73892 KB Output is correct
86 Correct 351 ms 95228 KB Output is correct
87 Correct 359 ms 95284 KB Output is correct
88 Correct 334 ms 95060 KB Output is correct
89 Correct 315 ms 95084 KB Output is correct
90 Correct 341 ms 95024 KB Output is correct
91 Correct 252 ms 100840 KB Output is correct
92 Correct 325 ms 101032 KB Output is correct
93 Correct 339 ms 98548 KB Output is correct
94 Correct 313 ms 97980 KB Output is correct
95 Correct 357 ms 100320 KB Output is correct
96 Correct 366 ms 100220 KB Output is correct
97 Correct 320 ms 95012 KB Output is correct