#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
#define int long long
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';
}
int solve_dp(int i, int j) {
if(i < 0 || j < 0) return 0;
if(dp[i][j] != -1) return dp[i][j];
int ans = 0;
ans = max(ans, solve_dp(i-1, j));
ans = max(ans, solve_dp(i, j-1));
if(a[i] == b[j] && L[i] < j && j < R[i])
ans = max(ans, solve_dp(i, j-1) + 1);
return dp[i][j] = ans;
}
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=0; i<n; i++) {
while(temp[st.top()] <= temp[i+1])
st.pop();
L[i] = st.top() - 1;
st.push(i+1);
}
while(!st.empty()) st.pop();
st.push(n+1);
for(int i=n-1; i>=0; i--) {
while(temp[st.top()] <= temp[i+1])
st.pop();
R[i] = st.top() - 1;
st.push(n+1);
}
if(n <= 5004) {
dp.resize(5005, vector<int>(5005, -1));
cout << solve_dp(n-1, n-1) << '\n';
return 0;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
196536 KB |
Output is correct |
2 |
Correct |
63 ms |
196556 KB |
Output is correct |
3 |
Correct |
62 ms |
196532 KB |
Output is correct |
4 |
Correct |
61 ms |
196556 KB |
Output is correct |
5 |
Correct |
62 ms |
196484 KB |
Output is correct |
6 |
Correct |
64 ms |
196472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
1108 KB |
Output is correct |
3 |
Correct |
11 ms |
3796 KB |
Output is correct |
4 |
Correct |
9 ms |
4256 KB |
Output is correct |
5 |
Correct |
16 ms |
4180 KB |
Output is correct |
6 |
Correct |
9 ms |
4180 KB |
Output is correct |
7 |
Correct |
10 ms |
4180 KB |
Output is correct |
8 |
Correct |
17 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
62 ms |
196472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
255 ms |
197592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
196536 KB |
Output is correct |
2 |
Correct |
63 ms |
196556 KB |
Output is correct |
3 |
Correct |
62 ms |
196532 KB |
Output is correct |
4 |
Correct |
61 ms |
196556 KB |
Output is correct |
5 |
Correct |
62 ms |
196484 KB |
Output is correct |
6 |
Correct |
64 ms |
196472 KB |
Output is correct |
7 |
Incorrect |
62 ms |
196552 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
196536 KB |
Output is correct |
2 |
Correct |
63 ms |
196556 KB |
Output is correct |
3 |
Correct |
62 ms |
196532 KB |
Output is correct |
4 |
Correct |
61 ms |
196556 KB |
Output is correct |
5 |
Correct |
62 ms |
196484 KB |
Output is correct |
6 |
Correct |
64 ms |
196472 KB |
Output is correct |
7 |
Incorrect |
62 ms |
196472 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |