#include "bits/stdc++.h"
using namespace std;
//~ #define int long long
void solve1(int n, vector<int>& a, vector<int>& b){
vector<int> dp(n);
int res = 0;
for(int i=0;i<n;i++){
int s = i, e = i;
while(s + 1 < n && a[s + 1] <= a[i]) s++;
while(e && a[e - 1] <= a[i]) e--;
vector<int> cnt(n);
for(int j=0;j<n;j++){
if(j) cnt[j] = cnt[j-1];
cnt[j] += (b[j] == a[i]);
}
vector<int> pref(n, -1e9);
for(int j=e;j<n;j++){
if(j) pref[j] = pref[j-1];
pref[j] = max(pref[j], (j ? - cnt[j-1] + dp[j-1] : 0));
}
for(int r=s;r>=e;r--){
dp[r] = max(dp[r], cnt[r] + pref[r]);
}
}
for(int i=0;i<n;i++) res = max(res, dp[i]);
cout<<res<<"\n";
}
struct ST{
vector<int> tree;
int n;
ST(int n) :n(n) {
tree.resize(4 * n);
}
void sett(int i, int v, int lx, int rx, int x){
if(lx == rx){
tree[x] = v;
return;
}
int m = (lx + rx)>>1;
if(i <= m) sett(i, v, lx, m, x<<1);
else sett(i, v, m+1, rx, x<<1|1);
tree[x] = max(tree[x<<1], tree[x<<1|1]);
}
void sett(int i, int v){
sett(i, v, 0, n, 1);
}
int get(int l, int r, int lx, int rx, int x){
if(lx > r || rx < l) return 0;
if(lx >= l && rx <= r) return tree[x];
int m = (lx + rx)>>1;
return max(get(l, r, lx, m, x<<1), get(l, r, m+1, rx, x<<1|1));
}
int get(int l, int r){
if(r < l) return 0;
return get(l, r, 0, n, 1);
}
};
void solve2(int n, vector<int>& a, vector<int>& b){
int mn = 1e9, mx = 0;
for(auto x : b) mn = min(mn, x), mx = max(mx, x);
if(mx == mn){
int last = 0, cnt = 0, res = 0;
for(int i=0;i<n;i++){
if(a[i] > b[0]){
if(cnt){
res += (i - last);
}
cnt = 0, last = i + 1;
} else {
cnt |= (a[i] == b[i]);
}
} if(cnt){
res += (n - last);
}
cout<<res<<"\n";
return;
}
//~ I DONT KNOW HOW TO SOLVE
ST tree(n), lazy(n);
map<int, int> pp;
for(int i=0;i<n;i++) pp[a[i]] = i, lazy.sett(i, a[i]);
for(int i=0;i<n;i++){
if(pp.count(b[i])){
int l = i, r = pp[b[i]];
if(lazy.get(min(l, r), max(l, r)) > b[i]) continue;
tree.sett(r, tree.get(0, r) + 1);
}
//~ for(int i=0;i<n;i++) cout<<tree.get(i, i)<<" ";
//~ cout<<"\n";
}
cout<<tree.get(0, n)<<"\n";
}
/*
3
2 1 0
2 2 1
*/
void solve(){
int n; cin>>n;
vector<int> a(n), b(n);
for(auto& x : a) cin>>x;
for(auto& x : b) cin>>x;
if(n == -1 && n <= 5005){
solve1(n, a, b);
} else {
solve2(n, a, b);
}
}
/*
3
1 2 3
2 2 2
*/
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1;
//~ cin>>t;
while(t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
7 ms |
460 KB |
Output is correct |
3 |
Correct |
22 ms |
1032 KB |
Output is correct |
4 |
Correct |
14 ms |
1100 KB |
Output is correct |
5 |
Correct |
31 ms |
1100 KB |
Output is correct |
6 |
Correct |
15 ms |
1104 KB |
Output is correct |
7 |
Correct |
16 ms |
1100 KB |
Output is correct |
8 |
Correct |
25 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
5 ms |
716 KB |
Output is correct |
5 |
Correct |
6 ms |
744 KB |
Output is correct |
6 |
Correct |
6 ms |
744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
716 KB |
Output is correct |
2 |
Correct |
49 ms |
3708 KB |
Output is correct |
3 |
Correct |
171 ms |
10072 KB |
Output is correct |
4 |
Correct |
153 ms |
10852 KB |
Output is correct |
5 |
Correct |
161 ms |
10816 KB |
Output is correct |
6 |
Correct |
146 ms |
10188 KB |
Output is correct |
7 |
Correct |
144 ms |
10860 KB |
Output is correct |
8 |
Correct |
136 ms |
10088 KB |
Output is correct |
9 |
Correct |
138 ms |
10040 KB |
Output is correct |
10 |
Correct |
109 ms |
10176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |