#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define ll long long
#define ar array
#define f first
#define s second
#define mpr make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for(auto i = a; i < b; i++)
#define FORD(i, a, b) for(auto i = a - 1; i >= b; i--)
#define trav(x, v) for(auto x : v)
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fileio freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
// --------------- SOLUTION --------------- //
const int N = 1e5+5;
int n;
int a[N], b[N];
int t[4 * N];
int next_max[N], prev_max[N];
int dp[5001][5001];
map <int, int> mp;
void pre() {
stack<int> s;
FOR(i, 1, n + 1) {
while (!s.empty() && a[s.top()] <= a[i])
s.pop();
if (s.empty())
next_max[i] = -1;
else
next_max[i] = s.top();
s.push(i);
}
while (!s.empty())
s.pop();
FORD(i, n + 1, 1) {
while (!s.empty() && a[s.top()] <= a[i])
s.pop();
if (s.empty())
prev_max[i] = n + 1;
else
prev_max[i] = s.top();
s.push(i);
}
}
void ubd(int tl, int tr, int x, int y, int pos) {
if (tl == tr) {
t[pos] = y;
return;
}
int m = (tl + tr) / 2;
if (x <= m)
ubd(tl, m, x, y, pos * 2);
else
ubd(m + 1, tr, x, y, pos * 2 + 1);
t[pos] = max(t[pos * 2], t[pos * 2 + 1]);
}
int qry(int tl, int tr, int l, int r, int pos) {
if (l > r)
return 0;
if (tl == l && tr == r)
return t[pos];
int m = (tl + tr) / 2;
return max(qry(tl, m, l, min(m, r), pos * 2),
qry(m + 1, tr, max(m + 1, l), r, pos * 2 + 1));
}
int segTreeSolve() {
FOR(i, 1, n + 1) {
mp[a[i]] = i;
}
FOR(i, 1, n + 1) {
if (mp.find(b[i]) == mp.end()) continue;
if (next_max[mp[b[i]]] < i && prev_max[mp[b[i]]] > i) ubd(1, n, mp[b[i]], qry(1, n, 1, mp[b[i]], 1) + 1, 1);
}
return t[1];
}
int less5000() {
int ans = 0;
FOR(i, 1, n + 1) {
FOR(j, 1, n + 1) {
if (a[i] == b[j] && next_max[i] < j && j < prev_max[i]) {
dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + 1);
}
dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]);
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
}
}
FOR(i, 1, n + 2)
FOR(j, 1, n + 2)
ans = max(ans, dp[i][j]);
return ans;
}
void solve() {
cin >> n;
FOR(i, 1, n + 1) {
cin >> a[i];
}
FOR(i, 1, n + 1) {
cin >> b[i];
}
pre();
if (n <= 5000) {
cout << less5000() << "\n";
return;
}
cout << segTreeSolve() << "\n";
}
int main() {
fastio;
// fileio;
int tests = 1;
// cin >> tests;
while (tests--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
8268 KB |
Output is correct |
2 |
Correct |
11 ms |
972 KB |
Output is correct |
3 |
Correct |
79 ms |
6976 KB |
Output is correct |
4 |
Incorrect |
17 ms |
2252 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
3276 KB |
Output is correct |
3 |
Correct |
35 ms |
23956 KB |
Output is correct |
4 |
Correct |
171 ms |
94312 KB |
Output is correct |
5 |
Incorrect |
191 ms |
98184 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
676 KB |
Output is correct |
2 |
Correct |
45 ms |
2960 KB |
Output is correct |
3 |
Correct |
130 ms |
6804 KB |
Output is correct |
4 |
Correct |
139 ms |
6880 KB |
Output is correct |
5 |
Correct |
170 ms |
7508 KB |
Output is correct |
6 |
Correct |
176 ms |
7600 KB |
Output is correct |
7 |
Correct |
132 ms |
7588 KB |
Output is correct |
8 |
Correct |
106 ms |
7176 KB |
Output is correct |
9 |
Correct |
150 ms |
7572 KB |
Output is correct |
10 |
Correct |
109 ms |
7604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
1216 KB |
Output is correct |
11 |
Correct |
1 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
4 ms |
3276 KB |
Output is correct |
9 |
Correct |
35 ms |
23956 KB |
Output is correct |
10 |
Correct |
171 ms |
94312 KB |
Output is correct |
11 |
Incorrect |
191 ms |
98184 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |