#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define SZ(x) ((int) (x).size())
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define debug(x) cout << "[" << #x << " = " << (x) << "]" << endl
#define left __left
#define right __right
#define prev __prev
#define next __next
#define fi first
#define se second
template <class X, class Y>
bool maximize(X &x, Y y) {
if (x < y) return x = y, true;
else return false;
}
template <class X, class Y>
bool minimize(X &x, Y y) {
if (x > y) return x = y, true;
else return false;
}
#define MAX_N 100'100
int a[MAX_N + 2], b[MAX_N + 2], lef[MAX_N + 2], rig[MAX_N + 2];
int n;
void prepare() {
stack <int> st;
FOR(i, 1, n) lef[i] = 0, rig[i] = n + 1;
FOR(i, 1, n) {
while (!st.empty() && a[st.top()] < a[i]) st.pop();
if (!st.empty()) lef[i] = st.top() + 1;
st.push(i);
}
while (!st.empty()) st.pop();
FORD(i, n, 1) {
while (!st.empty() && a[st.top()] < a[i]) st.pop();
if (!st.empty()) rig[i] = st.top() - 1;
st.push(i);
}
}
namespace subtask1356 {
bool check() {
return (n <= 5000);
}
int dp[5050][5050];
void solve() {
memset(dp, 0, sizeof(dp));
int ans = 0;
FOR(j, 1, n) FOR(i, 1, n) {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
if (a[i] == b[j] && lef[i] <= j && j <= rig[i]) {
dp[i][j] = dp[i][j - 1] + 1;
}
maximize(ans, dp[i][j]);
}
cout << ans;
}
};
int main() {
ios_base::sync_with_stdio(false);cin.tie(nullptr);
#define task "test"
if (fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n;
FOR(i, 1, n) cin >> a[i];
FOR(i, 1, n) cin >> b[i];
prepare();
if (subtask1356 :: check()) return subtask1356 :: solve(), 0;
return 0;
}
/* Discipline - Calm */
Compilation message (stderr)
exam.cpp: In function 'int main()':
exam.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
exam.cpp:84:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |