Submission #815543

# Submission time Handle Problem Language Result Execution time Memory
815543 2023-08-08T16:45:49 Z ZeroCool Exam (eJOI20_exam) C++14
100 / 100
331 ms 221908 KB
#include <bits/stdc++.h>  
#define INF 1e18
#define LL long long
#define int long long
using namespace std;
	
const int mod = 1e9 + 7;
const int inf = 1e18;
const int mxn = 1e5 +5;

struct FenwickTree{
	vector<int> bit;
	int n;
	FenwickTree(int _n){
		n = _n + 69;
		bit.resize(n,0);
	}
 
	void update(int pos,int val){
		for(pos++;pos<n;pos += pos & -pos)bit[pos] = max(bit[pos],val);
	}
 
	int query(int pos){
		int ans = 0;
		for(pos++;pos;pos -= pos & -pos)ans =  max(bit[pos],ans);
		return ans;
	}
};

int n;
	
int A[mxn];
int B[mxn];
int l[mxn];
int r[mxn];



void two_pointers(){
	int x = B[0];
	int res = 0;
	for(int i = 0;i<n;i++) {
		if(A[i] == x) {
			res++;
			int j = i - 1;
			while(j >= 0 && A[j] <= x) {
				j--;
				res++;
			}
			j = i + 1;
			while(j < n && A[j] <= x) {
				res++;
				j++;
			}
			i = j - 1;
		}
	}
	cout<<res<<endl;
	
}
	
int dp[5005][5005];
bool vis[5005][5005];
	
	
int f(int i,int j){
	if(i == -1 ||j == -1)return 0;
	if(vis[i][j])return dp[i][j];
	int ans = 0;
	if(A[i] == B[j] && l[i] < j && r[i] > j){
		ans = max(ans, f(i,j-1) + 1);
	}
	
	ans = max(ans , f(i-1,j));
	ans = max(ans, f(i,j-1));
	
	vis[i][j] = true;
	dp[i][j] = ans;
	return ans;
}
		
void solve(){
	cin>>n;
	
	set<int>s;
	int c[n+5];
	for(int i = 0;i<n;i++)cin>>A[i];
	for(int i = 0;i<n;i++){
		cin>>B[i];
		s.insert(B[i]);
	}
	
	for(int i = 0;i<n;i++)c[i+1] = A[i];
	
	c[0] = inf;
	c[n+1] = inf;
	stack<int> st;
	st.push(0);
	for(int i = 1;i<=n;i++){
		while(st.size() && (c[st.top()] <= c[i]))st.pop();
		l[i-1] = st.top() - 1;
		st.push(i);
	} 
	//st = stack<int>();
	while(st.size())st.pop();
	st.push(n+1);
	for(int i = n;i;i--){
		while(st.size() && (c[st.top()] <= c[i]))st.pop();
		//cout<<"tukaa"<<endl;
		r[i-1] = st.top() - 1;
		//if(r[i-1] >= n)r[i-1] = -1; 
		st.push(i);
	} 
	bool sub2 = true;
	for(int i = 1;i<n;i++){
		if(B[i] != B[0]){
			sub2 = false;
			break;
		}
	}
	
	if(sub2){
		two_pointers();
		return;
	}
	
	if(n <= 5000){
		cout<<f(n-1,n-1)<<endl;
		return;
	}

	map<int,int> mp;

	for(int i  = 0;i<n;i++){
		mp[A[i]] = i;
	}
	FenwickTree fwt(n);
	for(int i  = 0;i<n;i++){
		auto it = mp.find(B[i]);

		if(it != mp.end()){
			int idx = it->second;

			if(l[idx] < i and i < r[idx]) {
				int dp = fwt.query(idx);
				fwt.update(idx, dp + 1);
			}
		}
	}

	cout<<fwt.query(n-1)<<endl;
}
		
int32_t main() {  
	#ifdef ONLINE_JUDGE
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	#endif
	solve();
}   
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 11 ms 980 KB Output is correct
3 Correct 36 ms 4496 KB Output is correct
4 Correct 24 ms 4184 KB Output is correct
5 Correct 64 ms 4188 KB Output is correct
6 Correct 25 ms 4172 KB Output is correct
7 Correct 28 ms 4116 KB Output is correct
8 Correct 77 ms 4180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 4 ms 6612 KB Output is correct
3 Correct 41 ms 49932 KB Output is correct
4 Correct 258 ms 212880 KB Output is correct
5 Correct 234 ms 221748 KB Output is correct
6 Correct 220 ms 221908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 980 KB Output is correct
2 Correct 45 ms 5892 KB Output is correct
3 Correct 106 ms 15264 KB Output is correct
4 Correct 121 ms 16052 KB Output is correct
5 Correct 134 ms 15564 KB Output is correct
6 Correct 95 ms 14668 KB Output is correct
7 Correct 116 ms 15884 KB Output is correct
8 Correct 102 ms 14672 KB Output is correct
9 Correct 117 ms 15504 KB Output is correct
10 Correct 90 ms 15240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 1236 KB Output is correct
10 Correct 1 ms 2260 KB Output is correct
11 Correct 1 ms 2260 KB Output is correct
12 Correct 1 ms 2260 KB Output is correct
13 Correct 1 ms 2260 KB Output is correct
14 Correct 2 ms 2260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 4 ms 6612 KB Output is correct
9 Correct 41 ms 49932 KB Output is correct
10 Correct 258 ms 212880 KB Output is correct
11 Correct 234 ms 221748 KB Output is correct
12 Correct 220 ms 221908 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 1236 KB Output is correct
16 Correct 1 ms 2260 KB Output is correct
17 Correct 1 ms 2260 KB Output is correct
18 Correct 1 ms 2260 KB Output is correct
19 Correct 1 ms 2260 KB Output is correct
20 Correct 2 ms 2260 KB Output is correct
21 Correct 1 ms 2260 KB Output is correct
22 Correct 13 ms 17296 KB Output is correct
23 Correct 3 ms 468 KB Output is correct
24 Correct 331 ms 221632 KB Output is correct
25 Correct 277 ms 221592 KB Output is correct
26 Correct 217 ms 221664 KB Output is correct
27 Correct 219 ms 221580 KB Output is correct
28 Correct 218 ms 221720 KB Output is correct
29 Correct 276 ms 221772 KB Output is correct
30 Correct 209 ms 221780 KB Output is correct
31 Correct 247 ms 221684 KB Output is correct
32 Correct 265 ms 221680 KB Output is correct