#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--){
//~ int mx = -1e9;
//~ for(int l=r;l>=e;l--){
//~ mx = max((l ? - cnt[l-1] + dp[l-1] : 0), mx);
//~ mx = max(mx, (l ? - cnt[l-1] + dp[l-1] : 0));
//~ }
dp[r] = max(dp[r], cnt[r] + pref[r]);
}
}
for(int i=0;i<n;i++) res = max(res, dp[i]);
cout<<res<<"\n";
}
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;
}
}
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 <= 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 |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
204 KB |
Output is correct |
2 |
Correct |
5 ms |
460 KB |
Output is correct |
3 |
Correct |
18 ms |
972 KB |
Output is correct |
4 |
Correct |
15 ms |
1112 KB |
Output is correct |
5 |
Correct |
28 ms |
1100 KB |
Output is correct |
6 |
Correct |
15 ms |
1100 KB |
Output is correct |
7 |
Correct |
18 ms |
972 KB |
Output is correct |
8 |
Correct |
28 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
28 ms |
332 KB |
Output is correct |
4 |
Correct |
158 ms |
384 KB |
Output is correct |
5 |
Correct |
164 ms |
384 KB |
Output is correct |
6 |
Correct |
184 ms |
392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
388 KB |
Output is correct |
2 |
Incorrect |
10 ms |
644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
2 ms |
204 KB |
Output is correct |
9 |
Correct |
28 ms |
332 KB |
Output is correct |
10 |
Correct |
158 ms |
384 KB |
Output is correct |
11 |
Correct |
164 ms |
384 KB |
Output is correct |
12 |
Correct |
184 ms |
392 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
5 ms |
332 KB |
Output is correct |
23 |
Correct |
196 ms |
388 KB |
Output is correct |
24 |
Correct |
145 ms |
396 KB |
Output is correct |
25 |
Correct |
102 ms |
388 KB |
Output is correct |
26 |
Correct |
96 ms |
392 KB |
Output is correct |
27 |
Correct |
97 ms |
332 KB |
Output is correct |
28 |
Correct |
96 ms |
332 KB |
Output is correct |
29 |
Correct |
168 ms |
332 KB |
Output is correct |
30 |
Correct |
124 ms |
396 KB |
Output is correct |
31 |
Correct |
145 ms |
392 KB |
Output is correct |
32 |
Correct |
121 ms |
384 KB |
Output is correct |