Submission #548254

# Submission time Handle Problem Language Result Execution time Memory
548254 2022-04-12T19:35:14 Z inksamurai Copy and Paste 3 (JOI22_copypaste3) C++17
100 / 100
2302 ms 299412 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3rfKJyA ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
 
//snuke's mod int
template <ll mod>
struct modint{
	ll x; // typedef long long ll;
	modint(ll x=0):x((x%mod+mod)%mod){}
	modint operator-()const{return modint(-x);}
	modint& operator+=(const modint a){if((x+=a.x)>=mod) x-=mod; return *this;}
	modint& operator-=(const modint a){if((x+=mod-a.x)>=mod) x-=mod; return *this;}
	modint& operator*=(const modint a){(x*=a.x)%=mod; return *this;}
	modint operator+(const modint a)const{modint res(*this); return res+=a;}
	modint operator-(const modint a)const{modint res(*this); return res-=a;}
	modint operator*(const modint a)const{modint res(*this); return res*=a;}
	modint pow(ll n)const{
		modint res=1,x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	modint inv()const{return pow(mod-2);}
};
 
// include mint here
using hshmint=modint<1000000007>;
using hshmint1=modint<998244353>;
using magicpair=pair<hshmint,hshmint1>;
hshmint _p=9973; // base
hshmint1 _p1=257; // base1
hshmint pw[3011],invpw[3011];
hshmint1 pw1[3011],invpw1[3011];
magicpair hsh[3011];
 
struct rollhsh{		
	rollhsh(string s=""){
		init(s);
	}
	void init(string s){
		// put this out in global if multiple testcases
		hsh[0]={1,1};
		pw[0]=1;
		pw1[0]=1;
		invpw[0]=1;
		invpw1[0]=1;		
		hshmint _invbase=_p.x;
		hshmint1 _invbase1=_p1.x;
		_invbase=_invbase.inv(); _invbase1=_invbase1.inv();
		rep(i,sz(s)){
			pw[i+1]=pw[i]*_p;
			pw1[i+1]=pw1[i]*_p1;
			invpw[i+1]=invpw[i]*_invbase;
			invpw1[i+1]=invpw1[i]*_invbase1;
			hshmint x=(int)(s[i]-'a'+1);
			hshmint1 x1=(int)(s[i]-'a'+1);
			hsh[i+1]={hsh[i].fi+pw[i]*x,hsh[i].se+pw1[i]*x1};
		}
	}
	pair<ll,ll> get(int l,int r){ 
		// from - to , inclusive
		hshmint _f=invpw[l]*(hsh[r+1].fi-hsh[l].fi);
		hshmint1 _s=invpw1[l]*(hsh[r+1].se-hsh[l].se);
		return {_f.x,_s.x};	
	}
};
 
ll f[3011][3011],g[3011][3011];
 
int nxt[3011][3011];
 
signed main(){
_3rfKJyA;
	int n;
	cin>>n;
	
	string s;
	cin>>s;
	
	ll c0,c1,c2;
	cin>>c0>>c1>>c2;
 
	using pll=pair<ll,ll>;
	rollhsh nahsh(s);
	vec(vec(pll)) qol(n,vec(pll)(n));
	vec(pll) rbts;
	rep(i,n){
		rng(j,i,n){
			nxt[i][j]=-1;
			qol[i][j]=nahsh.get(i,j);
			rbts.pb(qol[i][j]);
		}
	}
	sort(rbts.begin(),rbts.end());
	rbts.erase(unique(rbts.begin(),rbts.end()));
 
	vec(vi) ids(n,vi(n));
	rep(i,n){
		rng(j,i,n){
			ids[i][j]=lower_bound(rbts.begin(),rbts.end(),qol[i][j])-rbts.begin();
		}
	}

	vi lst(sz(rbts)+1,-1);
	for(int len=1;len<=n;len++){
		for(int i=n-1;i>=0;i--){
			if(i-len+1>=0){
				int id=ids[i-len+1][i];
				nxt[i-len+1][i]=lst[id];
			}else{
				break;
			}
			if(i+len-1<=n-1){
				int id=ids[i][i+len-1];
				lst[id]=i;
			}
		}
		// for(int i=0;i<n;i++){
		// 	if(i+len-1>=n) break;
		// 	int id=ids[i][i+len-1];
		// 	// assert(id!=-1);
		// 	while(ptr[id]+len-1<i){
		// 		if(qol[ptr[id]][ptr[id]+len-1].fi==qol[i][i+len-1].fi 
		// 			and qol[ptr[id]][ptr[id]+len-1].se==qol[i][i+len-1].se){
		// 			nxt[ptr[id]][ptr[id]+len-1]=i;
		// 		}
		// 		ptr[id]+=1;
		// 	}
		// }
	}
 
 	rep(i,n+1)rep(j,n+1)f[i][j]=8e18;
 
	for(int len=1;len<=n;len++){
		for(int i=0;i<n;i++){
			if(i+len-1>=n){
				break;
			}
			int j=i+len-1;
			if(len>1){
				g[i][j]=min({g[i][j],g[i+1][j],g[i][j-1]});
			}
 
			f[i][j]=min(f[i][j],1ll*len*c0+g[i][j]);
 
			ll cnt=1;
			int nj=i;
			while(1){
				g[i][nj+len-1]=min(g[i][nj+len-1],-1ll*len*c0*cnt+1ll*cnt*c2+f[i][i+len-1]+c1);
				nj=nxt[nj][nj+len-1];
				if(nj==-1) break;
				cnt+=1;
			}
		}
	}
	print(f[0][n-1]);
//	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 3 ms 392 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 918 ms 166464 KB Output is correct
4 Correct 1044 ms 192712 KB Output is correct
5 Correct 1255 ms 223100 KB Output is correct
6 Correct 1637 ms 258188 KB Output is correct
7 Correct 1978 ms 299220 KB Output is correct
8 Correct 2213 ms 299280 KB Output is correct
9 Correct 2070 ms 299348 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 1 ms 472 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 1 ms 852 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 3 ms 392 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 472 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 864 KB Output is correct
22 Correct 1 ms 852 KB Output is correct
23 Correct 1 ms 852 KB Output is correct
24 Correct 1 ms 852 KB Output is correct
25 Correct 1 ms 852 KB Output is correct
26 Correct 1 ms 852 KB Output is correct
27 Correct 1 ms 852 KB Output is correct
28 Correct 1 ms 828 KB Output is correct
29 Correct 1 ms 852 KB Output is correct
30 Correct 1 ms 852 KB Output is correct
31 Correct 1 ms 852 KB Output is correct
32 Correct 2 ms 852 KB Output is correct
33 Correct 1 ms 852 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 596 KB Output is correct
39 Correct 1 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 3 ms 392 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 472 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 864 KB Output is correct
22 Correct 1 ms 852 KB Output is correct
23 Correct 1 ms 852 KB Output is correct
24 Correct 1 ms 852 KB Output is correct
25 Correct 1 ms 852 KB Output is correct
26 Correct 1 ms 852 KB Output is correct
27 Correct 1 ms 852 KB Output is correct
28 Correct 1 ms 828 KB Output is correct
29 Correct 1 ms 852 KB Output is correct
30 Correct 1 ms 852 KB Output is correct
31 Correct 1 ms 852 KB Output is correct
32 Correct 2 ms 852 KB Output is correct
33 Correct 1 ms 852 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 596 KB Output is correct
39 Correct 1 ms 852 KB Output is correct
40 Correct 2 ms 1748 KB Output is correct
41 Correct 6 ms 4560 KB Output is correct
42 Correct 7 ms 4560 KB Output is correct
43 Correct 7 ms 4560 KB Output is correct
44 Correct 7 ms 4572 KB Output is correct
45 Correct 7 ms 4560 KB Output is correct
46 Correct 8 ms 4560 KB Output is correct
47 Correct 8 ms 4560 KB Output is correct
48 Correct 8 ms 4624 KB Output is correct
49 Correct 8 ms 4560 KB Output is correct
50 Correct 8 ms 4560 KB Output is correct
51 Correct 8 ms 4532 KB Output is correct
52 Correct 7 ms 4560 KB Output is correct
53 Correct 8 ms 4560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 3 ms 392 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 472 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 864 KB Output is correct
22 Correct 1 ms 852 KB Output is correct
23 Correct 1 ms 852 KB Output is correct
24 Correct 1 ms 852 KB Output is correct
25 Correct 1 ms 852 KB Output is correct
26 Correct 1 ms 852 KB Output is correct
27 Correct 1 ms 852 KB Output is correct
28 Correct 1 ms 828 KB Output is correct
29 Correct 1 ms 852 KB Output is correct
30 Correct 1 ms 852 KB Output is correct
31 Correct 1 ms 852 KB Output is correct
32 Correct 2 ms 852 KB Output is correct
33 Correct 1 ms 852 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 596 KB Output is correct
39 Correct 1 ms 852 KB Output is correct
40 Correct 2 ms 1748 KB Output is correct
41 Correct 6 ms 4560 KB Output is correct
42 Correct 7 ms 4560 KB Output is correct
43 Correct 7 ms 4560 KB Output is correct
44 Correct 7 ms 4572 KB Output is correct
45 Correct 7 ms 4560 KB Output is correct
46 Correct 8 ms 4560 KB Output is correct
47 Correct 8 ms 4560 KB Output is correct
48 Correct 8 ms 4624 KB Output is correct
49 Correct 8 ms 4560 KB Output is correct
50 Correct 8 ms 4560 KB Output is correct
51 Correct 8 ms 4532 KB Output is correct
52 Correct 7 ms 4560 KB Output is correct
53 Correct 8 ms 4560 KB Output is correct
54 Correct 37 ms 14520 KB Output is correct
55 Correct 187 ms 55592 KB Output is correct
56 Correct 240 ms 55580 KB Output is correct
57 Correct 210 ms 55688 KB Output is correct
58 Correct 219 ms 55676 KB Output is correct
59 Correct 229 ms 55588 KB Output is correct
60 Correct 218 ms 55576 KB Output is correct
61 Correct 206 ms 55604 KB Output is correct
62 Correct 199 ms 55676 KB Output is correct
63 Correct 205 ms 55576 KB Output is correct
64 Correct 198 ms 55584 KB Output is correct
65 Correct 231 ms 55692 KB Output is correct
66 Correct 215 ms 55716 KB Output is correct
67 Correct 204 ms 55616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 3 ms 392 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 472 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 918 ms 166464 KB Output is correct
14 Correct 1044 ms 192712 KB Output is correct
15 Correct 1255 ms 223100 KB Output is correct
16 Correct 1637 ms 258188 KB Output is correct
17 Correct 1978 ms 299220 KB Output is correct
18 Correct 2213 ms 299280 KB Output is correct
19 Correct 2070 ms 299348 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 0 ms 468 KB Output is correct
23 Correct 1 ms 472 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 1 ms 852 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 724 KB Output is correct
33 Correct 1 ms 596 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 596 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 864 KB Output is correct
39 Correct 1 ms 852 KB Output is correct
40 Correct 1 ms 852 KB Output is correct
41 Correct 1 ms 852 KB Output is correct
42 Correct 1 ms 852 KB Output is correct
43 Correct 1 ms 852 KB Output is correct
44 Correct 1 ms 852 KB Output is correct
45 Correct 1 ms 828 KB Output is correct
46 Correct 1 ms 852 KB Output is correct
47 Correct 1 ms 852 KB Output is correct
48 Correct 1 ms 852 KB Output is correct
49 Correct 2 ms 852 KB Output is correct
50 Correct 1 ms 852 KB Output is correct
51 Correct 1 ms 468 KB Output is correct
52 Correct 1 ms 468 KB Output is correct
53 Correct 1 ms 468 KB Output is correct
54 Correct 1 ms 468 KB Output is correct
55 Correct 1 ms 596 KB Output is correct
56 Correct 1 ms 852 KB Output is correct
57 Correct 2 ms 1748 KB Output is correct
58 Correct 6 ms 4560 KB Output is correct
59 Correct 7 ms 4560 KB Output is correct
60 Correct 7 ms 4560 KB Output is correct
61 Correct 7 ms 4572 KB Output is correct
62 Correct 7 ms 4560 KB Output is correct
63 Correct 8 ms 4560 KB Output is correct
64 Correct 8 ms 4560 KB Output is correct
65 Correct 8 ms 4624 KB Output is correct
66 Correct 8 ms 4560 KB Output is correct
67 Correct 8 ms 4560 KB Output is correct
68 Correct 8 ms 4532 KB Output is correct
69 Correct 7 ms 4560 KB Output is correct
70 Correct 8 ms 4560 KB Output is correct
71 Correct 37 ms 14520 KB Output is correct
72 Correct 187 ms 55592 KB Output is correct
73 Correct 240 ms 55580 KB Output is correct
74 Correct 210 ms 55688 KB Output is correct
75 Correct 219 ms 55676 KB Output is correct
76 Correct 229 ms 55588 KB Output is correct
77 Correct 218 ms 55576 KB Output is correct
78 Correct 206 ms 55604 KB Output is correct
79 Correct 199 ms 55676 KB Output is correct
80 Correct 205 ms 55576 KB Output is correct
81 Correct 198 ms 55584 KB Output is correct
82 Correct 231 ms 55692 KB Output is correct
83 Correct 215 ms 55716 KB Output is correct
84 Correct 204 ms 55616 KB Output is correct
85 Correct 700 ms 127160 KB Output is correct
86 Correct 2210 ms 299272 KB Output is correct
87 Correct 2237 ms 299328 KB Output is correct
88 Correct 2163 ms 299276 KB Output is correct
89 Correct 2302 ms 299280 KB Output is correct
90 Correct 1963 ms 299344 KB Output is correct
91 Correct 2138 ms 299400 KB Output is correct
92 Correct 2259 ms 299348 KB Output is correct
93 Correct 1983 ms 299276 KB Output is correct
94 Correct 1937 ms 299304 KB Output is correct
95 Correct 2223 ms 299412 KB Output is correct
96 Correct 2144 ms 299352 KB Output is correct
97 Correct 1959 ms 299328 KB Output is correct