Submission #464308

# Submission time Handle Problem Language Result Execution time Memory
464308 2021-08-12T20:44:03 Z AdamGS Exam (eJOI20_exam) C++14
100 / 100
229 ms 165316 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=1e5+7;
int a[LIM], b[LIM], dp[5007][5007], ma[5007][5007], n;
int tr[4*LIM], tr2[4*LIM], N=1;
map<int,int>mp;
void subtask2() {
	rep(i, n-1) if(a[i+1]==b[1] && a[i+1]>=a[i+2]) a[i+2]=a[i+1];
	for(int i=n-1; i; --i) if(a[i+1]==b[1] && a[i+1]>=a[i]) a[i]=a[i+1];
	int ans=0;
	rep(i, n) if(a[i+1]==b[i+1]) ++ans;
	cout << ans << '\n';
}
int cnt(int v) {
	v+=N;
	int ans=tr[v];
	while(v) {
		if(v%2==1) ans=max(ans, tr[v-1]);
		v/=2;
	}
	return ans;
}
void upd(int v, int x) {
	v+=N;
	while(v) {
		tr[v]=max(tr[v], x);
		v/=2;
	}
}
int cnt2(int l, int r) {
	l+=N; r+=N;
	int ans=max(tr2[l], tr2[r]);
	while(l/2!=r/2) {
		if(l%2==0) ans=max(ans, tr2[l+1]);
		if(r%2==1) ans=max(ans, tr2[r-1]);
		l/=2; r/=2;
	}
	return ans;
}
void subtask4() {
	while(N<=n) N*=2;
	rep(i, n) tr2[N+i+1]=a[i+1];
	for(int i=N-1; i; --i) tr2[i]=max(tr2[2*i], tr2[2*i+1]);
	rep(i, n) mp[a[i+1]]=i+1;
	rep(i, n) {
		if(!mp[b[i+1]] || cnt2(min(mp[b[i+1]], i+1), max(mp[b[i+1]], i+1))>b[i+1]) continue;
		upd(mp[b[i+1]], cnt(mp[b[i+1]])+1);
	}
	cout << cnt(n) << '\n';
}
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	rep(i, n) cin >> a[i+1];
	rep(i, n) cin >> b[i+1];
	bool ok2=true;
	rep(i, n-1) if(b[i+1]!=b[i+2]) ok2=false;
	if(ok2) {
		subtask2();
		return 0;
	}
	if(n>5000) {
		subtask4();
		return 0;
	}
	rep(i, n) {
		for(int j=i; j<n; ++j) {
			ma[i+1][j+1]=max(ma[i+1][j], a[j+1]);
		}
	}
	rep(i, n) rep(j, n) {
		dp[i+1][j+1]=max(dp[i+1][j], dp[i][j+1]);
		if(a[i+1]==b[j+1] && ma[min(i+1, j+1)][max(i+1, j+1)]==a[i+1]) {
			dp[i+1][j+1]=max(dp[i+1][j+1], dp[i+1][j]+1);
		}
	}
	cout << dp[n][n] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 7 ms 716 KB Output is correct
3 Correct 18 ms 1204 KB Output is correct
4 Correct 15 ms 1176 KB Output is correct
5 Correct 27 ms 1348 KB Output is correct
6 Correct 16 ms 1120 KB Output is correct
7 Correct 16 ms 1188 KB Output is correct
8 Correct 25 ms 1296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 6 ms 5836 KB Output is correct
3 Correct 37 ms 39876 KB Output is correct
4 Correct 172 ms 157344 KB Output is correct
5 Correct 174 ms 165212 KB Output is correct
6 Correct 174 ms 165316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 716 KB Output is correct
2 Correct 43 ms 3392 KB Output is correct
3 Correct 117 ms 8008 KB Output is correct
4 Correct 138 ms 8924 KB Output is correct
5 Correct 170 ms 9412 KB Output is correct
6 Correct 146 ms 8668 KB Output is correct
7 Correct 151 ms 9340 KB Output is correct
8 Correct 126 ms 8412 KB Output is correct
9 Correct 136 ms 8748 KB Output is correct
10 Correct 96 ms 8644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 1100 KB Output is correct
10 Correct 2 ms 2124 KB Output is correct
11 Correct 2 ms 2124 KB Output is correct
12 Correct 2 ms 2124 KB Output is correct
13 Correct 2 ms 2124 KB Output is correct
14 Correct 2 ms 2124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 6 ms 5836 KB Output is correct
9 Correct 37 ms 39876 KB Output is correct
10 Correct 172 ms 157344 KB Output is correct
11 Correct 174 ms 165212 KB Output is correct
12 Correct 174 ms 165316 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 1100 KB Output is correct
16 Correct 2 ms 2124 KB Output is correct
17 Correct 2 ms 2124 KB Output is correct
18 Correct 2 ms 2124 KB Output is correct
19 Correct 2 ms 2124 KB Output is correct
20 Correct 2 ms 2124 KB Output is correct
21 Correct 2 ms 2124 KB Output is correct
22 Correct 12 ms 14156 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 229 ms 165176 KB Output is correct
25 Correct 196 ms 165204 KB Output is correct
26 Correct 187 ms 165196 KB Output is correct
27 Correct 192 ms 165188 KB Output is correct
28 Correct 175 ms 165188 KB Output is correct
29 Correct 187 ms 165164 KB Output is correct
30 Correct 174 ms 165284 KB Output is correct
31 Correct 175 ms 165256 KB Output is correct
32 Correct 175 ms 165220 KB Output is correct