#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 4;
const int INF = 1e9;
const int N = 5005;
int n, a[maxn], b[maxn], L[maxn], R[maxn];
set<int> mySet;
void Sub2() {
int res = 0, pre = 0;
for (int i = 1; i <= n; i++)
if (a[i] == b[i]) {
res += R[i] - max(pre, L[i]) - 1;
pre = R[i];
i = R[i] - 1;
}
cout << res;
}
struct seg_tree {
vector<int> t;
seg_tree() {
t.resize(4 * maxn);
}
void update(int pos, int val, int v = 1, int l = 1, int r = maxn - 1) {
if (l == r) {
t[v] = val;
return;
}
int m = (l + r) / 2;
if (pos <= m) update(pos, val, 2 * v, l, m);
else update(pos, val, 2 * v + 1, m + 1, r);
t[v] = max(t[2 * v], t[2 * v + 1]);
}
int get(int l, int r, int v = 1, int tl = 1, int tr = maxn - 1) {
if (tl > r || tr < l) return 0;
if (tl >= l && tr <= r) return t[v];
int m = (tl + tr) / 2;
int L = get(l, r, 2 * v, tl, m);
int R = get(l, r, 2 * v + 1, m + 1, tr);
return max(L, R);
}
} IT;
map<int,int> mp;
void Sub4() {
for (int i = 1; i <= n; i++)
mp[a[i]] = i;
for (int i = 1; i <= n; i++) {
auto it = mp.find(b[i]);
if (it == mp.end()) continue;
int idx = it->se;
if (L[idx] < i && R[idx] > i)
IT.update(idx, IT.get(1, idx) + 1);
}
cout << IT.t[1];
}
int dp[N][N];
void Sub6() {
int res = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (a[i] == b[j] && L[i] < j && j < R[i])
dp[i][j] = dp[i][j - 1] + 1;
dp[i][j] = max({dp[i][j], dp[i][j - 1], dp[i - 1][j]});
res = max(res, dp[i][j]);
}
cout << res << '\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++) {
cin >> b[i];
mySet.insert(b[i]);
}
stack<int> st;
st.push(0);
a[0] = a[n + 1] = INF + 1;
for (int i = 1; i <= n; i++) {
while (a[st.top()] <= a[i])
st.pop();
L[i] = st.top();
st.push(i);
}
stack<int>().swap(st);
st.push(n + 1);
for (int i = n; i >= 1; i--) {
while (a[st.top()] <= a[i])
st.pop();
R[i] = st.top();
st.push(i);
}
if (mySet.size() == 1) Sub2();
else if (n > 5000) Sub4();
else {
Sub6();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
5 ms |
2156 KB |
Output is correct |
3 |
Correct |
14 ms |
3668 KB |
Output is correct |
4 |
Correct |
11 ms |
3404 KB |
Output is correct |
5 |
Correct |
28 ms |
3360 KB |
Output is correct |
6 |
Correct |
13 ms |
3412 KB |
Output is correct |
7 |
Correct |
20 ms |
3412 KB |
Output is correct |
8 |
Correct |
29 ms |
3460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
4 ms |
4820 KB |
Output is correct |
3 |
Correct |
37 ms |
25664 KB |
Output is correct |
4 |
Correct |
150 ms |
96048 KB |
Output is correct |
5 |
Correct |
159 ms |
100000 KB |
Output is correct |
6 |
Correct |
159 ms |
100060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2260 KB |
Output is correct |
2 |
Correct |
36 ms |
5492 KB |
Output is correct |
3 |
Correct |
99 ms |
11088 KB |
Output is correct |
4 |
Correct |
92 ms |
11084 KB |
Output is correct |
5 |
Correct |
98 ms |
10380 KB |
Output is correct |
6 |
Correct |
102 ms |
10548 KB |
Output is correct |
7 |
Correct |
89 ms |
10572 KB |
Output is correct |
8 |
Correct |
98 ms |
10420 KB |
Output is correct |
9 |
Correct |
98 ms |
11192 KB |
Output is correct |
10 |
Correct |
69 ms |
10956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
2004 KB |
Output is correct |
9 |
Correct |
1 ms |
2260 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2900 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
4 ms |
4820 KB |
Output is correct |
9 |
Correct |
37 ms |
25664 KB |
Output is correct |
10 |
Correct |
150 ms |
96048 KB |
Output is correct |
11 |
Correct |
159 ms |
100000 KB |
Output is correct |
12 |
Correct |
159 ms |
100060 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
1 ms |
2004 KB |
Output is correct |
15 |
Correct |
1 ms |
2260 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2900 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
10 ms |
9812 KB |
Output is correct |
23 |
Correct |
2 ms |
2004 KB |
Output is correct |
24 |
Correct |
176 ms |
99868 KB |
Output is correct |
25 |
Correct |
167 ms |
99884 KB |
Output is correct |
26 |
Correct |
161 ms |
99904 KB |
Output is correct |
27 |
Correct |
162 ms |
99876 KB |
Output is correct |
28 |
Correct |
171 ms |
100028 KB |
Output is correct |
29 |
Correct |
165 ms |
100040 KB |
Output is correct |
30 |
Correct |
161 ms |
100044 KB |
Output is correct |
31 |
Correct |
158 ms |
100040 KB |
Output is correct |
32 |
Correct |
160 ms |
99948 KB |
Output is correct |