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 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(){
for(int i = 2; i <= N; i++){
if(A[i] <= A[i - 1]) return false;
}
return true;
}
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]);
}
}
cout << ans << '\n';
}
}
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;
}
# | 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... |