Submission #987939

# Submission time Handle Problem Language Result Execution time Memory
987939 2024-05-23T19:39:18 Z Antekb Copy and Paste 3 (JOI22_copypaste3) C++17
100 / 100
766 ms 82608 KB
#include<bits/stdc++.h>
 
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("trapv")
 
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
 
using namespace std;
 
///~~~~~~~~~~~~~~~~~~~~~~~~~~
 
template <typename H, typename T> 
ostream& operator<<(ostream& os, pair<H, T> m){
	return os <<"("<< m.st<<", "<<m.nd<<")";
}
template <typename H> 
ostream& operator<<(ostream& os, vector<H> V){
	os<<"{";
	for(int i=0; i<V.size(); i++){
		if(i)os<<" ";
		os<<V[i];
	}
	os<<"}";
	return os;
}
 
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);
//#define deb(x...) ;
 
///~~~~~~~~~~~~~~~~~~~~~~~~~
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

 
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=1<<20, INF=1e9+5, mod2=1e9+7, mod=998244353;

struct modint{
	int n=0;
	modint(){}
	modint(ll x){
		n=x%mod;
		if(n<0)n+=mod;
	}
	operator int(){
		return n;
	}
	modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;}
	modint operator+=(modint a){return (*this)=(*this)+a;}
	modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;}
	modint operator-=(modint a){return (*this)=(*this)-a;}
	modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;}
	modint operator*=(modint a){return (*this)=(*this)*a;}
	modint por(const ll &m)const{
		modint a(1);
		if(m==0)return a;
		if(m==1)return (*this);
		a=por(m/2);
		a*=a;
		return a*(por(m&1));
	}
	modint odw(){
		return por((ll)mod-2);
	}
	modint operator/(modint a){return (*this)*a.odw();}
	modint operator/=(modint a){return (*this)=(*this)/a;}
	bool operator==(modint a){return a.n==n;}
	friend ostream& operator<<(ostream& os, modint m) {
		return os << m.n; 
	}
};
modint fact[N], fact2[N];
typedef vector<modint> vm;
void factinit(){
    fact[0]=1;
    for(int i=1; i<N; i++){
        fact[i]=(fact[i-1]*modint(i))%mod;
    }
    fact2[N-1]=fact[N-1].odw();
    for(int i=N-2; i>=0; i--){
    	fact2[i]=(fact2[i+1]*modint(i+1))%mod;
    }
}
modint npok(int _n, int _k){
    return fact[_n]*fact2[_k]*fact2[_n-_k];
}
const int M=2505;
ll dp[M][M];
int nxt[M][M];
int jmp[M][M];
int main(){
	//factinit();
	BOOST;
	int n;
	cin>>n;
	string s;
	cin>>s;
	int a, b, c;
	cin>>a>>b>>c;
	for(int i=0; i<n; i++){
		int t=i+1;
		while(t<n && s[i]!=s[t]){
			t++;
		}
		nxt[i][i]=jmp[i][i]=t;
		for(int j=i; j<n; j++)dp[i][j]=1e18;
	}
	deb(jmp[0][0], jmp[1][1]);
	for(int d=0; d<n; d++){
		if(d){
			for(int i=0; i<n-d; i++){
				int t=nxt[i][i+d-1];
				while(t+d<n && s[i+d]!=s[t+d]){
					t=nxt[t][t+d-1];
				}
				if(t+d>=n)t=n;
				nxt[i][i+d]=t;
				t=jmp[i][i+d-1];
				if(t+d>=n)t=n;
				while(t+d<n && (s[i+d]!=s[t+d] || t<=i+d)){
					t=nxt[t][t+d-1];
				}
				jmp[i][i+d]=t;
				//deb(i, i+d, nxt[i][i+d], jmp[i][i+d]);
			}
		}
		for(int i=0; i<n-d; i++){
			dp[i][i+d]=min(dp[i][i+d], dp[i+1][i+d]+a);
			dp[i][i+d]=min(dp[i][i+d], dp[i][i+d-1]+a);
			int t=jmp[i][i+d];
			for(int j=2; t+d<n; j++, t=jmp[t][t+d]){
				//deb(t, jmp[t][t+d], j, (t+d-i+1-(d+1)*j),dp[i][i+d],b,j*c);
				dp[i][t+d]=min(dp[i][t+d], a*1ll*(t+d-i+1-(d+1)*j)+dp[i][i+d]+b+j*1ll*c);
			}
		}
	}
	cout<<dp[0][n-1]<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14172 KB Output is correct
2 Correct 6 ms 8540 KB Output is correct
3 Correct 6 ms 10332 KB Output is correct
4 Correct 8 ms 8648 KB Output is correct
5 Correct 7 ms 10332 KB Output is correct
6 Correct 6 ms 12120 KB Output is correct
7 Correct 7 ms 8540 KB Output is correct
8 Correct 8 ms 8540 KB Output is correct
9 Correct 6 ms 10500 KB Output is correct
10 Correct 7 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8796 KB Output is correct
2 Correct 7 ms 10224 KB Output is correct
3 Correct 263 ms 56436 KB Output is correct
4 Correct 306 ms 62036 KB Output is correct
5 Correct 468 ms 68264 KB Output is correct
6 Correct 496 ms 75140 KB Output is correct
7 Correct 730 ms 82524 KB Output is correct
8 Correct 766 ms 82528 KB Output is correct
9 Correct 632 ms 82424 KB Output is correct
10 Correct 9 ms 8668 KB Output is correct
11 Correct 9 ms 8676 KB Output is correct
12 Correct 8 ms 8540 KB Output is correct
13 Correct 7 ms 8676 KB Output is correct
14 Correct 16 ms 8780 KB Output is correct
15 Correct 8 ms 8792 KB Output is correct
16 Correct 9 ms 9052 KB Output is correct
17 Correct 8 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14172 KB Output is correct
2 Correct 6 ms 8540 KB Output is correct
3 Correct 6 ms 10332 KB Output is correct
4 Correct 8 ms 8648 KB Output is correct
5 Correct 7 ms 10332 KB Output is correct
6 Correct 6 ms 12120 KB Output is correct
7 Correct 7 ms 8540 KB Output is correct
8 Correct 8 ms 8540 KB Output is correct
9 Correct 6 ms 10500 KB Output is correct
10 Correct 7 ms 8540 KB Output is correct
11 Correct 7 ms 8796 KB Output is correct
12 Correct 10 ms 8880 KB Output is correct
13 Correct 7 ms 8792 KB Output is correct
14 Correct 9 ms 8676 KB Output is correct
15 Correct 9 ms 8796 KB Output is correct
16 Correct 8 ms 8796 KB Output is correct
17 Correct 9 ms 8540 KB Output is correct
18 Correct 7 ms 8540 KB Output is correct
19 Correct 7 ms 8796 KB Output is correct
20 Correct 9 ms 8540 KB Output is correct
21 Correct 8 ms 9004 KB Output is correct
22 Correct 8 ms 8872 KB Output is correct
23 Correct 8 ms 8868 KB Output is correct
24 Correct 8 ms 9048 KB Output is correct
25 Correct 8 ms 9052 KB Output is correct
26 Correct 8 ms 9052 KB Output is correct
27 Correct 8 ms 9052 KB Output is correct
28 Correct 9 ms 9048 KB Output is correct
29 Correct 8 ms 9052 KB Output is correct
30 Correct 8 ms 9052 KB Output is correct
31 Correct 7 ms 9052 KB Output is correct
32 Correct 9 ms 9052 KB Output is correct
33 Correct 7 ms 9048 KB Output is correct
34 Correct 8 ms 8736 KB Output is correct
35 Correct 10 ms 8540 KB Output is correct
36 Correct 7 ms 8540 KB Output is correct
37 Correct 7 ms 8776 KB Output is correct
38 Correct 7 ms 8796 KB Output is correct
39 Correct 7 ms 8936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14172 KB Output is correct
2 Correct 6 ms 8540 KB Output is correct
3 Correct 6 ms 10332 KB Output is correct
4 Correct 8 ms 8648 KB Output is correct
5 Correct 7 ms 10332 KB Output is correct
6 Correct 6 ms 12120 KB Output is correct
7 Correct 7 ms 8540 KB Output is correct
8 Correct 8 ms 8540 KB Output is correct
9 Correct 6 ms 10500 KB Output is correct
10 Correct 7 ms 8540 KB Output is correct
11 Correct 7 ms 8796 KB Output is correct
12 Correct 10 ms 8880 KB Output is correct
13 Correct 7 ms 8792 KB Output is correct
14 Correct 9 ms 8676 KB Output is correct
15 Correct 9 ms 8796 KB Output is correct
16 Correct 8 ms 8796 KB Output is correct
17 Correct 9 ms 8540 KB Output is correct
18 Correct 7 ms 8540 KB Output is correct
19 Correct 7 ms 8796 KB Output is correct
20 Correct 9 ms 8540 KB Output is correct
21 Correct 8 ms 9004 KB Output is correct
22 Correct 8 ms 8872 KB Output is correct
23 Correct 8 ms 8868 KB Output is correct
24 Correct 8 ms 9048 KB Output is correct
25 Correct 8 ms 9052 KB Output is correct
26 Correct 8 ms 9052 KB Output is correct
27 Correct 8 ms 9052 KB Output is correct
28 Correct 9 ms 9048 KB Output is correct
29 Correct 8 ms 9052 KB Output is correct
30 Correct 8 ms 9052 KB Output is correct
31 Correct 7 ms 9052 KB Output is correct
32 Correct 9 ms 9052 KB Output is correct
33 Correct 7 ms 9048 KB Output is correct
34 Correct 8 ms 8736 KB Output is correct
35 Correct 10 ms 8540 KB Output is correct
36 Correct 7 ms 8540 KB Output is correct
37 Correct 7 ms 8776 KB Output is correct
38 Correct 7 ms 8796 KB Output is correct
39 Correct 7 ms 8936 KB Output is correct
40 Correct 7 ms 9564 KB Output is correct
41 Correct 12 ms 11240 KB Output is correct
42 Correct 12 ms 11352 KB Output is correct
43 Correct 8 ms 11356 KB Output is correct
44 Correct 10 ms 11168 KB Output is correct
45 Correct 9 ms 11516 KB Output is correct
46 Correct 13 ms 11356 KB Output is correct
47 Correct 8 ms 11356 KB Output is correct
48 Correct 8 ms 11356 KB Output is correct
49 Correct 10 ms 11372 KB Output is correct
50 Correct 11 ms 11352 KB Output is correct
51 Correct 10 ms 11232 KB Output is correct
52 Correct 10 ms 11356 KB Output is correct
53 Correct 9 ms 11408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14172 KB Output is correct
2 Correct 6 ms 8540 KB Output is correct
3 Correct 6 ms 10332 KB Output is correct
4 Correct 8 ms 8648 KB Output is correct
5 Correct 7 ms 10332 KB Output is correct
6 Correct 6 ms 12120 KB Output is correct
7 Correct 7 ms 8540 KB Output is correct
8 Correct 8 ms 8540 KB Output is correct
9 Correct 6 ms 10500 KB Output is correct
10 Correct 7 ms 8540 KB Output is correct
11 Correct 7 ms 8796 KB Output is correct
12 Correct 10 ms 8880 KB Output is correct
13 Correct 7 ms 8792 KB Output is correct
14 Correct 9 ms 8676 KB Output is correct
15 Correct 9 ms 8796 KB Output is correct
16 Correct 8 ms 8796 KB Output is correct
17 Correct 9 ms 8540 KB Output is correct
18 Correct 7 ms 8540 KB Output is correct
19 Correct 7 ms 8796 KB Output is correct
20 Correct 9 ms 8540 KB Output is correct
21 Correct 8 ms 9004 KB Output is correct
22 Correct 8 ms 8872 KB Output is correct
23 Correct 8 ms 8868 KB Output is correct
24 Correct 8 ms 9048 KB Output is correct
25 Correct 8 ms 9052 KB Output is correct
26 Correct 8 ms 9052 KB Output is correct
27 Correct 8 ms 9052 KB Output is correct
28 Correct 9 ms 9048 KB Output is correct
29 Correct 8 ms 9052 KB Output is correct
30 Correct 8 ms 9052 KB Output is correct
31 Correct 7 ms 9052 KB Output is correct
32 Correct 9 ms 9052 KB Output is correct
33 Correct 7 ms 9048 KB Output is correct
34 Correct 8 ms 8736 KB Output is correct
35 Correct 10 ms 8540 KB Output is correct
36 Correct 7 ms 8540 KB Output is correct
37 Correct 7 ms 8776 KB Output is correct
38 Correct 7 ms 8796 KB Output is correct
39 Correct 7 ms 8936 KB Output is correct
40 Correct 7 ms 9564 KB Output is correct
41 Correct 12 ms 11240 KB Output is correct
42 Correct 12 ms 11352 KB Output is correct
43 Correct 8 ms 11356 KB Output is correct
44 Correct 10 ms 11168 KB Output is correct
45 Correct 9 ms 11516 KB Output is correct
46 Correct 13 ms 11356 KB Output is correct
47 Correct 8 ms 11356 KB Output is correct
48 Correct 8 ms 11356 KB Output is correct
49 Correct 10 ms 11372 KB Output is correct
50 Correct 11 ms 11352 KB Output is correct
51 Correct 10 ms 11232 KB Output is correct
52 Correct 10 ms 11356 KB Output is correct
53 Correct 9 ms 11408 KB Output is correct
54 Correct 12 ms 15452 KB Output is correct
55 Correct 51 ms 28404 KB Output is correct
56 Correct 36 ms 28436 KB Output is correct
57 Correct 33 ms 28540 KB Output is correct
58 Correct 32 ms 28396 KB Output is correct
59 Correct 32 ms 28464 KB Output is correct
60 Correct 30 ms 28460 KB Output is correct
61 Correct 38 ms 28496 KB Output is correct
62 Correct 48 ms 28292 KB Output is correct
63 Correct 36 ms 28404 KB Output is correct
64 Correct 37 ms 28508 KB Output is correct
65 Correct 40 ms 28356 KB Output is correct
66 Correct 53 ms 28304 KB Output is correct
67 Correct 35 ms 28424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14172 KB Output is correct
2 Correct 6 ms 8540 KB Output is correct
3 Correct 6 ms 10332 KB Output is correct
4 Correct 8 ms 8648 KB Output is correct
5 Correct 7 ms 10332 KB Output is correct
6 Correct 6 ms 12120 KB Output is correct
7 Correct 7 ms 8540 KB Output is correct
8 Correct 8 ms 8540 KB Output is correct
9 Correct 6 ms 10500 KB Output is correct
10 Correct 7 ms 8540 KB Output is correct
11 Correct 9 ms 8796 KB Output is correct
12 Correct 7 ms 10224 KB Output is correct
13 Correct 263 ms 56436 KB Output is correct
14 Correct 306 ms 62036 KB Output is correct
15 Correct 468 ms 68264 KB Output is correct
16 Correct 496 ms 75140 KB Output is correct
17 Correct 730 ms 82524 KB Output is correct
18 Correct 766 ms 82528 KB Output is correct
19 Correct 632 ms 82424 KB Output is correct
20 Correct 9 ms 8668 KB Output is correct
21 Correct 9 ms 8676 KB Output is correct
22 Correct 8 ms 8540 KB Output is correct
23 Correct 7 ms 8676 KB Output is correct
24 Correct 16 ms 8780 KB Output is correct
25 Correct 8 ms 8792 KB Output is correct
26 Correct 9 ms 9052 KB Output is correct
27 Correct 8 ms 8540 KB Output is correct
28 Correct 7 ms 8796 KB Output is correct
29 Correct 10 ms 8880 KB Output is correct
30 Correct 7 ms 8792 KB Output is correct
31 Correct 9 ms 8676 KB Output is correct
32 Correct 9 ms 8796 KB Output is correct
33 Correct 8 ms 8796 KB Output is correct
34 Correct 9 ms 8540 KB Output is correct
35 Correct 7 ms 8540 KB Output is correct
36 Correct 7 ms 8796 KB Output is correct
37 Correct 9 ms 8540 KB Output is correct
38 Correct 8 ms 9004 KB Output is correct
39 Correct 8 ms 8872 KB Output is correct
40 Correct 8 ms 8868 KB Output is correct
41 Correct 8 ms 9048 KB Output is correct
42 Correct 8 ms 9052 KB Output is correct
43 Correct 8 ms 9052 KB Output is correct
44 Correct 8 ms 9052 KB Output is correct
45 Correct 9 ms 9048 KB Output is correct
46 Correct 8 ms 9052 KB Output is correct
47 Correct 8 ms 9052 KB Output is correct
48 Correct 7 ms 9052 KB Output is correct
49 Correct 9 ms 9052 KB Output is correct
50 Correct 7 ms 9048 KB Output is correct
51 Correct 8 ms 8736 KB Output is correct
52 Correct 10 ms 8540 KB Output is correct
53 Correct 7 ms 8540 KB Output is correct
54 Correct 7 ms 8776 KB Output is correct
55 Correct 7 ms 8796 KB Output is correct
56 Correct 7 ms 8936 KB Output is correct
57 Correct 7 ms 9564 KB Output is correct
58 Correct 12 ms 11240 KB Output is correct
59 Correct 12 ms 11352 KB Output is correct
60 Correct 8 ms 11356 KB Output is correct
61 Correct 10 ms 11168 KB Output is correct
62 Correct 9 ms 11516 KB Output is correct
63 Correct 13 ms 11356 KB Output is correct
64 Correct 8 ms 11356 KB Output is correct
65 Correct 8 ms 11356 KB Output is correct
66 Correct 10 ms 11372 KB Output is correct
67 Correct 11 ms 11352 KB Output is correct
68 Correct 10 ms 11232 KB Output is correct
69 Correct 10 ms 11356 KB Output is correct
70 Correct 9 ms 11408 KB Output is correct
71 Correct 12 ms 15452 KB Output is correct
72 Correct 51 ms 28404 KB Output is correct
73 Correct 36 ms 28436 KB Output is correct
74 Correct 33 ms 28540 KB Output is correct
75 Correct 32 ms 28396 KB Output is correct
76 Correct 32 ms 28464 KB Output is correct
77 Correct 30 ms 28460 KB Output is correct
78 Correct 38 ms 28496 KB Output is correct
79 Correct 48 ms 28292 KB Output is correct
80 Correct 36 ms 28404 KB Output is correct
81 Correct 37 ms 28508 KB Output is correct
82 Correct 40 ms 28356 KB Output is correct
83 Correct 53 ms 28304 KB Output is correct
84 Correct 35 ms 28424 KB Output is correct
85 Correct 77 ms 47140 KB Output is correct
86 Correct 221 ms 82548 KB Output is correct
87 Correct 186 ms 82476 KB Output is correct
88 Correct 191 ms 82608 KB Output is correct
89 Correct 198 ms 82544 KB Output is correct
90 Correct 171 ms 82444 KB Output is correct
91 Correct 385 ms 82400 KB Output is correct
92 Correct 443 ms 82364 KB Output is correct
93 Correct 194 ms 82516 KB Output is correct
94 Correct 239 ms 82416 KB Output is correct
95 Correct 437 ms 82420 KB Output is correct
96 Correct 405 ms 82516 KB Output is correct
97 Correct 160 ms 82368 KB Output is correct