Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <cstring>
- #include <vector>
- #include <set>
- #include <stack>
- using namespace std;
- typedef long long ll;
- const int maxn = 1e5 + 10;
- int n;
- int a[maxn], b[maxn];
- int L[maxn], R[maxn];
- void solve_subtask2() {
- 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 and a[j] <= x) {
- j--;
- res++;
- }
- j = i + 1;
- while(j < n and a[j] <= x) {
- res++;
- j++;
- }
- i = j - 1;
- }
- }
- cout << res << endl;
- }
- int dp[5005][5005];
- int rec(int i, int j) {
- if(i < 0 or j < 0) {
- return 0;
- }
- if(dp[i][j] != -1) {
- return dp[i][j];
- }
- int res = 0;
- if(a[i] == b[j] and L[i] < j and R[i] > j) {
- res = max(res, rec(i, j - 1) + 1);
- }
- res = max(res, rec(i - 1, j));
- res = max(res, rec(i, j - 1));
- return dp[i][j] = res;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin >> n;
- set<int> st;
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- }
- for(int i = 0; i < n; i++) {
- cin >> b[i];
- st.insert(b[i]);
- }
- if((int) st.size() == 1) {
- solve_subtask2();
- return 0;
- }
- else if(n <= 5005){
- for(int i = 0; i < n; i++) {
- int idx1 = -1;
- for(int j = 0; j < i; j++) {
- if(a[i] < a[j]) {
- idx1 = j;
- }
- }
- int idx2 = n;
- for(int j = i + 1; j < n; j++) {
- if(a[i] < a[j]) {
- idx2 = j;
- break;
- }
- }
- L[i] = idx1;
- R[i] = idx2;
- }
- memset(dp, -1, sizeof dp);
- cout << rec(n - 1, n - 1) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement