#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(), v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define INF (ll)1e9
#define oo (ll)1e18
#define MOD (ll)(1e9 + 7)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 2e5 + 10;
int N;
int A[MAX], B[MAX], lef[MAX], rig[MAX];
void input(){
cin >> N;
for(int i = 1; i <= N; i++) cin >> A[i];
for(int i = 1; i <= N; i++) cin >> B[i];
}
void compress(){
vector<int> tmp;
for(int i = 1; i <= N; i++) tmp.push_back(A[i]);
for(int i = 1; i <= N; i++) tmp.push_back(B[i]);
cps(tmp);
for(int i = 1; i <= N; i++) A[i] = lower_bound(ALL(tmp), A[i]) - tmp.begin();
for(int i = 1; i <= N; i++) B[i] = lower_bound(ALL(tmp), B[i]) - tmp.begin();
}
void setup(){
for(int i = 1; i <= N; i++){
lef[i] = i - 1;
while(lef[i] > 0 && A[i] >= A[lef[i]]) lef[i] = lef[lef[i]];
}
for(int i = N; i >= 1; i--){
rig[i] = i + 1;
while(rig[i] <= N && A[i] >= A[rig[i]]) rig[i] = rig[rig[i]];
}
}
namespace subtask2{
void solve(){
}
}
namespace subtask3{
bool check(){
return is_sorted(A + 1, A + N + 1);
}
const int MAX = 5003;
int f[MAX][MAX];
//f[i][j]: ap dung nhung thay doi tu A[1] -> A[i] cho nhung thang tu 1 -> j
void solve(){
int ans = 0;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= i; j++){
f[i][j] = max(f[i - 1][j], f[i][j - 1]);
if(A[i] == B[j]){
maximize(f[i][j], f[i][j - 1] + 1);
}
maximize(ans, f[i][j]);
}
}
}
}
namespace seg{
int it[MAX * 4];
int n;
void init(int _n = 0){
n = _n;
memset(it, 0, n * 4 * sizeof(int));
}
void upd(int p, int c){
int i = 1, l = 1, r = n;
while(l < r){
int m = (l + r) >> 1;
if(p <= m){
i = i * 2;
r = m;
}
else{
i = i * 2 + 1;
l = m + 1;
}
}
maximize(it[i], c);
while(i > 1){
i >>= 1;
it[i] = max(it[i * 2], it[i * 2 + 1]);
}
}
int get(int u, int v, int i = 1, int l = 1, int r = n){
if(r < u || v < l) return 0;
if(u <= l && r <= v) return it[i];
int m = (l + r) >> 1;
return max(get(u, v, i * 2, l, m), get(u, v, i * 2 + 1, m + 1, r));
}
}
namespace subtask4{
bool check(){
vector<int> tmp;
for(int i = 1; i <= N; i++) tmp.push_back(A[i]);
cps(tmp);
return sz(tmp) == N;
}
int pos[MAX];
void solve(){
seg::init(N);
for(int i = 1; i <= N; i++){
pos[A[i]] = i;
}
for(int i = 1; i <= N; i++){
int p = pos[B[i]];
if(p == 0) continue;
if(lef[p] < i && i < rig[p]){
seg::upd(p, seg::get(1, p) + 1);
}
}
cout << seg::it[1] << '\n';
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("creature.inp", "r", stdin);
// freopen("creature.out", "w", stdout);
input();
compress();
setup();
if(subtask3::check()) return subtask3::solve(), 0;
if(subtask4::check()) return subtask4::solve(), 0;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6480 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4600 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6736 KB |
Output is correct |
2 |
Correct |
23 ms |
7828 KB |
Output is correct |
3 |
Correct |
53 ms |
10676 KB |
Output is correct |
4 |
Correct |
56 ms |
11444 KB |
Output is correct |
5 |
Correct |
64 ms |
11444 KB |
Output is correct |
6 |
Correct |
71 ms |
10676 KB |
Output is correct |
7 |
Correct |
59 ms |
11496 KB |
Output is correct |
8 |
Correct |
52 ms |
10676 KB |
Output is correct |
9 |
Correct |
62 ms |
10716 KB |
Output is correct |
10 |
Correct |
54 ms |
10676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6480 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4600 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6480 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4600 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |