This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define rall(x) x.rbegin(), x.rend()
#define each(x, v) for(auto &x : v)
#define mp make_pair
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
void setIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n;
vector<int> a, b;
vector<int> L, R;
vector<vector<int> > dp;
void solve_2() {
int curr = b[0];
int ans = 0;
for(int i=0; i<n; i++) {
if(a[i] != curr) continue;
ans++;
int j = i-1;
while(j >= 0 && a[j] <= curr) j--, ans++;
j = i+1;
while(j < n && a[j] <= curr) j++, ans++;
i = j-1;
}
cout << ans << '\n';
}
int32_t main() {
setIO();
cin >> n;
a.resize(n);
b.resize(n);
L.resize(n);
R.resize(n);
vector<int> temp(n+2, 0);
set<int> diff;
int id = 1;
for(int &x : a) cin >> x, temp[id++] = x;
for(int &x : b) cin >> x, diff.insert(x);
if(sz(diff) == 1) {
solve_2();
return 0;
}
temp[0] = INT_MAX;
temp[n+1] = temp[0];
stack<int> st;
st.push(0);
for(int i=1; i<=n; i++) {
while(temp[st.top()] <= temp[i])
st.pop();
L[i-1] = st.top() - 1;
st.push(i);
}
while(!st.empty()) st.pop();
st.push(n+1);
for(int i=n; i>=1; i--) {
while(temp[st.top()] <= temp[i])
st.pop();
R[i-1] = st.top() - 1;
st.push(i);
}
if(n <= 5004) {
vector<vector<ll> > dp(n+5, vector<ll>(n+5, 0));
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
if(a[i-1] == b[j-1] && L[i-1] < j-1 && j-1 < R[i-1])
dp[i][j] = dp[i][j-1] + 1;
dp[i][j] = max(dp[i][j], dp[i-1][j]);
dp[i][j] = max(dp[i][j], dp[i][j-1]);
}
}
cout << dp[n][n] << '\n';
return 0;
}
return 0;
}
# | 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... |